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
241
42
Sample Output
Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123
题意:让你玩4*4数独,每横行和每竖行只能是1,2,3,4四个数字(即不能有重复的数字),和平时玩的不同的是规则又加了一条,将数独平均分成4块(就是在第二行第二列引两条直线把它分开成4块),每一块的4个数中也必须是1,2,3,4这四个数字
思路:用dfs搜索来找答案,对*号那个部位进行枚举1,2,3,4,通过每一次往前判断直到16个位置全是数字
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
char mapp[6][6];
int judge(int r,int l)
{
for(int i=0;i<4;i++)
{
if(i!=r&&mapp[i][l]==mapp[r][l]) //判断和mapp[r][l]同列的数
return 0;
}
for(int j=0;j<4;j++)
{
if(j!=l&&mapp[r][j]==mapp[r][l])//判断和mapp[r][l]同行的数
return 0;
}
if(r<2) //表示此数在前2行
{
if(l<2) //表示此数在前两列
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(r!=i&&mapp[i][j]==mapp[r][l]) //不和mapp[r][l]同行的数
return 0;
if(l!=j&&mapp[r][l]==mapp[i][j])//不和mapp[r][l]同列的数
return 0;
}
}
}
else //次数在后两列
{
for(int i=0;i<2;i++)
{
for(int j=2;j<4;j++)
{
if(r!=i&&mapp[i][j]==mapp[r][l])
return 0;
if(l!=j&&mapp[r][l]==mapp[i][j])
return 0;
}
}
}
}
else
{
if(l<2)
{
for(int i=2;i<4;i++)
{
for(int j=0;j<2;j++)
{
if(r!=i&&mapp[i][j]==mapp[r][l])
return 0;
if(l!=j&&mapp[r][l]==mapp[i][j])
return 0;
}
}
}
else
{
for(int i=2;i<4;i++)
{
for(int j=2;j<4;j++)
{
if(r!=i&&mapp[i][j]==mapp[r][l])
return 0;
if(l!=j&&mapp[r][l]==mapp[i][j])
return 0;
}
}
}
}
return 1;
}
void dfs(int mark)
{
if(mark==16) //表明16个数全为数字
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
printf("%c",mapp[i][j]);
}
printf("\n");
}
return ;
}
int r,l; //r表示行,表示列
r=mark/4,l=mark%4;
if(mapp[r][l]=='*')
{
for(int a=1;a<=4;a++)
{
mapp[r][l]=a+'0';
if(judge(r,l)==1) //如果这个数字可以替代*,然后就去找下一个
{
dfs(mark+1);
}
mapp[r][l]='*'; //回溯
}
}
else
dfs(mark+1); //如果不是*,那么这个地方是数字,然后跳到下一个进行判断
}
int main()
{
int t,z=1;
scanf("%d",&t);
while(t--)
{
for(int a=0;a<4;a++)
scanf("%s",mapp[a]);
printf("Case #%d:\n",z++);
dfs(0);
}
return 0;
}
358

被折叠的 条评论
为什么被折叠?



