The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

Sudoku

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


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
题意:数独。
分析:暴力
 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 using namespace std;
 6 bool row[11][11];
 7 bool col[11][11];
 8 bool squ[11][11];
 9 int a[11][11];
10 int ans[11][11];
11 
12 int pos(int x, int y) {
13     return (x/3) * 2 + (y/3);
14 }
15 bool flag;
16 void dfs(int x, int y) {
17     if (flag) return;
18     if (y > 4) y = 1, x++;
19     if (x > 4) {
20         for (int i = 1; i <= 4; i++)
21             for (int j = 1; j <= 4; j++) ans[i][j] = a[i][j];
22         flag = true;
23         return;
24     }
25     if (a[x][y] != 0) {
26         dfs(x, y+1);
27 return;
28     }
29     for (int i = 1; i <= 4; i++) {
30         if (row[x][i]) continue;
31         if (col[y][i]) continue;
32         int z = pos(x, y);
33         if (squ[z][i]) continue;
34         row[x][i] = true;
35         col[y][i] = true;
36         squ[z][i] = true;
37         a[x][y] = i;
38         dfs(x, y+1);
39         a[x][y] = 0;
40         row[x][i] = false;
41         col[y][i] = false;
42         squ[z][i] = false;
43     }
44 }
45 char s[110];
46 int main() {
47     //freopen("h.in", "r",stdin);
48     int T, cas = 0;
49     scanf("%d", &T);
50     while (T--) {
51         printf("Case #%d:\n", ++cas);
52         for (int i = 1; i <= 4; i++) {
53             scanf("%s", s+1);
54             for (int j = 1; j <= 4; j++) {
55                 if (s[j] == '*') a[i][j] = 0;
56                 else a[i][j] = s[j] - '0';
57             }
58         }
59         memset(col,0,sizeof(col));
60         memset(row,0,sizeof(row));
61         memset(squ,0,sizeof(squ));
62         memset(ans,0,sizeof(ans));
63         for (int i = 1; i <= 4; i++) {
64             for (int j = 1; j <= 4; j++) {
65                 if (a[i][j] == 0) continue;
66                 row[i][a[i][j]] = true;
67                 col[j][a[i][j]] = true;
68                 int z = pos(i, j);
69                 squ[z][a[i][j]] = true;
70             }
71         }
72         flag = false;
73         dfs(1, 1);
74         for (int i = 1; i <= 4; i++) {
75             for (int j = 1; j <= 4; j++) printf("%d",ans[i][j]);
76             printf("\n");
77         }
78     }
79 }
View Code

 

posted @ 2015-12-22 21:05  yanzx6  阅读(190)  评论(0编辑  收藏  举报