UESTC - 1222 Sudoku

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×44×4 board with every row contains 11 to 44 , every column contains 11 to 44 .
Also he made sure that if we cut the board into four 2×22×2

pieces, every piece contains 11

to 44

.

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, TT

(1≤T≤1001≤T≤100

). TT

test cases follow. Each test case starts with an empty line followed by 44

lines.
Each line consist of 44

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 xx

is the test case number (starting from 11

).
Then output 44

lines with 44

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

题意:一个4*4的矩阵,满足每个数的同行同列都由1234构成的,把4*4矩阵分成4个2*2矩阵,每个矩阵的四个元素也分别为1234。这题就是暴力深搜。ac代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #define Max 8
 4 char st[Max][Max];
 5 int judg(int x,int y,char i)   //判断当前位置可以是什么数字
 6 {
 7     if(x<3&&y<3)
 8     {
 9         if(st[1][1]==i||st[1][2]==i||st[2][1]==i||st[2][2]==i)
10         return 0;
11     }
12     else if(x<3&&y>2)
13     {
14         if(st[1][3]==i||st[1][4]==i||st[2][3]==i||st[2][4]==i)
15         return 0;
16     }
17     else if(x>2&&y<3)
18     {
19         if(st[3][1]==i||st[3][2]==i||st[4][1]==i||st[4][2]==i)
20         return 0;
21     }
22     else if(x>2&&y>2)
23     {
24         if(st[3][3]==i||st[3][4]==i||st[4][3]==i||st[4][4]==i)
25         return 0;
26     }
27     for(int k=1;k<=4;k++)
28             {
29                 if(st[x][k]==i||st[k][y]==i)
30                 return 0;
31             }
32     return  1;
33 }
34 void dfs(int x,int y)
35 {
36     if(x==5)
37     {
38         for(int i=1;i<=4;i++)
39         printf("%s\n",st[i]+1);
40         return ;
41     }
42     if(st[x][y]!=‘*‘)
43     {
44         if(y==4)
45         dfs(x+1,1);
46         else
47         dfs(x,y+1);
48     }
49     else
50     {
51         for(int i=1;i<=4;i++)
52         {
53             if(judg(x,y,i+‘0‘))
54             {
55                 st[x][y]=i+‘0‘;
56                 if(y==4)
57                 dfs(x+1,1);
58                 else
59                 dfs(x,y+1);
60                 st[x][y]=‘*‘;  //如果没有满足的则回溯
61             }
62         }
63     }
64 }
65 int main()
66 {
67     int t;
68     scanf("%d",&t);
69     getchar();
70     for(int i=1;i<=t;i++)
71     {
72         for(int j=1;j<=4;j++)
73         scanf("%s",st[j]+1);
74         printf("Case #%d:\n",i);
75         dfs(1,1);
76     }
77     return 0;
78 }
				
时间: 2024-10-07 18:16:42

UESTC - 1222 Sudoku的相关文章

ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进行判断是否已经出现了重复,如果最后再判断的话复杂度有点高. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>

UESTC 电子科大专题训练 数据结构 D

UESTC 1584 题意:平面坐标上有n个怪物,每个怪物有一个rank值,代表x坐标和y坐标都不大于它本身的怪物数(不包括本身) 思路:对x y坐标从小到大排序,x优先排序,用数状数组计算y坐标小于它的数量 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #i

LeetCode37 Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially

UESTC 31 饭卡(Card) --背包问题

背包问题. 思路:如果m<5,此时也不能消费,所以此时答案为m m>=5: 求出背包容量为m-5,买前n-1样便宜的菜(排个序)的最大价值(即最大消费,即消费完后剩余值最接近5)最后减去最大的那个菜的价格,就得到最小的余额. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using n

UESTC 电子科大专题训练 数据结构 A

UESTC 1591 题意:求区间极值之差 思路:线段树裸题,不带更新 ACA代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "ma

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti