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!!!
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!!!
It's guaranteed that there will be exactly one way to recover the board.
3 **** 2341 4123 3214 *243 *312 *421 *134 *41* **3* 2*41 4*2*
Case #1: 1432 2341 4123 3214 Case #2: 1243 4312 3421 2134 Case #3: 3412 1234 2341 4123
题解:
给你一个4x4的矩阵,*号表示可以填的位置,要求满足数独规则并且四个角上2x2的地方要唯一,也就是只有1234,深搜+横列数列加上四个方阵判断一下就好了
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 100861111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
int a[5][5];
int h[5][5];
int l[5][5],ans,b[5][5];
char s[5][5];
int tag;
void dfs(int x,int y,int dp)
{
int i,j,k;
if(tag)
return;
for(i=1;i<=4;i++)
{
if(h[x][i]||l[y][i])
continue;
if(x>=0&&x<=1&&y>=0&&y<=1)
{
if(b[1][i])
continue;
b[1][i]=1;
}
else if(x>=0&&x<=1&&y>=2&&y<=3)
{
if(b[2][i])
continue;
b[2][i]=1;
}
else if(x>=2&&x<=3&&y>=0&&y<=1)
{
if(b[3][i])
continue;
b[3][i]=1;
}
else if(x>=2&&x<=3&&y>=2&&y<=3)
{
if(b[4][i])
continue;
b[4][i]=1;
}
a[x][y]=i;
h[x][i]=1;
l[y][i]=1;
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
if(a[j][k]==0)
{
dfs(j,k,dp+1);
goto deep;
}
}
}
deep:;
if(dp>=ans)
{
tag=1;
return;
}
if(tag)
return;
if(x>=0&&x<=1&&y>=0&&y<=1)
{
b[1][i]=0;
}
else if(x>=0&&x<=1&&y>=2&&y<=3)
{
b[2][i]=0;
}
else if(x>=2&&x<=3&&y>=0&&y<=1)
{
b[3][i]=0;
}
else if(x>=2&&x<=3&&y>=2&&y<=3)
{
b[4][i]=0;
}
h[x][i]=0;
l[y][i]=0;
a[x][y]=0;
}
}
int main()
{
int i,j,test,q;
scanf("%d",&test);
for(q=1;q<=test;q++)
{
memset(h,0,sizeof(h));
memset(l,0,sizeof(l));
memset(b,0,sizeof(b));
ans=0;
for(i=0;i<4;i++)
{
scanf("%s",s[i]);
for(j=0;j<4;j++)
{
if(s[i][j]=='*')
{
ans++;
a[i][j]=0;
continue;
}
h[i][s[i][j]-'0']=1;
l[j][s[i][j]-'0']=1;
if(i>=0&&i<=1&&j>=0&&j<=1)
b[1][s[i][j]-'0']=1;
else if(i>=0&&i<=1&&j>=2&&j<=3)
b[2][s[i][j]-'0']=1;
else if(i>=2&&i<=3&&j>=0&&j<=1)
b[3][s[i][j]-'0']=1;
else if(i>=2&&i<=3&&j>=2&&j<=3)
b[4][s[i][j]-'0']=1;
a[i][j]=s[i][j]-'0';
}
}
tag=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(s[i][j]=='*')
{
dfs(i,j,1);
goto loop;
}
}
}
loop:;
printf("Case #%d:\n",q);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d",a[i][j]);
printf("\n");
}
}
return 0;
}