2924 数独挑战

2924 数独挑战

 

时间限制: 1 s
空间限制: 1000 KB
题目等级 : 钻石 Diamond
 
 
 
 
题目描述 Description

“芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而且它只有一个答案。因卡拉说只有思考能力最快、头脑最聪明的人才能破解这个游戏。”这是英国《每日邮报》2012年6月30日的一篇报道。这个号称“世界最难数独”的“超级游戏”,却被扬州一位69岁的农民花三天时间解了出来。

看到这个新闻后,我激动不已,证明我们OI的实力的机会来了,我们虽然不是思考能力最快、头脑最聪明的人,但是我们可以保证在1s之内解题。

好了废话不多说了……

数独是一种填数字游戏,英文名叫Sudoku,起源于瑞士,上世纪70年代由美国一家数学逻辑游戏杂志首先发表,名为Number Place,后在日本流行,1984年将Sudoku命名为数独,即“独立的数字”的省略,解释为每个方格都填上一个个位数。2004年,曾任中国香港高等法院法官的高乐德(Wayne Gould)把这款游戏带到英国,成为英国流行的数学智力拼图游戏。

  玩家需要根据9×9盘面上的已知数字,推理出所有剩余位置(数据表示为数字0)的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复。

现在给你一个数独,请你解答出来。每个数独保证有解且只有一个。

输入描述 Input Description

9行9列。

每个数字用空格隔开。0代表要填的数

行末没有空格,末尾没有回车。

输出描述 Output Description

输出答案。

排成9行9列。

行末没有空格,结尾可以有回车。

样例输入 Sample Input

2 0 0 0 1 0 8 9 0
0 0 7 0 0 0 0 0 0
0 0 0 9 0 0 0 0 7
0 6 0 0 0 1 3 0 0
0 9 0 7 3 4 0 8 0
0 0 3 6 0 0 0 5 0
6 0 0 0 0 2 0 0 0
0 0 0 0 0 0 1 0 0
0 5 9 0 8 0 0 0 3

样例输出 Sample Output

2 4 5 3 1 7 8 9 6
9 1 7 2 6 8 5 3 4
3 8 6 9 4 5 2 1 7
4 6 2 8 5 1 3 7 9
5 9 1 7 3 4 6 8 2
8 7 3 6 2 9 4 5 1
6 3 8 1 7 2 9 4 5
7 2 4 5 9 3 1 6 8
1 5 9 4 8 6 7 2 3

数据范围及提示 Data Size & Hint

保证有解,每个数独都由<a href="http://oubk.com/">http://oubk.com</a>数独网提供。

其中数据hard1.in为芬兰数学家提供。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include<cstring>
 4 #include<string>
 5 using namespace std;
 6 
 7 const int N = 9;
 8 const int Group[9][9] = 
 9 {
10     0, 0, 0, 1, 1, 1, 2, 2, 2,
11     0, 0, 0, 1, 1, 1, 2, 2, 2,
12     0, 0, 0, 1, 1, 1, 2, 2, 2,
13     3, 3, 3, 4, 4, 4, 5, 5, 5,
14     3, 3, 3, 4, 4, 4, 5, 5, 5,
15     3, 3, 3, 4, 4, 4, 5, 5, 5,
16     6, 6, 6, 7, 7, 7, 8, 8, 8,
17     6, 6, 6, 7, 7, 7, 8, 8, 8,
18     6, 6, 6, 7, 7, 7, 8, 8, 8
19 };
20 
21 int a[N][N];
22 bool row[N][N], col[N][N], gr[N][N];
23 void print()
24 {
25     //printf("One Possible Solution:\n");
26     for(int i=0; i<N; i++)
27     {
28         for(int j=0; j<N; j++) printf("%d ", a[i][j] + 1);
29         printf("\n");
30     }
31     printf("\n");
32 }
33 void dfs3(int x, int y)
34 {
35     if(x == N)//jie  shu
36     {
37         print();
38         return;
39     }
40     int next_x = x, next_y = y + 1;//deng jia yv cong sou suo ci dian de tong hang qie xia yi lie;
41     if(next_y == N) next_x = x + 1, next_y = 0;// lie dao da n ,ze sou suo xia yi hang ,lie y wei 0;
42     if(a[x][y] >= 0) dfs3(next_x, next_y);//ru guo ci dian bu wei 0,jiu cong  ci dian sou suo xia qv;
43     else// ruo ci dian wei 0;则此时151行自减为-1; 
44     {
45         for(int i=0; i<N; i++)
46             if(!row[x][i] && !col[y][i] && !gr[Group[x][y]][i])//ruo ci 数未出现, 
47             {
48                 row[x][i] = col[y][i] = gr[Group[x][y]][i] = 1;//标记 
49                 a[x][y] = i;//记录 
50                 dfs3(next_x, next_y);//搜索 
51                 a[x][y] = -1;//回溯 
52                 row[x][i] = col[y][i] = gr[Group[x][y]][i] = 0;//回溯 
53             }
54     }
55 }
56 
57 int main()
58 {
59     
60     for(int i=0; i<N; i++)
61         for(int j=0; j<N; j++)
62             scanf("%d", &a[i][j]), a[i][j]--;
63     //printf("Dfs Method3:\n");
64 
65     for(int i=0; i<N; i++)
66         for(int j=0; j<N; j++)
67             if(a[i][j] >= 0) 
68                 row[i][a[i][j]] = 1,//di i hang a[i][j]=1;          hang  标记为此行已经有a[i][j]这个数 
69                 col[j][a[i][j]] = 1,//di j lie ;                    lie   标记为此列已经有a[i][j]这个数
70                 gr[Group[i][j]][a[i][j]] = 1;//Group 3*3 fang ge;   3*3   标记为此3*3已经有a[i][j]这个数
71                 //此处由Group[i][j]
72     dfs3(0, 0);
73     //cong 0,0 kai shi
74     return 0;
75 }

 

posted @ 2017-05-04 19:46  ioioioioioio  阅读(135)  评论(0编辑  收藏  举报