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. }    
全部评论

相关推荐

大愣子衰哥:老哥,是正式还是实习
点赞 评论 收藏
分享
求好运眷顾🙏🏻:翻译:面试前没盘点好hc一下面太多了,现在在排序回去等通知
点赞 评论 收藏
分享
05-12 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务