• 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
    ",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("
    ");
    86             p++;
    87             over=false;
    88             dfs(1);
    89             i=j=1;
    90             tot=1;
    91         }
    92     }
    93     return 0;
    94 }
    我的代码
  • 相关阅读:
    word2vec模型评估方案
    分词问题整理和发现
    11.1第一次相似度算法测试结果
    如何使用向量代表文档doc或者句子sentence
    fasttext学习笔记
    传统变量抽样
    统计抽样与非统计抽样
    误受风险和误拒风险
    企业所得税怎么算
    进一步审计程序
  • 原文地址:https://www.cnblogs.com/-maybe/p/4392147.html
Copyright © 2020-2023  润新知