Topic connection: http://acm.hdu.edu.cn/showproblem.php?pid=1426
Problem DescriptionSince the first Sudoku World Championship from March 10 to 11, 2006, Sudoku has been more and more loved and valued by people. InputThis question consists of several groups of tests separated by a blank line. Each group of tests will give you a 9 * 9 matrix, with two adjacent elements in the same row separated by a space. Among them, 1-9 represents the filled number of the location, and the question mark (?) indicates the number you need to fill in. OutputFor each group of tests, please output its solution. Two adjacent numbers in the same line are separated by a space. There should be a blank line between the two groups of solutions.
|
Solutions:
1. What we need to learn is the input problem. See the code for details
2. First define a structure, including x and y, and then define an array with this structure. When inputting, if it is "?" Put the left side into the array (I think it's a little tricky)
3. Write a check function, which can be used to judge whether there is any repetition in the same row and the same small nine palace lattice when filling in a certain number
4. Write a deep search dfs() function. If it's all filled in, print it out (pay attention to the output format); if it's not filled in, fill in 1-9 one by one and start deep search.
Code:
#include<iostream> using namespace std; struct dd { int x; int y; }a[100];//Coordinates used to store the location to be filled int map[10][10];//Used to store numbers on Sudoku. If it is "?" It is 0 int num;//It is used to count how many numbers need to be filled in int check(int k,int step)//k represents the number to be judged, and step represents the number to be filled in { int i,j,p,q; for(i=0;i<9;i++)//Judge whether the same line and the same column have duplicate if(map[a[step].x][i]==k||map[i][a[step].y]==k) return 0; p=(a[step].x/3)*3; q=(a[step].y/3)*3;//Think about what these two lines are for for(i=p;i<p+3;i++)//Judge whether a 3 * 3 square is repeated for(j=q;j<q+3;j++) if(map[i][j]==k) return 0; return 1; } void dfs(int step) { int i,j; if(num==step)//All have been filled in { for(i=0;i<9;i++)//Print out, pay attention to the output format { for(j=0;j<8;j++) cout<<map[i][j]<<" "; cout<<map[i][8]<<endl; } return ; } for(i=1;i<=9;i++)//1-9 judge whether it can be filled in { if(check(i,step)==1)//i can be filled in { map[a[step].x][a[step].y]=i;//Fill in i dfs(step+1); map[a[step].x][a[step].y]=0;//reduction } } return ; } int main() { int i,j,t=0; char s; while(cin>>s) { num=0; if(s=='?') { a[num].x=0; a[num].y=0;//Because the first "?" The coordinates of are (0, 0) num++; map[0][0]=0; } else map[0][0]=s-'0'; for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(i==0&&j==0) continue; else { cin>>s; if(s=='?') { a[num].x=i; a[num].y=j; num++; map[i][j]=0; } else map[i][j]=s-'0'; } } } if(t++) cout<<endl;//Control line feed dfs(0);//From the first } return 0; }
Now that I've seen it all, why don't you give me some praise and go!!!