zoukankan      html  css  js  c++  java
  • POJ-2676 Sudoku (DFS)

    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



    题目分析:数独题。就这道数独题而言仅仅DFS是可以AC的。先建立一棵搜索树,然后就搜!


    代码如下:
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 # include<algorithm>
     5 using namespace std;
     6 char p[12][12];
     7 int cnt,a[12][12];
     8 struct Position
     9 {
    10     int x,y;
    11     Position(){}
    12     Position(int a,int b):x(a),y(b){}
    13     bool operator < (const Position &a) const {
    14         if(x==a.x)
    15             return y<a.y;
    16         return x<a.x;
    17     }
    18 };
    19 bool yy;
    20 Position pos[100];
    21 bool ok(int x,int y,int m)
    22 {
    23     for(int k=1;k<=9;++k){
    24         if(a[x][k]==m||a[k][y]==m)
    25             return false;
    26     }
    27     int it=x,jt=y;
    28     while(it%3!=1)
    29         --it;
    30     while(jt%3!=1)
    31         --jt;
    32     for(int i=it;i<=it+2;++i)
    33         for(int j=jt;j<=jt+2;++j)
    34             if(a[i][j]==m)
    35                 return false;
    36     return true;
    37 }
    38 void dfs(int p)
    39 {
    40     if(yy)
    41         return ;
    42     if(p==cnt){
    43         for(int i=1;i<=9;++i){
    44             for(int j=1;j<=9;++j)
    45                 printf("%d",a[i][j]);
    46             printf("
    ");
    47         }
    48         yy=true;
    49         return ;
    50     }
    51     int x=pos[p].x,y=pos[p].y;
    52     for(int i=1;i<=9;++i){
    53         if(ok(x,y,i)){
    54             a[x][y]=i;
    55             dfs(p+1);
    56             a[x][y]=0;  ///注意要将这个位置重新置为0,否则影响接下来的搜索过程。
    57         }
    58         if(yy)
    59             return ;
    60     }
    61 }
    62 int main()
    63 {
    64     int T;
    65     scanf("%d",&T);
    66     while(T--)
    67     {
    68         cnt=0;
    69         for(int i=1;i<=9;++i){
    70             scanf("%s",p[i]+1);
    71             for(int j=1;j<=9;++j){
    72                 a[i][j]=p[i][j]-'0';
    73                 if(a[i][j]==0){
    74                     pos[cnt++]=Position(i,j);
    75                 }
    76             }
    77         }
    78         sort(pos,pos+cnt);
    79         yy=false;
    80         dfs(0);
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    面试之求找两个数和为某个数、几个连续数等于某个数
    oracle 创建字段自增长——两种实现方式汇总
    UnityTestTools測试工具
    现代控制理论课件资料与说明
    教你轻松自己定义ViewPagerIndicator
    【Android】自己定义ListView的Adapter报空指针异常解决方法
    SICP-Exercise 1.5
    网络编程----------SOCKET编程实现简单的TCP协议
    字符串翻转
    机房收费系统重构(六)—泛型集合
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4726169.html
Copyright © 2011-2022 走看看