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.
![](https://i-blog.csdnimg.cn/blog_migrate/1cff910f31f7f11d0a712030a1408c57.jpeg)
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
---------------------------------------------code--------------------------------------------------------------
同一行,同一列,同一个3*3的方格中,不能有重复的数字。
没有加剪枝,一阵爆搜。。。用了位运算判重。
4178209 | chinaeli | 2676 | Accepted | 204K | 516MS | C++ | 1881B | 2008-10-05 18:00:13 |
- #include<iostream>
- using namespace std;
- int c[9],r[9],s[9];
- int board[9][9],sum;
- bool flag;
- int square ( int m , int n )
- {
- if ( m>=0 && m<3 && n>=0 && n<3 )
- return 0;
- if ( m>=0 && m<3 && n>=3 && n<6 )
- return 1;
- if ( m>=0 && m<3 && n>=6 && n<9 )
- return 2;
- if ( m>=3 && m<6 && n>=0 && n<3 )
- return 3;
- if ( m>=3 && m<6 && n>=3 && n<6 )
- return 4;
- if ( m>=3 && m<6 && n>=6 && n<9 )
- return 5;
- if ( m>=6 && m<9 && n>=0 && n<3 )
- return 6;
- if ( m>=6 && m<9 && n>=3 && n<6 )
- return 7;
- if ( m>=6 && m<9 && n>=6 && n<9 )
- return 8;
- }
- void init ( )
- {
- int i,j,k;
- memset(c,0,sizeof(c));
- memset(r,0,sizeof(r));
- memset(s,0,sizeof(s));
- sum=0;
- for ( i=0 ; i<9 ; i++ )
- for ( j=0 ; j<9 ; j++ )
- {
- scanf("%1d",&board[i][j]);
- if ( board[i][j] )
- {
- r[i]|=(1<<board[i][j]);
- c[j]|=(1<<board[i][j]);
- s[square(i,j)]|=(1<<board[i][j]);
- }
- else
- sum++;
- }
- flag=false;
- }
- void dfs ( int m , int n , int left )
- {
- int i,j;
- if ( m==9 || !left )
- {
- if ( !left )
- flag=true;
- return;
- }
- else
- if ( board[m][n] )
- {
- if ( n==8 )
- dfs(m+1,0,left);
- else
- dfs(m,n+1,left);
- }
- else
- {
- for ( i=1 ; i<=9 && !flag ; i++ )
- {
- if ( !((r[m]>>i)&1) && !( (c[n]>>i)&1) && !( (s[square(m,n)]>>i)&1) )
- {
- r[m]|=1<<i,c[n]|=1<<i;
- s[square(m,n)]|=1<<i;
- board[m][n]=i;
- if ( n==8 )
- dfs(m+1,0,left-1);
- else
- dfs(m,n+1,left-1);
- if ( !flag )
- {
- board[m][n]=0;
- r[m]^=1<<i,c[n]^=1<<i,s[square(m,n)]^=1<<i;
- }
- else
- return;
- }
- }
- }
- }
- void print ( )
- {
- int i,j;
- for ( i=0 ; i<9 ; i++ )
- {
- for ( j=0 ; j<9 ; j++ )
- printf("%d",board[i][j]);
- printf("/n");
- }
- }
- int main ( )
- {
- int n;
- scanf("%d",&n);
- while ( n-- )
- {
- init();
- dfs(0,0,sum);
- print();
- }
- }