POJ 2676 数独(DFS)

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21612   Accepted: 10274   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

Source

题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字,填完数独。

分析:直接搜索,标记行、列、块,值得一提的是倒着搜比正着搜效率高出许多,这也算是一个技巧。

 代码:

  1 ////#include "bits/stdc++.h"
  2 #include "cstdio"
  3 #include "map"
  4 #include "set"
  5 #include "cmath"
  6 #include "queue"
  7 #include "vector"
  8 #include "string"
  9 #include "cstring"
 10 #include "time.h"
 11 #include "iostream"
 12 #include "stdlib.h"
 13 #include "algorithm"
 14 #define db double
 15 #define ll long long
 16 #define vec vector<ll>
 17 #define Mt  vector<vec>
 18 #define ci(x) scanf("%d",&x)
 19 #define cd(x) scanf("%lf",&x)
 20 #define cl(x) scanf("%lld",&x)
 21 #define pi(x) printf("%d
",x)
 22 #define pd(x) printf("%f
",x)
 23 #define pl(x) printf("%lld
",x)
 24 #define rep(i, x, y) for(int i=x;i<=y;i++)
 25 const int N   = 1e6 + 5;
 26 const int mod = 1e9 + 7;
 27 const int MOD = mod - 1;
 28 const db  eps = 1e-18;
 29 const db  PI  = acos(-1.0);
 30 using namespace std;
 31 int t;
 32 
 33 int R()
 34 {
 35     int x=0,f=1;char ch=getchar();
 36     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 37     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 38     return x*f;
 39 }
 40 char s[10][10];
 41 int  a[10][10];
 42 bool ok(int ans,int x,int y)
 43 {
 44     for(int i=0;i<9;i++)
 45         if(a[i][y]==ans) return 0;
 46     for(int i=0;i<9;i++)
 47         if(a[x][i]==ans) return 0;
 48     int xx=x-x%3,yy=y-y%3;
 49     for(int i=xx;i<xx+3;i++)
 50         for(int j=yy;j<yy+3;j++)
 51             if(a[i][j]==ans) return 0;
 52     return 1;
 53 }
 54 bool okk=0;
 55 void dfs(int x,int y,int cnt)
 56 {
 57     if(cnt==81){
 58         okk=1;//满足条件后立刻结束,并标记
 59         return;
 60     }
 61     while(a[x][y]){
 62         if(y==8) x++,y=0;
 63         else y++;
 64         if(x==9) {okk=1;return;}//满足条件后立刻结束,并标记
 65     }
 66     for(int i=1;i<=9;i++){
 67         if(ok(i,x,y)){
 68             a[x][y]=i;
 69             if(y==8) dfs(x+1,0,cnt+1);
 70             else     dfs(x,y+1,cnt+1);
 71             if(okk) return;//满足条件后立刻结束
 72             a[x][y]=0;
 73         }
 74     }
 75     return;
 76 }
 77 int main()
 78 {
 79     t=R();
 80     while(t--)
 81     {
 82         int cnt=81;
 83         memset(a,0, sizeof(a));
 84         memset(s,0, sizeof(s));
 85         okk=0;
 86         for(int i=0;i<9;i++)
 87         {
 88             scanf("%s",s[i]);
 89             for(int j=0;j<9;j++){
 90                 a[i][j]=s[i][j]-'0';
 91                 if(!a[i][j]) cnt--;
 92             }
 93         }
 94         dfs(0,0,cnt);//输出即为满足条件的,结束后的情况。
 95         for(int i=0;i<9;i++){
 96             for(int j=0;j<9;j++){
 97                 printf("%d",a[i][j]);
 98             }
 99             puts("");
100         }
101     }
102 }
 
原文地址:https://www.cnblogs.com/mj-liylho/p/8007498.html