Sudoku
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 16024 |
|
Accepted: 7824 |
|
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
Idea: Forward or reverse enumeration of 81 small cells, when the value of the cell is 0 o'clock, try to fill 1-9 number, to see if the requirements (that is, 3*3 cell, row and column are different) the difficulty is the operation of the control program
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <cmath > #include <queue> #define LL long longusing namespace Std;int n,l;int Map[10][10];char s[10][10];bool flag;int Ans;bool judge (int x,int y) {int i,j,k; For (i=x/3*3, i<x/3*3+3; i++) {for (j=y/3*3; j<y/3*3+3; J + +) {if (i==x&&y==j) Continue else if (Map[x][y]==map[i][j]) return false; }} K=map[x][y]; for (j=0; j<9; J + +) {if (j==y) continue; if (map[x][j]==k) return false; } for (i=0; i<9; i++) {if (i==x) continue; if (map[i][y]==k) return false; } return true; int dfs (int n) {int i,j,k; if (n<0)//Use tag variable to control backtracking {flag=true; return 1; } if (map[n/9][n%9]!=0)//If the current cell does not continue recursively for 0 next cell Dfs (N-1); else//otherwise enumerates the current condition {for (i=1;i<=9;i++) {map[n/9][n%9]=i; if (judge (n/9,n%9))//If it complies with the above 3 conditions {DFS (n-1);//continue to recursively next cell, at the same time there are non-eligible, until after all the search, if the enumeration after 1-9 all the number is still not Qualifying will return 0 so that backtracking changes the previous value if (flag)//If it has been found marked down, always return to the end of return 1; } map[n/9][n%9]=0; }} return 0;} int main () {int cla,i,j; scanf ("%d", &CLA); GetChar (); while (cla--) {flag=false; for (i=0; i<9; i++) {gets (s[i]); for (j=0; j<9; J + +) {map[i][j]=s[i][j]-' 0 '; }} dfs (80); for (i=0; i<9; i++) {for (j=0; j<9; J + +) {printf ("%d", map[i][j]); } printf ("\ n"); } printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2676 Sudoku (DFS)