计蒜客 数独(DFS)

蒜头君今天突然开始还念童年了,想回忆回忆童年。他记得自己小时候,有一个很火的游戏叫做数独。便开始来了一局紧张而又刺激的高阶数独。蒜头君做完发现没有正解,不知道对不对? 不知道聪明的你能否给出一个标准答案?

标准数独是由一个给与了提示数字的 9*9 网格组成,我们只需将其空格填上数字,使得每一行,每一列以及每一个3*3 宫都没有重复的数字出现。

 输入格式

一个9×9的数独,数字之间用空格隔开。*表示需要填写的数字。

输出格式

输出一个9×9的数独,把出入中的*替换成需要填写的数字即可。

本题答案不唯一,符合要求的答案均正确

样例输入

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

样例输出

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

 

这道题目类似八皇后问题,只不过八皇后是对每一行进行 1-8 的尝试,而这道题目是对每个空进行 1-9 的尝试。而且这道题目搜索到一种可行解就可以结束了。

标记方法为标记某行某个数字是否出现,标记某列某个数字是否出现,标记某个小方格某个数字是否出现。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 const double PI = acos(-1);
 17 const double eps =1e-8;
 18 #define Bug cout<<"---------------------"<<endl
 19 const int maxn=1e5+10;
 20 using namespace std;
 21 
 22 char G[11][11];
 23 int cnt[10]={9,9,9,9,9,9,9,9,9,9};//每个数字剩多少个 
 24 int flag;//是否找到答案 
 25 
 26 void PT()//输出答案 
 27 {
 28     for(int i=0;i<9;i++)
 29     {
 30         for(int j=0;j<9;j++)
 31         {
 32             if(j==0) printf("%c",G[i][j]);
 33             else printf(" %c",G[i][j]);
 34         }
 35         printf("
");
 36     }
 37     flag=1;
 38 }
 39 
 40 int judge(int x,int y,int num)//判断是否可以在G[x][y]处填入num 
 41 {
 42     for(int i=0;i<9;i++)
 43     {
 44         if(G[x][i]=='0'+num||G[i][y]=='0'+num)
 45             return 0;
 46     }
 47     for(int i=x/3*3;i<x/3*3+3;i++)
 48     {
 49         for(int j=y/3*3;j<y/3*3+3;j++)
 50         {
 51             if(G[i][j]=='0'+num)
 52                 return 0;
 53         }
 54     }
 55     return 1;
 56 }
 57 
 58 void DFS(int x,int y) //一层一层搜索 
 59 {
 60     if(x==9&&y==0)
 61     {
 62         if(flag==0) PT();
 63         return;
 64     }
 65     if(G[x][y]=='*')
 66     {
 67         for(int i=1;i<=9;i++)
 68         {
 69             if(judge(x,y,i)&&cnt[i])
 70             {
 71                 G[x][y]='0'+i; cnt[i]--;
 72                 if(y==8) DFS(x+1,0);
 73                 else DFS(x,y+1);
 74                 if(flag) return ;
 75                 G[x][y]='*'; cnt[i]++;
 76             }
 77         }
 78     }
 79     else
 80     {
 81         if(y==8) DFS(x+1,0);
 82         else DFS(x,y+1);
 83     }
 84     return ;
 85 }
 86 
 87 
 88 int main()
 89 {
 90     #ifdef DEBUG
 91     freopen("sample.txt","r",stdin);
 92     #endif
 93     ios_base::sync_with_stdio(false);
 94     cin.tie(NULL);
 95     
 96     for(int i=0;i<9;i++)
 97     {
 98         for(int j=0;j<9;j++)
 99         {
100             scanf("%c",&G[i][j]);
101             getchar();
102             if(G[i][j]>='1'&&G[i][j]<='9')
103                 cnt[G[i][j]-'0']--;
104         }
105     }
106     DFS(0,0);
107 
108     return 0;
109 }

-

原文地址:https://www.cnblogs.com/jiamian/p/12174269.html