Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Mine
#include <iostream> #define N 3 using namespace std; int grid[N][N] = { {3, 0, 6, 5, 0, 8, 4, 0, 0}, {5, 2, 0, 0, 0, 0, 0, 0, 0}, {0, 8, 7, 0, 0, 0, 0, 3, 1}, }; bool isPresentInCol(int col, int num){ //check whether num is present in col or not for (int row = 0; row < N; row++) if (grid[row][col] == num) return true; return false; } bool isPresentInRow(int row, int num){ //check whether num is present in row or not for (int col = 0; col < N; col++) if (grid[row][col] == num) return true; return false; } bool isPresentInBox(int boxStartRow, int boxStartCol, int num){ //check whether num is present in 3x3 box or not for (int row = 0; row < 1; row++) for (int col = 0; col < 1; col++) if (grid[row+boxStartRow][col+boxStartCol] == num) return true; return false; } void sudokuGrid(){ //print the sudoku grid after solve for (int row = 0; row < N; row++){ for (int col = 0; col < N; col++){ if(col == 0 || col == 2) cout << " | "; cout << grid[row][col] <<" "; } if(row == 0 || row == 2){ cout << endl; for(int i = 0; i<N; i++) cout << "---"; } cout << endl; } } bool findEmptyPlace(int &row, int &col){ //get empty location and update row and column for (row = 0; row < N; row++) for (col = 0; col < N; col++) if (grid[row][col] == 0) //marked with 0 is empty return true; return false; } bool isValidPlace(int row, int col, int num){ //when item not found in col, row and current 3x3 box return !isPresentInRow(row, num) && !isPresentInCol(col, num) && !isPresentInBox(row - row%1, col - col%1, num); } bool solveSudoku(){ int row, col; if (!findEmptyPlace(row, col)) return true; //when all places are filled for (int num = 1; num <= 9; num++){ //valid numbers are 1 - 9 if (isValidPlace(row, col, num)){ //check validation, if yes, put the number in the grid grid[row][col] = num; if (solveSudoku()) //recursively go for other rooms in the grid return true; grid[row][col] = 0; //turn to unassigned space when conditions are not satisfied } } return false; } int main(){ if (solveSudoku() == true) sudokuGrid(); else cout << "No solution exists"; }
run
|
edit
|
history
|
help
0
Test 11(2020)
12
VecHotel2
basic caculate ii
Bitwise - Check power of 2 or not
initializer_list example
Print Euler Path
proga2
dsu on tree (http://codeforces.com/contest/208/problem/E)
Boost phoenix. e.g 3 phoenix: functor