• 深搜+回溯 POJ 2676 Sudoku


    POJ 2676 Sudoku

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17627   Accepted: 8538   Special Judge

    Description

    Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

    Input

    The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

    Output

    For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

    Sample Input

    1
    103000509
    002109400
    000704000
    300502006
    060000050
    700803004
    000401000
    009205800
    804000107

    Sample Output

    143628579
    572139468
    986754231
    391542786
    468917352
    725863914
    237481695
    619275843
    854396127
    我的思路错误之处:我本来只设置了两个二维bool数组表示每行9个数字和每列9个数字,是否用过,再深搜填写9个方格,可是发现,深搜填写每个方格是很难写的。
    正解:开是三个二维bool数组,分别表示每列每行每个大方格的数字的使用情况。
    读入时注意数字是连起来的。
     1 /*----------------正确代码-------------------------*/
     2 #include<iostream>
     3 using namespace std;
     4 #include<cstdio>
     5 #include<cstring>
     6 #define N 15
     7 char duru[N];
     8 int jz[N][N];
     9 bool flag=false,flagfg[N][N],flaghang[N][N],flaglie[N][N];
    10 inline void input()
    11 {
    12     for(int i=1;i<=9;++i)
    13     {
    14         scanf("%s",duru+1);/*注意读入的数字之间没有空格*/
    15         for(int j=1;j<=9;++j)
    16       {
    17           jz[i][j]=duru[j]-'0';
    18           if(jz[i][j]==0) continue;
    19           int k=3*((i-1)/3)+(j-1)/3+1;
    20           flagfg[k][jz[i][j]]=true;
    21           flaghang[i][jz[i][j]]=true;
    22           flaglie[j][jz[i][j]]=true;
    23       }
    24     }
    25 }
    26 void output()
    27 {
    28     for(int i=1;i<=9;++i)
    29     {
    30         for(int j=1;j<=9;++j)
    31           printf("%d",jz[i][j]);
    32         printf("
    ");
    33     }
    34 }
    35 void dfs(int x,int y)
    36 {
    37     if(x==10)
    38     {
    39         flag=true;
    40         return;
    41     }
    42     if(jz[x][y])
    43     {
    44         if(y==9)
    45            dfs(x+1,1);
    46         else dfs(x,y+1);
    47         if(flag) return; 
    48     }
    49     else {
    50         int k=3*((x-1)/3)+(y-1)/3+1;
    51         for(int i=1;i<=9;++i)
    52         {
    53             if(!flaghang[x][i]&&!flaglie[y][i]&&!flagfg[k][i])
    54             {/*枚举符合三个条件的i*/
    55                 jz[x][y]=i;
    56                 flaghang[x][i]=true;
    57                 flaglie[y][i]=true;
    58                 flagfg[k][i]=true;
    59                 if(y==9)
    60                   dfs(x+1,1);
    61                 else dfs(x,y+1);
    62                 if(flag) return;
    63                 jz[x][y]=0;/*回溯*/
    64                 flaghang[x][i]=false;
    65                 flaglie[y][i]=false;
    66                 flagfg[k][i]=false;
    67                 
    68             }
    69         }
    70     }
    71 }
    72 int main()
    73 {
    74     int T;
    75     scanf("%d",&T);
    76     while(T--)
    77     {
    78         input();
    79         dfs(1,1);
    80         output();
    81         memset(jz,0,sizeof(jz));
    82         memset(flagfg,false,sizeof(flagfg));
    83             memset(flaghang,false,sizeof(flaghang));
    84             memset(flaglie,false,sizeof(flaglie));
    85             flag=false;
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    HDU2034:人见人爱A-B
    Codeup1085: 阶乘的和
    HDU2037:今年暑假不AC
    剑指Offer(Java版)第二十七题:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
    剑指Offer(Java版)第二十六题:输入两个整数序列,第一个序列表示栈的压入顺序, 请判断第二个序列是否为该栈的弹出顺序。 假设压入栈的所有数字均不相等。 例如序列1、2、3、4、5是某栈的压栈序列, 序列5、4、3、2、1是该压栈序列对应的一个弹出序列, 但4、3、5、1、2就不可能是该压栈序列的弹出序列。
    剑指Offer(Java版)第二十五题:包含min函数的栈
    剑指Offer(Java版)第二十四题:顺时针打印矩阵
    MongoDB学习笔记10——分片
    MongoDB学习笔记9——复制
    MongoDB学习笔记8——优化
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5578584.html
Copyright © 2020-2023  润新知