HDOJ5547 SudoKu

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547

题目大意:填数独。。。

思路:爆搜

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 bool row[5][5],col[5][5],siz[5][5];
 7 int G[5][5];
 8 bool flag;
 9 int Num(int x,int y){
10     if(x<=2&&y<=2) return 1;
11     if(x<=2&&y>2) return 2;
12     if(x>2&&y<=2) return 3;
13     if(x>2&&y>2) return 4;
14 }
15 void dfs(int x,int y){
16     if(y>4&&x==4){
17         flag=true;
18         return ;
19     }
20     if(flag) return ;
21     if(y>4) x++,y=1;
22     if(G[x][y]) {
23         dfs(x,y+1);
24         return;
25     }
26     if(flag) return ;
27     for(int i=1;i<=4;i++){
28         if(!row[x][i]&&!col[y][i]&&!siz[Num(x,y)][i]){
29             G[x][y]=i;
30             row[x][i]=col[y][i]=siz[Num(x,y)][i]=true;
31             dfs(x,y+1);
32             if(flag) return ;
33             G[x][y]=0;
34             row[x][i]=col[y][i]=siz[Num(x,y)][i]=false;
35         }
36     }
37 }
38 void init(){
39     flag=false;
40     memset(row,false,sizeof(row));
41     memset(col,false,sizeof(col));
42     memset(siz,false,sizeof(siz));
43 }
44 void solve(int T){
45     printf("Case #%d:
",T);
46     init();
47     for(int x=1;x<=4;x++){
48         for(int y=1;y<=4;y++){
49             char tmp;
50             scanf(" %c",&tmp);
51             if(tmp=='*'){
52                 G[x][y]=0;
53             }
54             else {
55                 G[x][y]=tmp-'0';
56                 int now=tmp-'0';
57                 row[x][now]=col[y][now]=siz[Num(x,y)][now]=true;
58             }
59         }
60     }
61     dfs(1,1);
62     for(int i=1;i<=4;i++){
63         for(int j=1;j<=4;j++){
64             printf("%d",G[i][j]);
65         }
66         printf("
");
67     }
68 }
69 int main(){
70     int T;
71     //freopen("C:\Users\acm\Desktop\ACM\out.txt","w",stdout);
72     scanf("%d",&T);
73     for(int i=1;i<=T;i++) solve(i);
74 }
原文地址:https://www.cnblogs.com/as3asddd/p/6071887.html