HDU--2780

数独问题,深搜即可。不过题目可能给出的是不合数独要求的数据,所以要提前判断。

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 
  5 using namespace std;
  6 
  7 bool row[10][10];
  8 bool col[10][10];
  9 bool grid[10][10];
 10 int map[10][10];
 11 
 12 bool judge(int x,int y,int val)
 13 {
 14     for(int i=1;i<=9;i++)
 15         if(i!=x && map[i][y]==val)
 16             return true;
 17     for(int i=1;i<=9;i++)
 18         if(i!=y && map[x][i]==val)
 19             return true;
 20     for(int i=1;i<=9;i++)
 21     {
 22         for(int j=1;j<=9;j++)
 23         {
 24             int k=3*((i-1)/3)+(j-1)/3+1;
 25             int tar=3*((x-1)/3)+(y-1)/3+1;
 26             if(k==tar)
 27             {
 28                 if(i!=x && j!=y && map[i][j]==val)
 29                     return true;
 30             }
 31         }
 32     }
 33     return false;
 34 }
 35 
 36 bool JUDGE()
 37 {
 38     for(int i=1;i<=9;i++)
 39     {
 40         for(int j=1;j<=9;j++)
 41         {
 42             if(map[i][j]!=0)
 43                 if(judge(i,j,map[i][j]))
 44                     return true;
 45         }
 46     }
 47     return false;
 48 }
 49 
 50 bool dfs(int x,int y)
 51 {
 52     if(x==10)
 53         return true;
 54     bool flag=false;
 55     if(map[x][y])
 56     {
 57         if(y==9)
 58             flag=dfs(x+1,1);
 59         else
 60             flag=dfs(x,y+1);
 61         if(flag)
 62             return true;
 63         else
 64             return false;
 65     }
 66     else
 67     {
 68         int k=3*((x-1)/3)+(y-1)/3+1;
 69         for(int i=1;i<=9;i++)
 70         {
 71             if(!row[x][i] && !col[y][i] && !grid[k][i])
 72             {
 73                 map[x][y]=i;
 74                 row[x][i]=true;
 75                 col[y][i]=true;
 76                 grid[k][i]=true;
 77                 if(y==9)
 78                     flag=dfs(x+1,1);
 79                 else
 80                     flag=dfs(x,y+1);
 81                 if(!flag)
 82                 {
 83                     map[x][y]=0;
 84                     row[x][i]=false;
 85                     col[y][i]=false;
 86                     grid[k][i]=false;
 87                 }
 88                 else
 89                     return true;
 90             }
 91         }
 92     }
 93     return false;
 94 }
 95 
 96 int main()
 97 {
 98     int T;
 99     cin>>T;
100     for(int t=1;t<=T;t++)
101     {
102         memset(row,0,sizeof(row));
103         memset(col,0,sizeof(col));
104         memset(grid,0,sizeof(grid));
105         char MAP[10][10];
106         for(int i=1;i<=9;i++)
107         {
108             for(int j=1;j<=9;j++)
109             {
110                 cin>>MAP[i][j];
111                 map[i][j]=MAP[i][j]-'0';
112                 row[i][ map[i][j] ]=true;
113                 col[j][ map[i][j] ]=true;
114                 int k=3*((i-1)/3)+(j-1)/3+1;
115                 grid[k][ map[i][j] ]=true;
116             }
117         }
118         if(JUDGE())
119             printf("Could not complete this grid.\n");
120         else
121         {
122             int ans=dfs(1,1);
123             if(ans)
124             {
125                 for(int i=1;i<=9;i++)
126                 {
127                     for(int j=1;j<=9;j++)
128                     {
129                         printf("%d",map[i][j]);
130                     }
131                     printf("\n");
132                 }
133             }
134             else
135                 printf("Could not complete this grid.\n");
136         }
137         if(t!=T)
138             printf("\n");
139     }
140     return 0;
141 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值