Sudoku

Sudoku


Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. 

Actually, Yi Sima was playing it different. First of all, he tried to generate a board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four pieces, every piece contains 1 to 4. 

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover. 

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
Input   The first line of the input gives the number of test cases,  test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima. 

It's guaranteed that there will be exactly one way to recover the board.   Output   For each test case, output one line containing  Case #x: , where  is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.   Sample Input
3
****
2341
4123
3214
*243
*312
*421
*134
*41*
**3*
2*41
4*2*
Sample Output
Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123

题意:每行每列都有1234,划分成4个2*2的小块,每个小块也有1234

思路:搜索+回溯

AC代码:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. #include<algorithm>  
  6. #include<cmath>  
  7. #include<utility>  
  8. #include<set>  
  9. #include<vector>  
  10. #include<map>  
  11. #include<queue>  
  12. #include<stack>  
  13. #define maxn 1010  
  14. #define INF 0x3f3f3f3f  
  15. #define LL long long  
  16. #define ULL unsigned long long  
  17. #define E 1e-8  
  18. #define mod 1000000007  
  19. #define P pair<int,int>  
  20. using namespace std;  
  21.   
  22. struct node  
  23. {  
  24.     int x,y;  
  25. }f[20];  
  26. int t;  
  27. char a[10][10];  
  28. int ans;  
  29. bool flag;  
  30. map<char,int>m;  
  31.   
  32. /*bool check() 
  33. { 
  34.     for(int i=0;i<4;i++){ 
  35.         m.clear(); 
  36.         for(int j=0;j<4;j++){ 
  37.             if(m[a[i][j]]){ 
  38.                 return false; 
  39.             } 
  40.             m[a[i][j]]++; 
  41.         } 
  42.     } 
  43.     for(int i=0;i<4;i++){ 
  44.         m.clear(); 
  45.         for(int j=0;j<4;j++){ 
  46.             if(m[a[j][i]]){ 
  47.                 return false; 
  48.             } 
  49.             m[a[j][i]]++; 
  50.         } 
  51.     } 
  52.     for(int k=0;k<4;k++){ 
  53.         m.clear(); 
  54.         for(int i=(k/2)*2;i<(k/2)*2+2;i++){ 
  55.             for(int j=(k%2)*2;j<(k%2)*2+2;j++){ 
  56.                 if(m[a[i][j]]){ 
  57.                     return false; 
  58.                 } 
  59.                 m[a[i][j]]++; 
  60.             } 
  61.         } 
  62.     } 
  63. }*/  
  64.   
  65. bool check(int x,int y,char s)  
  66. {  
  67.     for(int i=0;i<4;i++){  
  68.         if(a[x][i]==s) return false;  
  69.         if(a[i][y]==s) return false;  
  70.     }  
  71.     for(int i=(x/2)*2;i<(x/2)*2+2;i++){  
  72.         for(int j=(y/2)*2;j<(y/2)*2+2;j++){  
  73.             if(a[i][j]==s) return false;  
  74.         }  
  75.     }  
  76.     return true;  
  77. }  
  78.   
  79. void dfs(int step)  
  80. {  
  81.     if(step==ans){  
  82.         for(int i=0;i<4;i++){  
  83.             for(int j=0;j<4;j++){  
  84.                 printf("%c",a[i][j]);  
  85.             }  
  86.             printf("\n");  
  87.         }  
  88.         flag=true;  
  89.         return;  
  90.     }  
  91.     int x=f[step].x;  
  92.     int y=f[step].y;  
  93.     for(int i=1;i<=4&&!flag;i++){  
  94.         char ss='0'+i;  
  95.         if(check(x,y,ss)){  
  96.             a[x][y]=ss;  
  97.             //cout<<a[x][y]<<endl;  
  98.             dfs(step+1);  
  99.             a[x][y]='*';  
  100.         }  
  101.     }  
  102. }  
  103.   
  104. int main()  
  105. {  
  106.     scanf("%d",&t);  
  107.     getchar();  
  108.     int cas=0;  
  109.     while(t--){  
  110.         //    getchar();  
  111.         ans=0;  
  112.         for(int i=0;i<4;i++){  
  113.             scanf("%s",a[i]);  
  114.             for(int j=0;j<4;j++){  
  115.                 if(a[i][j]=='*'){  
  116.                     f[ans].x=i;  
  117.                     f[ans++].y=j;  
  118.                 }  
  119.             }  
  120.         }  
  121.         flag=false;  
  122.         printf("Case #%d:\n",++cas);  
  123.         dfs(0);  
  124.     }  
  125.     return 0;  
  126. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值