# Openjudge Sudoku 题解

## 2982:Sudoku

2000ms

65536kB

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3×3 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 3×3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

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.

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.

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Southeastern Europe 2005

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 #include #include using namespace std; int array[10][10]; bool linearr[10][10],rowarr[10][10],tablearr[10][10];  //[pos][num] ; default false int table_belong[9][9]={     {1,1,1,2,2,2,3,3,3},     {1,1,1,2,2,2,3,3,3},     {1,1,1,2,2,2,3,3,3},     {4,4,4,5,5,5,6,6,6},     {4,4,4,5,5,5,6,6,6},     {4,4,4,5,5,5,6,6,6},     {7,7,7,8,8,8,9,9,9},     {7,7,7,8,8,8,9,9,9},     {7,7,7,8,8,8,9,9,9}, }; inline int calculate_table(int line,int row){     return table_belong[line-1][row-1]; } void clear(){     memset(linearr,false,sizeof(linearr));     memset(rowarr,false,sizeof(rowarr));     memset(tablearr,false,sizeof(tablearr)); } bool search(int line,int row){     if(row>9){         line++;row=1;         if(line==10)return true;     }     if(array[line][row]!=0)return search(line,row+1);     for(int i=1;i<=9;i++){         if(linearr[line][i]==false&&rowarr[row][i]==false&&tablearr[calculate_table(line,row)][i]==false){             linearr[line][i]=true;             rowarr[row][i]=true;             tablearr[calculate_table(line,row)][i]=true;             bool flag=search(line,row+1);             if(flag){                 array[line][row]=i;                 return true;             }             else{                 linearr[line][i]=false;                 rowarr[row][i]=false;                 tablearr[calculate_table(line,row)][i]=false;             }         }     }     return false; } void solvesudoku(){     search(1,1); } int main(){     int n;     scanf("%d",&n);     for(int k=1;k<=n;k++){         clear();         for(int i=1;i<=9;i++){             for(int j=1;j<=9;j++){                 scanf("%1d",&array[i][j]);                 linearr[i][array[i][j]]=true;                 rowarr[j][array[i][j]]=true;                 tablearr[calculate_table(i,j)][array[i][j]]=true;             }         }         solvesudoku();         for(int i=1;i<=9;i++){             for(int j=1;j<=9;j++){                 printf("%d",array[i][j]);             }             printf("\n");         }     } }