poj 2676 sudoku dfs
Sudoku
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 10299 | Accepted: 5141 | 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
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
解题思路(转自http://blog.csdn.net/lyy289065406/article/details/6647977):
DFS试探,失败则回溯
用三个数组进行标记每行、每列、每个子网格已用的数字,用于剪枝
bool row[10][10]; //row[i][x] 标记在第i行中数字x是否出现了
bool col[10][10]; //col[j][y] 标记在第j列中数字y是否出现了
bool grid[10][10]; //grid[k][x] 标记在第k个3*3子格中数字z是否出现了
row 和 col的标记比较好处理,关键是找出grid子网格的序号与 行i列j的关系
即要知道第i行j列的数字是属于哪个子网格的
首先我们假设子网格的序号如下编排:
由于1<=i、j<=9,我们有: (其中“/”是C++中对整数的除法)
令a= i/3 , b= j/3 ,根据九宫格的 行列 与 子网格 的 关系,我们有:
不难发现 3a+b=k
即 3*(i/3)+j/3=k
又我在程序中使用的数组下标为 1~9,grid编号也为1~9
因此上面的关系式可变形为 3*((i-1)/3)+(j-1)/3+1=k
有了这个推导的关系式,问题的处理就变得非常简单了,直接DFS即可
代码:
View Code
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 int sudoku[10][10],p[81][2]; //sudoku存放数独 p记录数独中的空格 7 int r[10][10],l[10][10],w[10][10]; //r[i][x]=1表示第i行已经有x这个数了, 8 //同理l[i][x]表示第i列已经有x这个数了 , 9 //w[i][x] 表示第i个九宫个中已经有x这个数了。 10 int n,k; 11 12 int dfs(int x, int y) 13 { 14 int i,j; 15 if(n==k) 16 { 17 for(i=0;i<9;i++) 18 { 19 for(j=0;j<9;j++) 20 { 21 printf("%d",sudoku[i][j]); 22 } 23 printf("\n"); 24 } 25 return 1; 26 } 27 for(i=1;i<=9;i++) 28 { 29 if(!r[x][i] && !l[y][i] && !w[x/3*3+y/3][i]){ 30 sudoku[x][y]=i; 31 r[x][i]=l[y][i]=w[x/3*3+y/3][i]=1; 32 k++; 33 if(dfs(p[k][0],p[k][1])) return 1; 34 k--; 35 sudoku[x][y]=0; 36 r[x][i]=l[y][i]=w[x/3*3+y/3][i]=0; 37 } 38 } 39 return 0; 40 } 41 42 int main() 43 { 44 int t; 45 scanf("%d",&t); 46 while(t--) 47 { 48 memset(r,0,sizeof(r)); 49 memset(w,0,sizeof(w)); 50 memset(l,0,sizeof(l)); 51 int i,j; 52 k=0; 53 for(i=0;i<9;i++) 54 { 55 for(j=0;j<9;j++) 56 { 57 scanf("%1d",&sudoku[i][j]); 58 if(sudoku[i][j]) 59 { 60 r[i][sudoku[i][j]]=1; 61 l[j][sudoku[i][j]]=1; 62 w[i/3*3+j/3][sudoku[i][j]]=1; 63 } 64 else 65 { 66 p[k][0]=i; 67 p[k][1]=j; 68 k++; 69 } 70 } 71 } 72 n=k; 73 k=0; 74 dfs(p[k][0],p[k][1]); 75 } 76 return 0; 77 }