Problem Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
Input
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
Sample Input
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
***************************************************************************************************************************
3 * 3 grid calculated upper-left corner of {(i / 3) * 3, (j / 3) * 3}
***************************************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdio> 6 using namespace std; 7 int num,su[1001][2],map[11][11],cas; 8 int i,j,k; 9 bool judge(int x,int y,int k) 10 { 11 int it,jt; 12 //同行同列时的判断 13 for(it=0;it<9;it++) 14 { 15 if(map[it][y]==k)return false; 16 if(map[x][it]==k)return false; 17 } 18 it=(x/3)*3;//小方格左上角的数 19 jt=(y/3)*3; 20 for(int st=0;st<3;st++) 21 for(intYT = 0 ; YT < . 3 ; YT ++ ) 22 is IF (Map [IT + ST] [YT + JT] == K) 23 is return to false ; 24 return to true ; 25 } 26 is int DFS ( int CAP) 27 { 28 int IE ; 29 IF (CAP < 0 ) return . 1 ; // real conditions of 30 for (IE = . 1 ; IE <= . 9 ; ++ IE ) 31 is { 32 intXS = SU [CAP] [ 0 ]; 33 is int YS = SU [CAP] [ . 1 ]; 34 is IF (Judge (XS, YS, IE)) 35 { 36 Map [XS] [YS] = IE; 37 [ IF ( DFS (CAP- . 1 )) return . 1 ; // search in the end, the condition returns 38 is Map [XS] [YS] = 0 ; // restore 39 } 40 } 41 is return 0 ; 42 is } 43 is int main () 44 is { 45 scanf("%d",&cas); 46 getchar(); 47 while(cas--) 48 { 49 char ch; 50 num=0; 51 for(i=0;i<9;i++,getchar()) 52 { 53 for(j=0;j<9;j++) 54 { 55 scanf("%c",&ch); 56 Map [I] [J] = CH-, ' 0 ' ; 57 is IF (Map [I] [J] == 0 ) // record the number of positions to be filled 58 { 59 SU [NUM] [ 0 ] = I ; 60 SU [NUM ++] [ . 1 ] = J; 61 is } 62 is } 63 is } 64 DFS (num- . 1 ); 65 // output 66 for (I = 0 ; I < . 9 ; I ++ ) 67 { 68 for(j=0;j<9;j++) 69 printf("%d",map[i][j]); 70 printf("\n"); 71 } 72 printf("\n"); 73 74 } 75 return 0; 76 77 }
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3372893.html