博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深搜+回溯 POJ 2676 Sudoku
阅读量:4649 次
发布时间:2019-06-09

本文共 3620 字,大约阅读时间需要 12 分钟。

POJ 2676 Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17627   Accepted: 8538   Special Judge

Description

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

Input

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.

Output

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.

Sample Input

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127 我的思路错误之处:我本来只设置了两个二维bool数组表示每行9个数字和每列9个数字,是否用过,再深搜填写9个方格,可是发现,深搜填写每个方格是很难写的。 正解:开是三个二维bool数组,分别表示每列每行每个大方格的数字的使用情况。 读入时注意数字是连起来的。
1 /*----------------正确代码-------------------------*/ 2 #include
3 using namespace std; 4 #include
5 #include
6 #define N 15 7 char duru[N]; 8 int jz[N][N]; 9 bool flag=false,flagfg[N][N],flaghang[N][N],flaglie[N][N];10 inline void input()11 {12 for(int i=1;i<=9;++i)13 {14 scanf("%s",duru+1);/*注意读入的数字之间没有空格*/15 for(int j=1;j<=9;++j)16 {17 jz[i][j]=duru[j]-'0';18 if(jz[i][j]==0) continue;19 int k=3*((i-1)/3)+(j-1)/3+1;20 flagfg[k][jz[i][j]]=true;21 flaghang[i][jz[i][j]]=true;22 flaglie[j][jz[i][j]]=true;23 }24 }25 }26 void output()27 {28 for(int i=1;i<=9;++i)29 {30 for(int j=1;j<=9;++j)31 printf("%d",jz[i][j]);32 printf("\n");33 }34 }35 void dfs(int x,int y)36 {37 if(x==10)38 {39 flag=true;40 return;41 }42 if(jz[x][y])43 {44 if(y==9)45 dfs(x+1,1);46 else dfs(x,y+1);47 if(flag) return; 48 }49 else {50 int k=3*((x-1)/3)+(y-1)/3+1;51 for(int i=1;i<=9;++i)52 {53 if(!flaghang[x][i]&&!flaglie[y][i]&&!flagfg[k][i])54 {
/*枚举符合三个条件的i*/55 jz[x][y]=i;56 flaghang[x][i]=true;57 flaglie[y][i]=true;58 flagfg[k][i]=true;59 if(y==9)60 dfs(x+1,1);61 else dfs(x,y+1);62 if(flag) return;63 jz[x][y]=0;/*回溯*/64 flaghang[x][i]=false;65 flaglie[y][i]=false;66 flagfg[k][i]=false;67 68 }69 }70 }71 }72 int main()73 {74 int T;75 scanf("%d",&T);76 while(T--)77 {78 input();79 dfs(1,1);80 output();81 memset(jz,0,sizeof(jz));82 memset(flagfg,false,sizeof(flagfg));83 memset(flaghang,false,sizeof(flaghang));84 memset(flaglie,false,sizeof(flaglie));85 flag=false;86 }87 return 0;88 }

 

转载于:https://www.cnblogs.com/c1299401227/p/5578584.html

你可能感兴趣的文章