[Cpp]
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int map [9] [9];
Bool isPlace (int count ){
Int row = count/9;
Int col = count % 9;
Int j;
// Same row
For (j = 0; j <9; ++ j ){
If (map [row] [j] = map [row] [col] & j! = Col ){
Return false;
}
}
// Same Column
For (j = 0; j <9; ++ j ){
If (map [j] [col] = map [row] [col] & j! = Row ){
Return false;
}
}
// Same cell
Int tempRow = row/3*3;
Int tempCol = col/3*3;
For (j = tempRow; j <tempRow + 3; ++ j ){
For (int k = tempCol; k <tempCol + 3; ++ k ){
If (map [j] [k] = map [row] [col] & j! = Row & k! = Col ){
Return false;
}
}
}
Return true;
}
Void backtrace (int count ){
If (count = 81 ){
Cout <"Result:" <endl;
For (int I = 0; I <9; ++ I ){
For (int j = 0; j <9; ++ j ){
Cout <map [I] [j] <"";
}
Cout <endl;
}
Return;
}
Int row = count/9;
Int col = count % 9;
If (map [row] [col] = 0 ){
For (int I = 1; I <= 9; ++ I ){
Map [row] [col] = I; // assign a value to www.2cto.com
If (isPlace (count) {// you can place
Backtrace (count + 1); // enter the next layer
}
}
Map [row] [col] = 0; // backtracking
} Else {
Backtrace (count + 1 );
}
}
Int main ()
{
For (int I = 0; I <9; ++ I ){
For (int j = 0; j <9; ++ j ){
Cin> map [I] [j];
}
}
Backtrace (0 );
Return 0;
}
Test data:
[Plain]
8 0 0 0 0 0 0 1
9 0 0 0 2 0 0 3
0 3 0 0 5 0 7 0
0 0 5 0 0 4 0 0
0 0 4 5 0 9 6 0 0
0 0 0 8 0 1 0 0 0
0 0 0 0 0 0 0 0
0 4 6 0 0 0 8 2 0
0 2 0 3 0 5 0 9 0
Running result:
From # include <cannel_2020>