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
  • 相关阅读:
    利用GetInvalidFileNameChars()得到有效的文件名
    C# 下载远程http文件到本地
    CLR无法从COM 上下文*****转换为COM上下文*****,这种状态已持续60秒。
    Wpf UserControl使用 KeyBinding,失效问题
    C# windows服务知识集锦
    制作Windows服务和安装程序(C#版)
    C语言内存管理
    Python初学注意问题
    msp430学习笔记-USART
    msp430学习笔记-ADC12
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4726169.html
Copyright © 2011-2022 走看看