H - Sudoku (dfs暴力)
来源:互联网 发布:2017淘宝发货时间规定 编辑:程序博客网 时间:2024/05/29 15:15
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 to , every column contains to . Also he made sure that if we cut the board into four pieces, every piece contains to .
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!!!
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 lines. Each line consist of 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.
For each test case, output one line containing Case #x:
, where is the test case number (starting from ). Then output lines with characters each. indicate the recovered board.
3
2341 4123 3214
243 312 421 134
41 *3 241 42*
Case #1: 1432 2341 4123 3214 Case #2: 1243 4312 3421 2134 Case #3: 3412 1234 2341 4123
#include<stdio.h>#include<string.h>char s[10];int a[10][10];int flag;int judge( int x,int y,int k){ for(int i=0; i<4; i++) { if(a[x][i] == k) return 0; if(a[i][y] == k) return 0; } int nx = x / 2; int ny = y / 2; for( int i = nx*2;i<2*nx+2;i++) { for(int j = ny*2;j<2*ny+2; j++) { if(a[i][j] == k) return 0; } } return 1;}void dfs(int n){ if(flag) return; if(n >= 16) { flag = 1; return; } else { int nx = (n)/4; int ny = n%4; for(int i = 1; i<=4; i++) { if( !a[nx][ny]) { if(judge(nx,ny,i)) { a[nx][ny] = i; dfs(n+1); if(flag) return; a[nx][ny] = 0; } } else { dfs(n+1); } } }}int main(){ int t,n,m,i,j; scanf("%d", &t); int cc = 1; while(t--) { memset(a, 0, sizeof(a)); for(i=0; i < 4; i++) { scanf("%s", s); for(j=0;j<4;j++) { if(s[j] == '*') a[i][j] = 0; else a[i][j] = s[j] - '0'; } } flag = 0; dfs(0); printf("Case #%d:\n",cc++); for( i = 0; i<4; i++) { for(j=0;j<3; j++) printf("%d",a[i][j]); printf("%d\n", a[i][j]); } } return 0;}
- H - Sudoku (dfs暴力)
- H - Sudoku【ccpc dfs】
- hdu5547 Sudoku (暴力DFS)
- HDU1426 Sudoku Killer(DFS暴力)
- hdu 5547 Sudoku dfs 暴力回溯 解题报告
- HDU 1426 Sudoku Killer ( 暴力dfs 输入输出格式坑)
- Sudoku(DFS)
- Sudoku--DFS
- Sudoku(DFS)
- CDOJ 1222 Sudoku【暴力】
- 【HDU5547 2015 CCPC 南阳国赛H】【DFS】Sudoku 4x4棋盘的填充
- PKU 2676 (Sudoku) DFS
- POJ 2676 Sudoku dfs
- 【DFS】ZOJ 1580 Sudoku
- POJ2676:Sudoku(DFS)
- ZOJ 2580 Sudoku (DFS)
- hdu1426 Sudoku Killer dfs
- poj 2676 sudoku(DFS)
- 使用ssh公钥实现免密码登录
- iOS与js 的简单交互
- css单行、多行文字溢出显示省略号
- 08 - Docker常用命令(Docker系列)
- Android代码中来记忆23种设计模式
- H - Sudoku (dfs暴力)
- JS字符串中提取数字
- 09 - docker run 之 Wordpress+mysql快速搭建网站(Docker系列)
- Codeforces#383D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
- spring MVC利用注解直接返回Json
- Android自定义圆形渐变进度条
- mongodb之索引
- 生成固定位数验证码的工具类
- iOS从零基础到精通就业-C语言入门 2 算数运算符