Skip to content

Commit 1f7cd15

Browse files
Add English Tutorial for LOJ-1397: Sudoku Solver (#400)
* Create 1397 Solution to 1397 * Delete 1397 * Create en.md Update to resolve issues based on given suggestions * Fixed minor details that the authors didn't address --------- Co-authored-by: Rafid Bin Mostofa <[email protected]>
1 parent 51bfdae commit 1f7cd15

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

1397/en.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Sudoku Solver
2+
3+
###### Helpful Resources:
4+
5+
[Sudoku Solver Geeks For Geeks](https://www.geeksforgeeks.org/sudoku-backtracking-7/)
6+
7+
[Understanding Sudo solves in CPP](https://www.youtube.com/watch?v=lIOupNAafT0)
8+
9+
[Introduction to Backtracking](https://www.youtube.com/watch?v=DKCbsiDBN6c)
10+
11+
###### Solution Approach
12+
13+
Firstly we implement a basic sudoku solver with five important points:
14+
15+
1)We first have to find the empty positions for that we have the findEmptyLocation function
16+
17+
2) To solve a sudoku we have to place a number at an empty position denoted by '.' in this question
18+
19+
3) We use the isSafe function to determine whether it is valid to place a particular number here
20+
21+
4) If empty location found, we iterate from 1 to 9 and try to place that number in that empty position, checking whether it is safe to place the value in each iteration
22+
23+
5) If the empty positions are such that we can't just place 1 to 9, then we can conclude that the Sudoku is not solvable and the empty positions remain as 0 denoting that it cannot be filled
24+
25+
To summarise,
26+
27+
In each iteration, we find an empty location. At the empty location, we place a value absent from the same row, box and column, and check if the current state of board can be solved. If it cannot be solved, we output so.
28+
Otherwise we place the appropriate value and finally print the final state of the sudoku matrix.
29+
30+
31+
Note: This is a classic backtracking problem, it does not require dynamic programming approach, it cannot be solved simply by a naive brute force solution.
32+
33+
34+
## Solution in C++
35+
```cpp
36+
#include<bits/stdc++.h>
37+
using namespace std;
38+
#define N 9
39+
40+
int grid[N][N];
41+
42+
bool isSafeInRow(int& row,int value){
43+
for(int i=0;i<N;i++){
44+
if(grid[row][i]==value){
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
51+
bool isSafeInCol(int& col,int value){
52+
for(int i=0;i<N;i++){
53+
if(grid[i][col]==value){
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
60+
bool isSafeBox(int& row,int& col, int value){
61+
int rowFactor=row-row%3;
62+
int colFactor=col-col%3;
63+
for(int i=0;i<3;i++){
64+
for(int j=0;j<3;j++){
65+
if(grid[i+rowFactor][j+colFactor]==value){
66+
return false;
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
73+
bool isSafe(int& row,int& col,int value){
74+
if(isSafeInRow(row,value) && isSafeInCol(col,value) && isSafeBox(row,col,value)){
75+
return true;
76+
}
77+
return false;
78+
}
79+
80+
bool findEmptyLocation(int& x,int& y){
81+
int Min=10,cnt=0;
82+
for (int i = 0; i < N; i++)
83+
{
84+
for (int j = 0; j < N; j++)
85+
{
86+
if(grid[i][j]==0){
87+
int cnt=0;
88+
for(int k=1;k<=9;k++)
89+
{
90+
if (isSafe(i,j,k))
91+
cnt++;
92+
}
93+
if(cnt<Min)
94+
{
95+
Min=cnt;
96+
x=i;
97+
y=j;
98+
}
99+
}
100+
}
101+
}
102+
if(Min<10)
103+
return true;
104+
return false;
105+
}
106+
107+
108+
109+
bool solveSudoku(){
110+
int row,col;
111+
if(!findEmptyLocation(row,col)){
112+
return true;
113+
}
114+
for(int i=1;i<=9;i++){
115+
if(isSafe(row,col,i)){
116+
grid[row][col]=i;
117+
if(solveSudoku()){
118+
return true;
119+
}
120+
grid[row][col]=0;
121+
}
122+
}
123+
return false;
124+
}
125+
126+
void solve(){
127+
string inp[N];
128+
string s;
129+
getline(cin,s);
130+
for(int i=0;i<N;i++){
131+
getline(cin,inp[i]);
132+
}
133+
for(int i=0;i<N;i++){
134+
for(int j=0;j<N;j++){
135+
if(inp[i][j]=='.'){
136+
grid[i][j]=0;
137+
}
138+
else grid[i][j]=inp[i][j]-'0';
139+
}
140+
}
141+
solveSudoku();
142+
for(int i=0;i<N;i++){
143+
for(int j=0;j<N;j++){
144+
cout<<grid[i][j];
145+
}
146+
cout<<endl;
147+
}
148+
}
149+
150+
151+
152+
int main(void){
153+
int t;
154+
cin>>t;
155+
cin.ignore();
156+
for(int i=1;i<=t;i++){
157+
cout<<"Case "<<i<<": "<<endl;
158+
solve();
159+
}
160+
return 0;
161+
}
162+
163+
```

0 commit comments

Comments
 (0)