2015南阳CCPC H - Sudoku 暴力
2015-10-21 22:46
465 查看
[b]H - Sudoku[/b]
Time Limit: 1 Sec
Memory Limit: 256 MB
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!!!
It's guaranteed that there will be exactly one way to recover the board.
Sample Output
让你找到一个4*4的数独的合法解
题解:
直接爆搜就能过
代码:
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
无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(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.
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
HINT
题意让你找到一个4*4的数独的合法解
题解:
直接爆搜就能过
代码:
#include<stdio.h> #include<iostream> #include<math.h> using namespace std; string s[5]; int p[5][5]; int tx[20]; int ty[20]; int tot = 0; int flag; int vis[5]; int check() { for(int i=0;i<4;i++) { vis[1]=vis[2]=vis[3]=vis[4]=0; for(int j=0;j<4;j++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } for(int j=0;j<4;j++) { vis[1]=vis[2]=vis[3]=vis[4]=0; for(int i=0;i<4;i++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } vis[1]=vis[2]=vis[3]=vis[4]=0; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } vis[1]=vis[2]=vis[3]=vis[4]=0; for(int i=2;i<4;i++) { for(int j=0;j<2;j++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } vis[1]=vis[2]=vis[3]=vis[4]=0; for(int i=0;i<2;i++) { for(int j=2;j<4;j++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } vis[1]=vis[2]=vis[3]=vis[4]=0; for(int i=2;i<4;i++) { for(int j=2;j<4;j++) { if(p[i][j]==0)continue; if(vis[p[i][j]])return 0; vis[p[i][j]]=1; } } return 1; } void dfs(int x) { if(flag)return; if(x==tot){ for(int i=0;i<4;i++) { for(int j=0;j<4;j++) printf("%d",p[i][j]); printf("\n"); } flag=1; return;} for(int i=1;i<=4;i++) { p[tx[x]][ty[x]]=i; if(check()) dfs(x+1); p[tx[x]][ty[x]]=0; } } int main() { int t;scanf("%d",&t); for(int cas = 1;cas <= t;cas++) { tot = 0; flag = 0; for(int i=0;i<4;i++) cin>>s[i]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(s[i][j]=='*') p[i][j]=0; else p[i][j]=s[i][j]-'0'; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(p[i][j]==0) { tx[tot]=i; ty[tot]=j; tot++; } printf("Case #%d:\n",cas); dfs(0); } }
相关文章推荐
- c语言/c++常见面试题
- Linux - 磁盘分区
- SqlServer+Json+S2SH+Android实现访问数据库
- 设计模式总结(经典)
- ListView多项选择功能的实现
- 51nod 1163:最高的奖励 优先队列
- 2015南阳CCPC G - Ancient Go 暴力
- Redis部署管理持久化迁移
- 51nod 1163:最高的奖励 优先队列
- Java开源的支持xpath的html解析器介绍--JsoupXpath
- Net-SNMP动态库方式扩展MIB
- ORA-02020的处理,dblink超出数量的设置
- C++中指针和引用的区别(转)
- python 发送邮件,未完
- 杭电acm2029-Palindromes _easy version
- STL学习笔记-quene容器(队列)
- C语言,内存管理
- 边缘检测
- 两步让你的mobile traffic通过fiddler代理传送
- ORA-01033: ORACLE initialization or shutdown in progress数据文件损坏错误解决方案