Sudoku
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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 4×4 board with every row contains 1 1 to 4 4, every column contains 1 1 to 4 4. Also he made sure that if we cut the board into four 2×2 2×2 pieces, every piece contains 1 1 to 4 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
T(
1≤T≤100
1≤T≤100).
T
T test cases follow. Each test case starts with an empty line followed by
4
4 lines. Each line consist of
4
4characters. 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
x is the test case number (starting from
1
1). Then output
4
4 lines with
4
4 characters each. indicate the recovered board.
Sample input and output
Sample Input | Sample Output |
---|---|
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 |
Source
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<stdio.h>
#include<math.h>
#include<stack>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define mem(a,num) memset(a,num,sizeof(a))
#define inf(a,n) fill(a,a+n,0x3f3f3f3f)
int get(char a)
{
if(a>'0'&&a<='9')return a-'0';
return 0;
}
char f(int a)
{
return char(a+'0');
}
int kk(int x,int y)//获取当前坐标所属分块标号
{
if(x<2){
if(y<2)return 0;
return 1;
}
if(y<2)return 2;
return 3;
}
int main()
{
ll T,t;
char a[4][4];
bool x[4][5],y[4][5],k[4][5];
scanf("%lld",&T);
t=T;
while(T--)
{
mem(x,0);
mem(y,0);
mem(k,0);
int count=0;
for(int i=0;i<4;++i)
for(int j=0;j<4;++j) cin>>a[i][j];
//scanf("%c",&a[i][j]);
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
{
int tmp = get(a[i][j]);
x[i][tmp]=1;
y[j][tmp]=1;
k[kk(i,j)][tmp]=1;
if(a[i][j]=='*')count++;
}
while(count>0){
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
{
if(a[i][j]=='*'){
int tmp=0,flag=0;
for(int l=1;l<5;++l)
if(!x[i][l]&&!y[j][l]&&!k[kk(i,j)][l]){
tmp++;
flag=l;
}
if(tmp==1){
a[i][j]=f(flag);
x[i][flag]=1;
y[j][flag]=1;
k[kk(i,j)][flag]=1;
count--;
}
}
}
}
printf("Case #%lld:\n",t-T);
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j) cout<<a[i][j];
cout<<endl;
}
}
return 0;
}