Sudoku(模拟)
2017-04-26 20:14
120 查看
Sudoku
[align=center]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2223 Accepted Submission(s): 756
[/align]
[align=left]Problem Description[/align]
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!!!
[align=left]Input[/align]
The first line of the input gives the number of test cases,
T(1≤T≤100).
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
题解:
大意:
给你一个4*4的矩阵,其中有一些 * 号,让你在 * 号上填数字1~4,使这个矩阵符合每行每列和四个
2*2的(左上,左下,右上,右下)小矩阵都含有1~4……只有唯一解
思路:
由于只有为一解,所以一定存在 在符合三个条件之后只有一个数字符合条件,故找到它,并更新它在
矩阵中的值,然后继续寻找,直到更新完毕……
代码如下
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<stdlib.h> #include<time.h> #include<string> #include<math.h> #include<map> #include<queue> #include<stack> #define INF 0x3f3f3f3f #define ll long long #define For(i,a,b) for(int i=a;i<b;i++) #define sf(a) scanf("%d",&a) #define sfs(a) scanf("%s",a) #defi 4000 ne sff(a,b) scanf("%d%d",&a,&b) #define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c) #define pf(a) printf("%d\n",a) #define P() printf("\n") #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; char w[5][5]; // 存图 int row[5],col[5],x[5]; // 行,列,矩阵(标记) void change(int a,int b) { mem(row,0); mem(col,0); mem(x,0); for(int i=0; i<4; i++) // 标记行和列出现过的数字 { if(w[a][i]!='*') row[w[a][i]-'0']=1; if(w[i][b]!='*') col[w[i][b]-'0']=1; } if(a<2) // 矩阵标记 { if(b<2) // 左上 { for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { if(w[i][j]!='*') { x[w[i][j]-'0']=1; } } } } // 右上 else { for(int i=0; i<2; i++) { for(int j=2; j<4; j++) { if(w[i][j]!='*') { x[w[i][j]-'0']=1; } } } } } else { if(b<2) // 左下 { for(int i=2; i<4; i++) { for(int j=0; j<2; j++) { if(w[i][j]!='*') { x[w[i][j]-'0']=1; } } } } else // 右下 { for(int i=2; i<4; i++) { for(int j=2; j<4; j++) { if(w[i][j]!='*') { x[w[i][j]-'0']=1; } } } } } int flag=0,aa; for(int i=1; i<=4; i++) // 判断此时的位置,符合三个条件的数字是否唯一 { if(row[i]==0&&col[i]==0&&x[i]==0) { flag++; aa=i; } } if(flag==1)w[a][b]=aa+'0'; // 唯一,更新矩阵 } int main() { int t,s=0; sf(t); while(t--) { s++; For(i,0,4)sfs(w[i]); int flag; while(1) { flag=1; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(w[i][j]=='*') // 遍历 { flag=0; change(i,j); } } } if(flag)break; } printf("Case #%d:\n",s); for(int i=0; i<4; i++) printf("%s\n",w[i]); } }
相关文章推荐
- ACdream 1195 Sudoku Checker(模拟)
- HDU 5547 Sudoku(dfs+模拟+状态判断)
- [NOIP2017模拟]最佳序列
- 如何简单模拟Web服务
- c#第一篇 在WPF的window窗体中使用httpwebrequest实现模拟登陆网页,并在webbroser控件中显示
- PAT-A1006 Sign In and Sign Out(25)(模拟)
- 通过代码动态实现和模拟Android手机上的back键
- NOIP2017模拟 玩游戏 最小生成树 树上倍增
- 真实的模拟***综合实验
- hdu 4112 Break the Chocolate(模拟)
- Codeforces--629B--Far Relative’s Problem(模拟)
- Python爬虫实战之爬取链家广州房价_04链家的模拟登录(记录)
- 基础系列(1)-傻瓜模拟【寻宝】(基础+)
- 使用MFC 发送消息模拟按钮按下事件
- DM642 模拟SPI通信
- python利用dict模拟switch
- 手工数据结构系列-C语言模拟队列和栈 hdu1702
- Gym - 101177A Anticlockwise Motion 模拟
- 用dll实现类的动态创建模拟实例
- TI单片机及模拟技术教育者全国巡回讲座游记