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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include<cstdio> #include<cstring> 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"); } } } |