HDU 1426 Sudoku Killer DFS 简单题

给出一个数独的一部分,然后然后要我们填完整这个数独。

Input

本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。

Output

对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。
对于每组测试数据保证它有且只有一个解。

Sample Input

7 1 2 ? 6 ? 3 5 8

? 6 5 2 ? 7 1 ? 4

? ? 8 5 1 3 6 7 2

9 2 4 ? 5 6 ? 3 7

5 ? 6 ? ? ? 2 4 1

1 ? 3 7 2 ? 9 ? 5

? ? 1 9 7 5 4 8 6

6 ? 7 8 3 ? 5 1 9

8 5 9 ? 4 ? ? 2 3

Sample Output

7 1 2 4 6 9 3 5 8

3 6 5 2 8 7 1 9 4

4 9 8 5 1 3 6 7 2

9 2 4 1 5 6 8 3 7

5 7 6 3 9 8 2 4 1

1 8 3 7 2 4 9 6 5

2 3 1 9 7 5 4 8 6

6 4 7 8 3 2 5 1 9

8 5 9 6 4 1 7 2 3

这道题主要是输入很烦。

思路:直接对于每一个空,枚举每一个可能的解,然后DFS,不断填下去,如果能够填完,就输出,且标记over=true

注意回溯。

DFS(int cnt) 当前已经填了cnt-1个数了,要填第cnt个数。

 1 #include<cstdio>
 2 #include<cstring>
 3 int maze[12][12];
 4 int tot;
 5 bool over;
 6 struct Point
 7 {
 8     int x,y;
 9 }point[85];
10 int change(int x)
11 {
12     if(1<=x&&x<=3)
13         return 1;
14     if(4<=x&&x<=6)
15         return 4;
16     if(7<=x&&x<=9)
17         return 7;
18 }
19 bool judge(int cnt,int t)
20 {
21     for(int i=1;i<=9;i++)
22     {
23         if(maze[point[cnt].x][i]==t||maze[i][point[cnt].y]==t)
24             return false;
25     }
26     int u=change(point[cnt].x);
27     int v=change(point[cnt].y);
28     for(int jj=u;jj<=u+2;jj++)
29     {
30         for(int jjj=v;jjj<=v+2;jjj++)
31             if(maze[jj][jjj]==t)
32                 return false;
33     }
34     return true;
35 }
36 void dfs(int cnt)//have been finish the number of cnt
37 {
38     if(cnt==tot)
39     {
40         for(int i=1;i<=9;i++)
41         {
42             for(int j=1;j<9;j++)
43                 printf("%d ",maze[i][j]);
44             printf("%d\n",maze[i][9]);
45         }
46         over=true;
47         return ;
48     }
49     for(int i=1;i<=9;i++)
50     {
51         if(judge(cnt,i)&&!over)
52         {
53             maze[point[cnt].x][point[cnt].y]=i;
54             dfs(cnt+1);
55             maze[point[cnt].x][point[cnt].y]=0;
56         }
57     }
58 }
59 int main()
60 {
61     char s[3];
62     int p=0,i=1,j=1;
63     tot=1;
64     while(scanf("%s",&s)!=EOF)
65     {
66         if(s[0]!=‘?‘)
67         {
68             maze[i][j]=s[0]-‘0‘;
69         }
70         else
71         {
72             maze[i][j]=0;
73             point[tot].x=i;
74             point[tot++].y=j;
75         }
76         j++;
77         if(j>9)
78         {
79             i++;
80             j=1;
81         }
82         if(i==10)
83         {
84             if(p)
85                 printf("\n");
86             p++;
87             over=false;
88             dfs(1);
89             i=j=1;
90             tot=1;
91         }
92     }
93     return 0;
94 }

我的代码

时间: 2024-08-04 21:20:17

HDU 1426 Sudoku Killer DFS 简单题的相关文章

hdu 1426 Sudoku Killer(DFS)

1 #include <iostream> 2 #include <memory.h> 3 #include <vector> 4 #include <string> 5 #include <cstdio> 6 using namespace std; 7 8 int row[11][11],col[11][11],blo[11][11],mp[11][11]; 9 struct node{ 10 int x,y; 11 }; 12 node u

HDU 1426 Sudoku Killer(dfs 解数独)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9804    Accepted Submission(s): 2944 Problem Description 自从2006年3月10日至11日的首届数独世界

HDU 1426 Sudoku Killer(数独,划分区域是关键)

Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6283    Accepted Submission(s): 1981 Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单

HDU 1426 Sudoku Killer【DFS 数独】

自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会. 所以全球人民前仆后继,为了奖品日夜训练茶饭不思.当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的

hdu 1426 Sudoku Killer

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<iostream> 6 using namespace std; 7 char map[12][12]; 8 bool row[12][12],list[12][12

HUD 1426 Sudoku Killer (DFS)

链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数组来辅助记录. $visR[i][num] = 1, 代表第 i 行num值已经出现过, visC[i][num] = 1, 代表第 i 列num值已经出现过, visB[i][num] = 1, 代表第 i 小块 num值已经出现过$. 计算小块的标号只需要 $x / 3 * 3 + y / 3 $ 即可.

hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )

利用 Dancing Link 来解数独 具体的可以看    lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , 是通过求解精确覆盖 精确覆盖就是给出一个 01 矩阵 , 要求我们选择一些行 , 使得每一列有且仅有一个 1 对于数独问题 , 行就是我们的选择 , 即在第 i 行 第 j 列 放上 数字 k , 所以我们最多有 i * j * k 中选择 如果某些位置( x , y  )已经放了数字 a , 那么我们的选择

HDU 1426 Sudoku Killer (回溯 + 剪枝)

题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数.输出这个数独的解,每组有且只有一个解. 思路: 记录下空缺的地方,每个空缺的地方有 9 中状态,DFS + 剪枝处理其他的,用scanf进行输入,gets() TLE到死.... 代码: #include <cstdio> #include <iostream> #include <cstring> #include <cstrin

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b