#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int map[10][10], hang[10][10], lie[10][10], kuai[10][10], cnt;
int cal(int i, int j)//输入行列号,判断他在哪一块里
{
return ((i-1)/3)+(((j-1)/3)*3+1);
}
struct point
{
int x;
int y;
}p[100];
int dfs(int n)
{
if(n > cnt) return 1;
int xx = p[n].x, yy = p[n].y;
for(int i = 1; i <= 9; i++)
{
if(!hang[xx][i] && !lie[yy][i] && !kuai[cal(xx, yy)][i])
{
hang[xx][i] = 1;
lie[yy][i] = 1;
kuai[cal(xx, yy)][i] = 1;
map[xx][yy] = i;
if(dfs(n+1)) return 1;
hang[xx][i] = 0;
lie[yy][i] = 0;
kuai[cal(xx, yy)][i] = 0;
}
}
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(map, 0, sizeof(map));
memset(hang, 0, sizeof(hang));
memset(lie,0 ,sizeof(lie));
memset(kuai, 0, sizeof(kuai));
cnt = 0;
char str[10];
for(int i = 1; i <= 9; i++)
{
scanf("%s", str);
for(int j = 1; j <= 9; j++)
{
int x = str[j-1]-48;
if(x)
{
map[i][j] = x;
hang[i][x] = 1;
lie[j][x] = 1;
kuai[cal(i, j)][x] = 1;
}
else
{
p[++cnt].x = i;
p[cnt].y = j;
}
}
}
dfs(1);
for(int i = 1; i <= 9; i++)
{
for(int j = 1; j <= 9; j++)
{
cout<<map[i][j];
}
cout<<endl;
}
}
return 0;
}
DFS-Sudoku(poj2676)
猜你喜欢
转载自blog.csdn.net/weixin_43863650/article/details/84892870
今日推荐
周排行