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<cstring>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int map[15][15];
 6 int group[15][15];
 7 int h[15][15];
 8 int z[15][15];
 9 int x[15][15];
10 bool flag=true;
11 void chu()
12 {
13     int a=1;
14     for(int i=1; i<=9; ++i)
15     {
16         if(i>3&&i<=6)a=4;
17         else if(i>6)a=7;
18         for(int j=1; j<=9; ++j)
19         {
20             if(j<=3)group[i][j]=a;
21             else if(j>3&&j<=6)group[i][j]=a+1;
22             else if(j>6)group[i][j]=a+2;
23         }
24     }
25 }
26 void print()
27 {
28     for(int i=1; i<=9; ++i)
29     {
30         for(int j=1; j<=9; ++j)
31             cout<<map[i][j]<<" ";
32         cout<<endl;
33     }
34     flag=false ;
35 }
36 void search(int a,int b)
37 {
38     if(flag==false)return ;
39     if(map[a][b]!=0)
40     {
41         if(a==9&&b==9)print();
42         if(b==9) search(a+1,1);else search(a,b+1);
43     }
44     if(map[a][b]==0)
45     {
46         for(int i=1; i<=9; ++i)
47         {
48             if(h[a][i]==0 && z[b][i]==0 && x[group[a][b]][i]==0)
49             {
50                 map[a][b]=i;
51                 h[a][i]=1;
52                 z[b][i]=1;
53                 x[group[a][b]][i]=1;
54                 if(a==9&&b==9)print();
55                 if(b<9)search(a,b+1);
56                 else search(a+1,1);
57                 map[a][b]=0;
58                 h[a][i]=0;
59                 z[b][i]=0;
60                 x[group[a][b]][i]=0;
61             }
62         }
63         
64     }
65     
66 }
67 int main()
68 {
69     chu();
70     memset(map,0,sizeof(map));
71     for(int i=1; i<=9; ++i)
72     {
73         for(int a,j=1; j<=9; ++j)
74         {
75             cin>>a;
76             map[i][j]=a;
77             if(a>0)
78             {
79                 h[i][a]=1;
80                 z[j][a]=1;
81                 x[group[i][j]][a]=1;
82             }
83         }
84     }
85     search(1,1);
86     return 0;
87 }

 

原文地址:https://www.cnblogs.com/mjtcn/p/6785043.html