HDU5547-Sudoku

来源:互联网 发布:数据库能干什么 编辑:程序博客网 时间:2024/06/11 02:22

Sudoku

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


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****234141233214*243*312*421*134*41***3*2*414*2*
 

Sample Output
Case #1:1432234141233214Case #2:1243431234212134Case #3:3412123423414123
 

Source
The 2015 China Collegiate Programming Contest
 

Recommend
wange2014
 

题意:给你一个4*4的方阵,里面包含的字符是1到4,还有*,然后要求把所有的*都替换成1到4,要求是每个上下左右四个2*2的小方阵里面1到4都要有,每一行每一列的数字不能相同

解题思路:记录下所有‘*’的位置,然后爆搜


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst LL INF = 0x3f3f3f3f3f3f3f3f;const double pi = acos(-1.0);const double eps = 1e-8;char ch[10][10];struct node{int x, y, ans;} a[20];int cnt, flag;bool check(int x, int y,int k){for (int i = 0; i<4; i++){if (ch[x][i] == '0'+k||ch[i][y] == '0'+k) return false;}int xx = x / 2 * 2, yy = y / 2 * 2;for (int i = xx; i <= xx + 1; i++)for (int j = yy; j <= yy + 1; j++)if (ch[i][j] == '0'+k) return false;return true;}void dfs(int k){if (k == cnt){flag = 1;return;}for (int i = 1; i <= 4; i++){if (check(a[k].x, a[k].y, i)){ch[a[k].x][a[k].y] = i + '0';dfs(k + 1);if (flag) return;ch[a[k].x][a[k].y] = '*';}}}int main(){int t, cas = 0;scanf("%d", &t);while (t--){printf("Case #%d:\n", ++cas);cnt = 0;for (int i = 0; i<4; i++){scanf("%s", ch[i]);for (int j = 0; j<4; j++){if (ch[i][j] == '*')a[cnt].x = i, a[cnt++].y = j;}}flag = 0;dfs(0);for (int i = 0; i<4; i++) puts(ch[i]);}return 0;}

0 0
原创粉丝点击