Sudoku concept, Idea, solution idea, C++code
The overall code of the blog's thinking is very clear, it is worth learning.
The above blog code DFS knowledge output an array of answers, if the number is unique to multiple solutions, simply change the DFS code simple (as follows):
void dfs (int row,int col) {if (row>9)//if row exceeds 9 lines, direct output {for (int i=1; i<=9 ; i++) {for (int j=1; j<=9; J + +) {printf ("%d", a[i][j]); (j = = 9)?
cout << "": cout << "";
printf ("\ n");
} cout << "***************************************\n";
Exit (0),//returned to the operating system by the parameter, 0 indicates a normal end, and Non-zero indicates an abnormal end.
return; } if (a[row][col]==0)//If there is not a number {for (int i=1; i<=9; i++) {if (Is_row_col_repea
T (Row, col, i) && is_block_repeat (row, col, i))//If row nine Sudoku do not repeat {a[row][col]=i;//fill digit
DFS (row+ (col+1)/10, (Col <= 8)? col+1:1);//Continue searching a[row][col]=0;//reset to 0} //a[row][col]=0;//reset to 0} else//if already filled, continue searching {DFS (row+ (col+1)/10, col <= 8)? col
+1:1); }
}
The
Overall code is as follows:
#include <bits/stdc++.h> using namespace std; int a[10][10];//stores the number int is_row_col_repeat (int row,int col,int num)//Judging whether the rows are repeated {//whether the row repeats for (int i=1; i<=9; i++) {if (a[row][i]==num) {return 0;//row repeat, returns 0}}//Determine whether the column repeats for T I=1; i<=9;
i++) {if (a[i][col]==num) {return 0;//column repeat, returned 0}} returns 1;//row does not repeat, returns 1}
int check_row_range (int row)//judge the range of the row {if (row>=1 && row<=3) {return 1;
else if (row>=4&&row<=6) {return 4;
else {return 7;
} int check_col_range (int col)//Judgment column range {if (col>=1&&col<=3) {return 1;
else if (col>=4&&col<=6) {return 4;
else {return 7;
} int is_block_repeat (int row,int col,int num)//Determine if the same color nine sudoku repeat {int x,y;
X=check_row_range (row); Y=check_col_raNge (COL);
for (int i=x; i<=x+2; i++) {for (int j=y; j<=y+2; J + +) {if (a[i][j]==num)
{return 0;//repeat, returns 0}}} Return 1;//does not repeat, returns 1} void Dfs (int row,int col) {if (row>9)//if row exceeds 9 lines, direct output {for (int i=1; i< ; = 9; i++) {for (int j=1; j<=9; J + +) {printf ("%d", a[i][j]); (j = = 9)?
cout << "": cout << "";
printf ("\ n");
} cout << "***************************************\n";
Exit (0);
return; } if (a[row][col]==0)//If there is not a number {for (int i=1; i<=9; i++) {if (Is_row_col_repea
T (Row, col, i) && is_block_repeat (row, col, i))//If row nine Sudoku do not repeat {a[row][col]=i;//fill digit
DFS (row+ (col+1)/10, (Col <= 8)? col+1:1);//Continue searching a[row][col]=0;//reset to 0 //a[row][col]=0;//reset to 0} else//if already filled in, continue searching for {DFS (row+ (col+1)/10, col <= 8)?
COL+1:1);
int main (int argc, char *argv[]) {string S; for (int i=1; i<=9; i++) {cin >> s;//input string for (int j=1; j<=9; J + +) {C
Har ss=s.at (j-1), and/or the j-1 character a[i][j]=ss-' 0 ';//Convert SS to Integer} DFS (1,1);
return 0;
}
The above code looks pretty long, I try to compress it, it's not that ugly, and it's not that long.
#include <bits/stdc++.h> using namespace std;
const int N = 10;
int a[n][n]; int is_row_col_repeat (int row, int col, int num) {for (int i = 1; I <= 9; i++) if (a[row][i] = num | | a[i][
COL] = = num) return 0;
return 1;
int check_row_range (int row) {if (row >= 1 && row <= 3) return 1;
else if (row >= 4 && row <= 6) return 4;
else return 7;
int check_col_range (int col) {if (Col >= 1 && Col <= 3) return 1;
else if (Col >= 4 && Col <= 6) return 4;
else return 7;
int is_block_repeat (int row, int col, int num) {int x = Check_row_range (row);
int y = check_col_range (col); for (int i = x; I <= x+2. i++) for (int j = y; J <= Y+2; j +) if (a[i][j] = num) r
Eturn 0;
return 1;
void output () {for (int i = 1; I <= 9; i++) for (int j = 1; J <= 9; j +) cout << A[i][j], (J <= 8)?
printf (""): Putchar (' \ n ');
printf ("************************\n");
void Dfs (int row, int col) {if (Row > 9) output (), exit (0); if (a[row][col] = = 0) for (int i = 1; I <= 9; i++) if (is_row_col_repeat (row, col, i) && IS_BL
Ock_repeat (Row, col, i)) {A[row][col] = i;
DFS (row + (col+1)/ten, (Col <= 8)? col+1:1);
A[row][col] = 0;
} if (A[row][col]!= 0) DFS (row + (col+1)/ten (Col <= 8)? col+1:1);
int main () {string S;
for (int i = 1; I <= 9; i++) {cin >> S;
for (int j = 1; J <= 9; j +) {char SS = s.at (j-1);
A[I][J] = SS-' 0 ';
} DFS (1, 1);
return 0;
}