codevs 2924 数独

 数独挑战

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codevs.cn/problem/2924/

Description

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

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

好了废话不多说了……

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

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

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

Input

9行9列。

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

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

Output

输出答案。

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

HINT

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

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

题意

题解:

dfs

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <queue>
10 #include <map>
11 #include <stack>
12 #define MOD 1000000007
13 using namespace std;
14 typedef long long LL;
15 int read()
16 {
17     int x=0,f=1;
18     char ch=getchar();
19     while(ch<'0'||ch>'9')
20     {
21         if(ch=='-')f=-1;
22         ch=getchar();
23     }
24     while(ch>='0'&&ch<='9')
25     {
26         x=x*10+ch-'0';
27         ch=getchar();
28     }
29     return x*f;
30 }
31 //**********************************************************
32 int a[10][10];
33 bool flag=false;
34 void dfs(int ,int);
35 bool jug(int x,int y,int k)
36 {
37     for(int i=1;i<=9;i++)
38         if(a[x][i]==k||a[i][y]==k) return false;
39     for(int i=(x-1)/3*3+1;i<=(x-1)/3*3+3;i++)
40         for(int j=(y-1)/3*3+1;j<=(y-1)/3*3+3;j++)
41         if(a[i][j]==k) return false;
42     return true;
43 }
44 void dfsnext(int x,int y)
45 {
46       if(y==9) dfs(x+1,1);
47         else dfs(x,y+1);
48 }
49 void dfs(int x,int y)
50 {
51     if(x==10&&y==1)
52     {
53         for(int i=1;i<=9;i++)
54         {
55             for(int j=1;j<=9;j++)
56                 {printf("%d",a[i][j]);if(j!=9) printf(" ");}
57             printf("
");
58         }
59         flag=true;
60     }
61     if(flag)return ;
62     if(a[x][y]!=0){
63        dfsnext(x,y);
64     }
65     else
66     {
67         for(int i=1;i<=9;i++)
68         {
69             if(jug(x,y,i)) {a[x][y]=i;dfsnext(x,y);a[x][y]=0;}
70         }
71     }
72 }
73 int main()
74 {
75 
76     for(int i=1;i<=9;i++)
77     {
78         for(int j=1;j<=9;j++)
79         scanf("%d",&a[i][j]);
80     }
81     dfs(1,1);
82     return 0;
83 }
View Code
原文地址:https://www.cnblogs.com/zxhl/p/4665536.html