HDOJ 5547 Sudoku (qwb铜牌题 DFS搜索)

Sudoku

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1635    Accepted Submission(s): 574


Problem Description
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  4×4  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  2×2  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,  T(1T100) T  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  x  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
 

Source

The 2015 China Collegiate Programming Contest 


思路:

给定一个4*4矩阵,让你填数,确保每行、每列、每1/4区域(就是左上角左下角右上角右下角)都没有重复的元素。因为每个区域只有4个位置,只有4个数,所以每个位置方法肯定是只有一个。直接用dfs搜索,对于每个*点试探放进去1~4这四个数,这里用到的一个技巧是dfs函数传入的是一个数,而不是坐标,因为由这个数是多少就能确定点的坐标,这样更好想也好做一点。。。 

代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>    
  2. #include<cstdio>    
  3. #include<cstring>    
  4. #include<cmath>    
  5. #include<algorithm>      
  6. using namespace std;    
  7.     
  8. char maps[110][110];    
  9.     
  10. int judge(int row,int col)     
  11. {    
  12.     int i,j;    
  13.     for(i=0;i<4;i++)   //同行   
  14.     {    
  15.         if( i!=col && maps[row][i]==maps[row][col])    
  16.             return 0;    
  17.     }    
  18.     for(i=0;i<4;i++)  ///同列    
  19.     {    
  20.         if( i!=row && maps[i][col]==maps[row][col])    
  21.             return 0;    
  22.     }    
  23.     //1/4区域  分成4块分别求   
  24.     if(row<2)    
  25.     {    
  26.         if(col<2)    
  27.         {    
  28.             for(i=0;i<2;i++)    
  29.             {    
  30.                 for(j=0;j<2;j++)    
  31.                     if(row!=i && col!=j && maps[i][j]==maps[row][col])    
  32.                     return 0;    
  33.             }    
  34.         }    
  35.         else    
  36.         {    
  37.             for(i=0;i<2;i++)    
  38.             {    
  39.                 for(j=2;j<4;j++)    
  40.                     if(row!=i && col!=j && maps[i][j]==maps[row][col])    
  41.                     return 0;    
  42.             }    
  43.         }    
  44.     }    
  45.     else  
  46.     {    
  47.         if(col<2)    
  48.         {    
  49.             for(i=2;i<4;i++)    
  50.             {    
  51.                 for(j=0;j<2;j++)    
  52.                     if(row!=i && col!=j && maps[i][j]==maps[row][col])    
  53.                     return 0;    
  54.             }    
  55.         }    
  56.         else    
  57.         {    
  58.             for(i=2;i<4;i++)    
  59.             {    
  60.                 for(j=2;j<4;j++)    
  61.                     if(row!=i && col!=j && maps[i][j]==maps[row][col])    
  62.                     return 0;    
  63.             }    
  64.         }    
  65.     }    
  66.     
  67.     return 1;    
  68. }    
  69.     
  70.     
  71. void dfs(int cur)   
  72. {    
  73.     int i,j;    
  74.     if(cur==16)     
  75.     {    
  76.         for(i=0;i<4;i++)    
  77.         {    
  78.             for(j=0;j<4;j++)    
  79.                 printf("%c",maps[i][j]);    
  80.             printf("\n");    
  81.         }    
  82.       return ;    
  83.     }    
  84.     
  85.     int row=cur/4;      
  86.     int col=cur%4;    
  87.     if(maps[row][col]=='*')    
  88.     {    
  89.         for(i=1;i<=4;i++)     
  90.         {    
  91.             maps[row][col]=i+'0';    
  92.             if(judge(row,col)==1)     
  93.             {    
  94.                 dfs(cur+1);        
  95.             }    
  96.             maps[row][col]='*';        
  97.         }    
  98.     
  99.     }    
  100.     else    
  101.        dfs(cur+1);      
  102. }    
  103.     
  104. int main()    
  105. {    
  106.     int T,cas=1,i;    
  107.     scanf("%d",&T);    
  108.     while(T--)    
  109.     {    
  110.         for(i=0;i<4;i++)    
  111.             scanf("%s",maps[i]);    
  112.         printf("Case #%d:\n",cas++);    
  113.         dfs(0);      
  114.     }    
  115.     return 0;    
  116. }    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值