-
Notifications
You must be signed in to change notification settings - Fork 617
/
sudoku_solver.cpp
189 lines (168 loc) · 4.49 KB
/
sudoku_solver.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
1. Sudoku can be solved using Backtracking
2. We take the unfinished Sudoku as input from the user where we fill the 0s.
3. The Sudoku has to be filled so that no number is repeated in a row, column or 3 * 3 submatrix.
4. We try to fill a number with any valid number from 1 to 9 that is not repeated.
5. If after filling all the cells we reach the nth row, we have successfully filled the sudoku.
6. Else we try other options of filling a cell by backtracking.
*/
#include <bits/stdc++.h>
using namespace std;
//Function that checks whether a number is repeated in a row, column or a 3 * 3 submatrix
bool isValid(vector<vector<int>> sudoku, int row, int col, int num)
{
//checking row
for(int i = 0; i < sudoku[0].size(); ++i)
{
if(sudoku[row][i] == num)
return false;
}
//checking column
for(int i = 0; i < sudoku.size(); ++i)
{
if(sudoku[i][col] == num)
return false;
}
//checking submatrix
int submat_row = 3 * row/3; //finding top corner row of 3 * 3 submatrix
int submat_col = 3 * col/3; //finding top corner column of 3 * 3 submatrix
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
if(sudoku[submat_row][submat_col] == num)
return false;
}
}
return true;
}
//Helper function to solve the Sudoku
void sudoku_solver(vector<vector<int>> sudoku, int row, int col)
{
//If we reach the nth row, we have solved the sudoku and hence we print the solved sudoku
if(row == sudoku.size())
{
cout<<"The solved sudoku matrix is : \n";
for(int i = 0; i < sudoku.size(); ++i)
{
for(int j = 0; j < sudoku[0].size(); ++j)
{
cout<<sudoku[i][j]<<" ";
}
cout<<endl;
}
exit(0);
}
//Finding the next row and column
int next_row = 0, next_col = 0;
//if we reach the last column, next row will be current + 1 and next col will be 0 as we move to the first element of the next row
if(col == sudoku[0].size() - 1)
{
next_row = row + 1;
next_col = 0;
}
//Else we just increment the column by 1
else
{
next_row = row;
next_col = col + 1;
}
//If the number is not 0, it is already filled and hence we do not have to fill it
if(sudoku[row][col] != 0)
{
sudoku_solver(sudoku, next_row, next_col);
}
//If the number is not filled
else
{
//We try all possible options from 1 to 9
for(int num = 1; num <= 9; ++num)
{
//If the option is valid, we place the number in that place or move ahead
if(isValid(sudoku, row, col, num))
{
sudoku[row][col] = num;
sudoku_solver(sudoku, next_row, next_col);
//If the number makes the sudoku unsolvable, we remove it and recurse for other options which is the Backtracking step
sudoku[row][col] = 0;
}
}
}
}
int main()
{
cout<<"Enter the size of the sudoku matrix\n";
int n;
cin>>n;
vector<vector<int>> sudoku;
cout<<"Enter the elements of the sudoku matrix\n";
for(int i = 0; i < n; ++i)
{
vector<int> sudoku_row;
for(int j = 0; j < n; ++j)
{
int num;
cin>>num;
sudoku_row.push_back(num);
}
sudoku.push_back(sudoku_row);
}
//solving the sudoku by calling the helper method.
sudoku_solver(sudoku, 0, 0);
return 0;
}
/*
Input 1 :
N = 9
sudoku =
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
0 0 3 0 1 0 0 8 0
9 0 0 8 6 3 0 0 5
0 5 0 0 9 0 6 0 0
1 3 0 0 0 0 2 5 0
0 0 0 0 0 0 0 7 4
0 0 5 2 0 6 3 0 0
Output 1 :
The solved sudoku matrix is :
3 1 6 5 2 8 4 9 7
5 2 1 3 4 7 8 6 9
2 8 7 6 5 4 9 3 1
6 4 3 9 1 5 7 8 2
9 7 2 8 6 3 1 4 5
7 5 8 4 9 1 6 2 3
1 3 4 7 8 9 2 5 6
8 6 9 1 3 2 5 7 4
4 9 5 2 7 6 3 1 8
*/
/*
Input 2 :
N = 9
sudoku =
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 0 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
Output 2 :
The solved sudoku matrix is :
5 3 1 2 7 4 6 9 8
6 2 3 1 9 5 8 4 7
1 9 8 3 4 6 7 5 2
8 4 2 9 6 7 5 1 3
4 7 6 8 5 3 9 2 1
7 1 9 5 2 8 4 3 6
9 6 5 7 3 1 2 8 4
2 8 7 4 1 9 3 6 5
3 5 4 6 8 2 1 7 9
*/
/*
Time Complexity of the approach : O(9 ^ (N * N))
Space Complexity of the approach : O(N * N)
where, N = number of rows and columns of the input matrix
*/