Sudoku
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2217 Accepted Submission(s): 752
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!!!
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.
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
Source
The 2015 China Collegiate Programming Contest
#include <iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
char maap[5][5];//这里maap[10001][10001]绝对不行
int vis[11][11][11];
int flagg;
void dfs(int a,int b)
{
// cout<<"ofdsofdsofds"<<endl;
for(int i=0; i<4; i++)
{
if(maap[a][i]!='*')
{
vis[a][b][maap[a][i]-'0']=1;
}
}
for(int i=0; i<4; i++)
{
if(maap[i][b]!='*')
{
vis[a][b][maap[i][b]-'0']=1;
}
}
if(a==0)
{
if(b==0)
{
if(maap[a+1][b+1]!='*')
{
vis[a][b][maap[a+1][b+1]-'0']=1;
}
}
if(b==1)
{
if(maap[1][0]!='*')
{
vis[a][b][maap[1][0]-'0']=1;
}
}
if(b==2)
{
if(maap[1][3]!='*')
{
vis[a][b][maap[1][3]-'0']=1;
}
}
if(b==3)
{
if(maap[1][2]!='*')
{
vis[a][b][maap[1][2]-'0']=1;
}
}
}
if(a==1)
{
if(b==0)
{
if(maap[0][1]!='*')
{
vis[a][b][maap[0][1]-'*']=1;
}
}
if(b==1)
{
if(maap[0][0]!='*')
{
vis[a][b][maap[0][0]-'*']=1;
}
}
if(b==2)
{
if(maap[0][3]!='*')
{
vis[a][b][maap[0][3]-'0']=1;
}
}
if(b==3)
{
if(maap[0][2]!='*')
{
vis[a][b][maap[0][2]-'0']=1;
}
}
}
if(a==2)
{
if(b==0)
{
if(maap[3][1]!='*')
{
vis[a][b][maap[3][1]-'0']=1;
}
}
if(b==1)
{
if(maap[3][0]!='*')
{
vis[a][b][maap[3][0]-'0']=1;
}
}
if(b==2)
{
if(maap[3][3]!='*')
{
vis[a][b][maap[3][3]-'0']=1;
}
}
if(b==3)
{
if(maap[3][2]!='*')
{
vis[a][b][maap[3][2]-'0']=1;
}
}
}
if(a==3)
{
if(b==0)
{
if(maap[2][1]!='*')
{
vis[a][b][maap[2][1]-'0']=1;
}
}
if(b==1)
{
if(maap[2][0]!='*')
{
vis[a][b][maap[2][0]-'0']=1;
}
}
if(b==2)
{
if(maap[2][3]!='*')
{
vis[a][b][maap[2][3]-'0']=1;
}
}
if(b==3)
{
if(maap[2][2]!='*')
{
vis[a][b][maap[2][2]-'0']=1;
}
}
}
}
bool juge(int a,int b,int k)
{
// cout<<"ofdsofdsofds"<<endl;
for(int i=0; i<4; i++)
{
if(maap[a][i]!='*')
{
if(maap[a][i]==k+'0')
{
return false;
}
}
}
for(int i=0; i<4; i++)
{
if(maap[i][b]!='*')
{
if(maap[i][b]==k+'0')
{
return false;
}
}
}
if(a==0)
{
if(b==0)
{
if(maap[a+1][b+1]!='*')
{
if(maap[a+1][b+1]==k+'0')
{
return false;
}
}
}
if(b==1)
{
if(maap[1][0]!='*')
{
if(maap[1][0]==k+'0')
{
return false;
}
}
}
if(b==2)
{
if(maap[1][3]!='*')
{
if(maap[1][3]==k+'0')
{
return false;
}
}
}
if(b==3)
{
if(maap[1][2]!='*')
{
if(maap[1][2]==k+'0')
{
return false;
}
}
}
}
if(a==1)
{
if(b==0)
{
if(maap[0][1]!='*')
{
if(maap[0][1]==k+'0')
{
return false;
}
}
}
if(b==1)
{
if(maap[0][0]!='*')
{
if(maap[0][0]==k+'0')
{
return false;
}
}
}
if(b==2)
{
if(maap[0][3]!='*')
{
if(maap[0][3]==k+'0')
{
return false;
}
}
}
if(b==3)
{
if(maap[0][2]!='*')
{
if(maap[0][2]==k+'0')
{
return false;
}
}
}
}
if(a==2)
{
if(b==0)
{
if(maap[3][1]!='*')
{
if(maap[3][1]==k+'0')
return false;
}
}
if(b==1)
{
if(maap[3][0]!='*')
{
if(maap[3][0]==k+'0')
{
return false;
}
}
}
if(b==2)
{
if(maap[3][3]!='*')
{
if(maap[3][3]==k+'0')
{
return false;
}
}
}
if(b==3)
{
if(maap[3][2]!='*')
{
if(maap[3][2]==k+'0')
{
return false;
}
}
}
}
if(a==3)
{
if(b==0)
{
if(maap[2][1]!='*')
{
if(maap[2][1]==k+'0')
{
return false;
}
}
}
if(b==1)
{
if(maap[2][0]!='*')
{
if(maap[2][0]==k+'0')
{
return false;
}
}
}
if(b==2)
{
if(maap[2][3]!='*')
{
if(maap[2][3]==k+'0')
{
return false;
}
}
}
if(b==3)
{
if(maap[2][2]!='*')
{
if(maap[2][2]==k+'0')
{
return false;
}
}
}
}
return true;
}
int suan(int a1,int b1,int k)
{
for(int i=0;i<4;i++)
{
if(maap[i][b1]!='*')
{
if(maap[i][b1]-'0'==k)
return 0;
}
}
for(int i=0;i<4;i++)
{
if(maap[a1][i]!='*')
{
if(maap[a1][i]-'0'==k)
{
return 0;
}
}
}
if(a1>=0&&a1<=1)
{
if(b1>=0&&b1<=1)
{
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
if(maap[i][j]-'0'==k)
{
return 0;
}
}
}
}
}
else
{
if(b1>=0&&b1<=1)
{
for(int i=2;i<=3;i++)
{
for(int j=0;j<=1;j++)
{
if(maap[i][j]-'0'==k)
{
return 0;
}
}
}
}
else
{
for(int i=2;i<=3;i++)
{
for(int j=2;j<=3;j++)
{
if(maap[i][j]-'0'==k)
{
return 0;
}
}
}
}
}
return 1;
}
void dfs1(int i,int j)
{
if(j==4)
{
//if(i+1)
dfs1(i+1,0);
}
if(i==4)
{
flagg=1;
//if(j+1<4)
//dfs(i,j+1);
return;
}
else
{
if(maap[i][j]=='*')
{
for(int k=1;k<=4;k++)
{
if(juge(i,j,k))
{
maap[i][j]=k+'0';
dfs1(i,j+1);
if(flagg==0)
{
maap[i][j]='*';
}
}
}
}
else
{
dfs1(i,j+1);
}
}
}
int main()
{
int t,T=1;
scanf("%d",&t);
while(t--)
{
for(int i=0; i<4; i++)
{
scanf("%s",maap[i]);
}
int flag=0;
/* for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(maap[i][j]=='*')
{
memset(vis,0,sizeof(vis));
dfs(i,j);
int num=0;
int poi;
for(int p=1; p<=4; p++)
{
if(vis[i][j][p]==0)
{
num++;
poi=p;
}
}
if(num==1)
{
maap[i][j]=poi+'0';
}
else
{
flag=1;
}
}
}
}
if(flag)*/
// {
flagg=0;
dfs1(0,0);
// }
printf("Case #%d:\n",T++);
for(int i=0; i<4; i++)
{
printf("%s\n",maap[i]);
}
}
return 0;
}