深搜+回溯 POJ 2676 Sudoku

本文提供了一种解决POJ2676数独问题的有效方法,通过设置三个二维布尔数组来记录每行、每列及每个3x3子区域中数字的使用情况,利用深度优先搜索策略实现数独的自动填充。

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

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
我的思路错误之处:我本来只设置了两个二维bool数组表示每行9个数字和每列9个数字,是否用过,再深搜填写9个方格,可是发现,深搜填写每个方格是很难写的。
正解:开是三个二维bool数组,分别表示每列每行每个大方格的数字的使用情况。
读入时注意数字是连起来的。
 1 /*----------------正确代码-------------------------*/
 2 #include<iostream>
 3 using namespace std;
 4 #include<cstdio>
 5 #include<cstring>
 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值