#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int count,map[10][10];
int a[100][2];
void Input()
{
char ch;
count=0;
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
{
cin>>ch;
map[i][j] = ch-'0';
if(map[i][j]==0)
{
a[count][0]=i;
a[count++][1]=j;
}
}
}
bool Isok(int x,int y,int value)
{
for(int i=1;i<=9;i++)
{
if(map[x][i]==value ) return false;
if(map[i][y]==value ) return false;
}
int xx = ((x-1)/3)*3; //注意这里的计算
int yy = ((y-1)/3)*3;
for( i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(map[xx+i][yy+j]==value)
return false;
return true;
}
bool dfs(int cur)
{
if(cur<0)
return 1;
int x,y;
for(int i=1;i<=9;i++)
{
x = a[cur][0];
y = a[cur][1];
if(Isok(x,y,i))
{
map[x][y]=i;
if(dfs(cur-1))
return 1;
//dfs(cur-1);
map[x][y]=0;
}
}
return 0;
}
void Output()
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
cout<<map[i][j];
cout<<endl;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
Input();
dfs(count-1);
printf("*****\n");
Output();
}
return 0;
}
/*
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
*/
数独
最新推荐文章于 2023-02-05 15:24:13 发布