蓝桥杯校内选拔赛/POJ 数独(深搜)

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 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

Source

搜索时要对行,列,每个小数独阵进行判断,判断该数字是否在这三个中出现过,是否出现用标识符标记,处理时i,j从下标0开始,处理行标,列标比较方便。思路较简单,看代码吧。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define MAXN 12
 5 using namespace std;
 6 int num[MAXN][MAXN];
 7 int row[MAXN][MAXN];//用来标记每一行九个数是否已被使用,使用记为1
 8 int col[MAXN][MAXN];//
 9 int mar[MAXN][MAXN];//用0-8标识9个小数独矩阵
10 char chr[MAXN][MAXN];
11 bool flag=false;
12 void dfs(int m,int n)
13 {
14     int s,i,j;
15 
16     if(m==9&&n==0)
17     {
18         for(i=0;i<=8;i++)
19         {
20             for(j=0;j<=8;j++)
21             {
22                 cout<<num[i][j];
23             }
24             cout<<endl;
25         }
26         flag=true;
27     }
28     else
29     {
30         if(num[m][n]!=0)
31         {
32             if(n<=7)
33                 dfs(m,n+1);
34             else
35                 dfs(m+1,0);
36         }
37         else
38         {
39             for(i=1;i<=9;i++)
40         {
41             if(row[m][i]==0&&col[n][i]==0&&mar[m/3*3+n/3][i]==0)//根据i,j标定它所属的行,列,和所在的小矩阵
42             {
43                 s=m/3*3+n/3;
44                 num[m][n]=i;
45                 row[m][i]=1;
46                 col[n][i]=1;
47                 mar[s][i]=1;
48                 if(n<=7)
49                     dfs(m,n+1);
50                 else
51                     dfs(m+1,0);
52                 if(flag)
53                     return ;
54                 num[m][n]=0;//一旦回溯过来,必须重新置0,因为说明此数字对于后面的搜索产生了不满足,故将其置0.
55                 row[m][i]=0;
56                 col[n][i]=0;
57                 mar[s][i]=0;
58             }
59         }
60         }
61     }
62 }
63 int main()
64 {
65     //freopen("data.in","r",stdin);
66    // freopen("data.out","w",stdout);
67     int i,j,s,t;
68     cin>>t;
69     while(t--)
70     {
71         memset(row,0,sizeof(row));
72         memset(col,0,sizeof(col));
73         memset(mar,0,sizeof(mar));
74         for(i=0;i<9;i++)
75             scanf("%s",chr[i]);
76         for(i=0;i<9;i++)
77             for(j=0;j<9;j++)
78         {
79             num[i][j]=chr[i][j]-'0';
80             s=num[i][j];
81             if(s!=0)
82             {
83                 row[i][s]=1;
84                 col[j][s]=1;
85                 mar[i/3*3+j/3][s]=1;//初始,一旦有数字,则要将它所在的行列及小矩阵标为1
86             }
87         }
88         flag=false;
89         dfs(0,0);
90     }
91     return 0;
92 }

 

posted @ 2015-02-27 00:07  fancy_boy  阅读(1477)  评论(0编辑  收藏  举报