zoukankan      html  css  js  c++  java
  • 深搜+回溯 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 }
  • 相关阅读:
    centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs,base64,jq 第十六节课
    centos Linux系统日常管理2 tcpdump,tshark,selinux,strings命令, iptables ,crontab,TCP,UDP,ICMP,FTP网络知识 第十五节课
    一个兼职DBA的数据库运维经验 小米科技 xx@xiaomi.com 2011
    centos Linux系统日常管理1 cpuinfo cpu核数 命令 w, vmstat, uptime ,top ,kill ,ps ,free,netstat ,sar, ulimit ,lsof ,pidof 第十四节课
    HTML5矢量实现文件上传进度条
    基于HTML5的WebGL呈现A星算法3D可视化
    基于HTML5的WebGL呈现A星算法的3D可视化
    基于HTML5的WebGL设计汉诺塔3D游戏
    基于HTML5树组件延迟加载技术实现
    基于HTML5的WebGL电信网管3D机房监控应用
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5578584.html
Copyright © 2011-2022 走看看