1
+ #include < iostream>
2
+ #include < cstdio>
3
+ #include < cstring>
4
+ #include < cstdlib>
5
+ using namespace std ;
6
+ #define UNASSIGNED 0
7
+ #define N 9
8
+
9
+ bool FindUnassignedLocation (int grid[N][N], int &row, int &col);
10
+ bool isSafe (int grid[N][N], int row, int col, int num);
11
+
12
+ /* assign values to all unassigned locations for Sudoku solution
13
+ */
14
+ bool SolveSudoku (int grid[N][N])
15
+ {
16
+ int row, col;
17
+ if (!FindUnassignedLocation (grid, row, col))
18
+ return true ;
19
+ for (int num = 1 ; num <= 9 ; num++)
20
+ {
21
+ if (isSafe (grid, row, col, num))
22
+ {
23
+ grid[row][col] = num;
24
+ if (SolveSudoku (grid))
25
+ return true ;
26
+ grid[row][col] = UNASSIGNED;
27
+ }
28
+ }
29
+ return false ;
30
+ }
31
+
32
+ /* Searches the grid to find an entry that is still unassigned. */
33
+ bool FindUnassignedLocation (int grid[N][N], int &row, int &col)
34
+ {
35
+ for (row = 0 ; row < N; row++)
36
+ for (col = 0 ; col < N; col++)
37
+ if (grid[row][col] == UNASSIGNED)
38
+ return true ;
39
+ return false ;
40
+ }
41
+
42
+ /* Returns whether any assigned entry n the specified row matches
43
+ the given number. */
44
+ bool UsedInRow (int grid[N][N], int row, int num)
45
+ {
46
+ for (int col = 0 ; col < N; col++)
47
+ if (grid[row][col] == num)
48
+ return true ;
49
+ return false ;
50
+ }
51
+
52
+ /* Returns whether any assigned entry in the specified column matches
53
+ the given number. */
54
+ bool UsedInCol (int grid[N][N], int col, int num)
55
+ {
56
+ for (int row = 0 ; row < N; row++)
57
+ if (grid[row][col] == num)
58
+ return true ;
59
+ return false ;
60
+ }
61
+
62
+ /* Returns whether any assigned entry within the specified 3x3 box matches
63
+ the given number. */
64
+ bool UsedInBox (int grid[N][N], int boxStartRow, int boxStartCol, int num)
65
+ {
66
+ for (int row = 0 ; row < 3 ; row++)
67
+ for (int col = 0 ; col < 3 ; col++)
68
+ if (grid[row+boxStartRow][col+boxStartCol] == num)
69
+ return true ;
70
+ return false ;
71
+ }
72
+
73
+ /* Returns whether it will be legal to assign num to the given row,col location.
74
+ */
75
+ bool isSafe (int grid[N][N], int row, int col, int num)
76
+ {
77
+ return !UsedInRow (grid, row, num) && !UsedInCol (grid, col, num) &&
78
+ !UsedInBox (grid, row - row % 3 , col - col % 3 , num);
79
+ }
80
+
81
+ void printGrid (int grid[N][N])
82
+ {
83
+ for (int row = 0 ; row < N; row++)
84
+ {
85
+ for (int col = 0 ; col < N; col++)
86
+ cout<<grid[row][col]<<" " ;
87
+ cout<<endl;
88
+ }
89
+ }
90
+
91
+ int main ()
92
+ {
93
+ int grid[N][N] = {{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
94
+ {0 , 0 , 0 , 9 , 3 , 7 , 2 , 0 , 0 },
95
+ {0 , 0 , 0 , 2 , 4 , 8 , 9 , 7 , 0 },
96
+ {4 , 0 , 3 , 0 , 0 , 0 , 5 , 6 , 0 },
97
+ {0 , 6 , 0 , 0 , 0 , 0 , 7 , 9 , 0 },
98
+ {0 , 5 , 0 , 7 , 0 , 0 , 3 , 2 , 0 },
99
+ {0 , 9 , 0 , 0 , 0 , 1 , 0 , 0 , 0 },
100
+ {0 , 0 , 6 , 8 , 2 , 0 , 0 , 0 , 0 },
101
+ {1 , 0 , 0 , 0 , 0 , 4 , 0 , 0 , 0 }};
102
+ if (SolveSudoku (grid) == true )
103
+ printGrid (grid);
104
+ else
105
+ cout<<" No solution exists" <<endl;
106
+ return 0 ;
107
+ }
0 commit comments