Sudoku
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 14530 |
|
Accepted: 7178 |
|
Special Judge |
Description
Sudoku is a very simple task. A Square Table with 9 rows and 9 columns are divided to 9 smaller squares 3x3 as shown on the figure. In some of the cells is written decimal digits from 1 to 9. The other cells is empty. The goal is to fill the empty cells with a decimal digits from 1 to 9, one digit per cell, and in such. Each column and in each marked a 3x3 subsquare, all of the digits from 1 through 9 to appear. Write a program to solve a given sudoku-task.
Input
The input data would start with the number of the 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 are given, corresponding to the cells in this line. If a cell is empty it's 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 has the filled according to the rules. If Solutions is isn't unique, then the program could print any one of the them.
Sample Input
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127
Source
Southeastern Europe 2005
When searching for rows, columns, each decimal single-array to determine whether the number appears in these three, whether it appears with an identifier tag, processing i,j from subscript 0, processing row labels, column label is more convenient. The idea is simple, look at the code bar.
1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 125 using namespacestd;6 intNUM[MAXN][MAXN];7 intROW[MAXN][MAXN];//Used to Mark whether nine numbers per row have been used, use 18 intCOL[MAXN][MAXN];//column9 intMAR[MAXN][MAXN];//Identify 9 decimal single matrices with 0-8Ten CharCHR[MAXN][MAXN]; One BOOLflag=false; A voidDfsintMintN) - { - ints,i,j; the - if(m==9&&n==0) - { - for(i=0; i<=8; i++) + { - for(j=0; j<=8; j + +) + { Acout<<Num[i][j]; at } -cout<<Endl; - } -flag=true; - } - Else in { - if(num[m][n]!=0) to { + if(n<=7) -DFS (m,n+1); the Else *DFS (m+1,0); $ }Panax Notoginseng Else - { the for(i=1; i<=9; i++) + { A if(row[m][i]==0&&col[n][i]==0&&mar[m/3*3+n/3][i]==0)//according to I,j, the row, column, and small matrix where it belongs. the { +s=m/3*3+n/3; -num[m][n]=i; $row[m][i]=1; $col[n][i]=1; -mar[s][i]=1; - if(n<=7) theDFS (m,n+1); - ElseWuyiDFS (m+1,0); the if(flag) - return ; Wunum[m][n]=0;//once back, you have to reset 0, because it indicates that the number is not satisfied with the subsequent search, so it is set to 0. -row[m][i]=0; Aboutcol[n][i]=0; $mar[s][i]=0; - } - } - } A } + } the intMain () - { $ //freopen ("data.in", "R", stdin); the //freopen ("Data.out", "w", stdout); the inti,j,s,t; theCin>>T; the while(t--) - { inmemset (Row,0,sizeof(Row)); thememset (Col,0,sizeof(col)); thememset (Mar,0,sizeof(Mar)); About for(i=0;i<9; i++) thescanf"%s", Chr[i]); the for(i=0;i<9; i++) the for(j=0;j<9; j + +) + { -num[i][j]=chr[i][j]-'0'; thes=Num[i][j];Bayi if(s!=0) the { therow[i][s]=1; -col[j][s]=1; -mar[i/3*3+j/3][s]=1;//Initially, once you have a number, mark the row and the small matrix as 1 the } the } theflag=false; theDfs0,0); - } the return 0; the}
Blue Bridge Cup in-school trials/poj Sudoku (Deep Search)