-
Notifications
You must be signed in to change notification settings - Fork 114
/
Sudoku.cpp
63 lines (62 loc) · 1.51 KB
/
Sudoku.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
bool isPossible(int matrix[9][9], int r, int c, int k){
for(int i=0;i<9;i++){
if(matrix[i][c] == k){
return false;
}
if(matrix[r][i] == k){
return false;
}
}
int rstart = 3 * (r/3);
int cstart = 3 * (c/3);
for(int i=rstart;i<rstart+3;i++){
for(int j=cstart;j<cstart+3;j++){
if(matrix[i][j] == k){
return false;
}
}
}
return true;
}
bool isItSudoku(int matrix[9][9]) {
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(matrix[i][j] == 0){
for(int k=1;k<=9;k++){
if(isPossible(matrix, i, j, k)){
matrix[i][j] = k;
if(isItSudoku(matrix)){
return true;
}
matrix[i][j] = 0;
}
}
return false;
}
}
}
return true;
}
int main() {
int matrix[9][9];
cout <<"Enter the sudoku matrix: " <<endl;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cin >>matrix[i][j];
}
}
if(isItSudoku(matrix)){
cout <<"The solved sudoku is: " <<endl;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout <<matrix[i][j] <<" ";
}
cout <<endl;
}
}
else{
cout <<"The sudoku is not solvable" <<endl;
}
}