https://vjudge.net/contest/236374#problem/D
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 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×22×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!!!
InputThe first line of the input gives the number of test cases, T(1≤T≤100)T(1≤T≤100). TT 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.OutputFor each test case, output one line containing Case #x:, where xx 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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 4
char matrix[N][N];
char ans[N][N];
int right = 0;
int row[N][N]; //row[0][1]第0行1已经占据
int cloum[N][N];
void init()
{
int i = 0;
int j = 0;
for(i = 0;i<N;i++)
{
for(j=0;j<N;j++)
{
ans[i][j] = '\0';
row[i][j] = 0;
cloum[i][j] = 0;
}
}
return;
}
void parse()
{
int i = 0;
int j = 0;
int tmp = 0;
for(i = 0;i<N;i++)
{
for(j=0;j<N;j++)
{
if('*' == matrix[i][j]) continue;
tmp = matrix[i][j] - '0';
row[i][tmp] = 1;
cloum[j][tmp] = 1;
right+=1; //有1个合法的数字
}
}
right = 0;
}
void dfs(int x, int y, char value)
{
int tmpx = 0;
int tmpy = 0;
int k = 0;
int i = 0;
int j = 0;
if(x<0) return;
if(x>=N) return;
if(y<0) return;
if(y>=N) return;
if(16 == right) return;
if(1 == row[x][k]) return;
if(1 == cloum[y][k]) return;
ans[x][y] = value;
//向左向右
if((x>=0)&&(3 == y))
{
tmpx+=1;
tmpy = 0;
}
else if((x>=0)&&(x<=(N-1))&&(y>=0)&&(y<(N-1)))
{
tmpx =x;
tmpy =y+1;
}
for(k = 1;k<=4;k++)
{
right+=1;
dfs(tmpx,tmpy,k);
row[tmpx][k] = 0;
cloum[tmpy][k] = 0;
right-=1;
}
}
int main()
{
int T = 0;
int i = 0;
int j = 0;
int k = 0;
int m = 0;
freopen("sukudo.txt","r",stdin);
scanf("%d",&T);
for(m = 1;m<=T;m++)
{
init();
for(j = 0;j<N;j++)
{
scanf("%s", matrix[j]);
}
parse();
for(i = 0;i<N;i++)
{
for(j = 0;j<N;j++)
{
for(k = 1;k<=4;k++)
{
if('*' != matrix[i][j])
{
ans[i][j] = matrix[i][j];
}
else
{
//if(1 == row[i][k]) continue;
//if(1 == cloum[j][k]) continue;
row[i][k] = 1;
cloum[j][k] = 1;
right+=1;
dfs(i,j,k);
if(16 == right) break;
row[i][k] = 0;
cloum[j][k] = 0;
right-=1;
}
}
}
}
printf("Case #%d\n",m);
for(i = 0;i<N;i++)
{
for(j = 0;j<N;j++)
{
printf("%c",ans[i][j]);
}
printf("\n");
}
}
return 0;
}