代码如下 :
#include <cstdio>
using namespace std;
int a[10][10] , last[2] , min[10][10] , ok;
bool check[10][10][10][10];
int plaid(int x , int y)
{
return (x - 1) / 3 * 3 + 1 + (y - 1) / 3;
}
void in()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
scanf("%d" , &a[i][j]);
if (a[i][j]) ok++;
min[i][j] = 1;
check[i][j][plaid(i , j)][a[i][j]] = true;
}
}
}
void out()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
printf("%d " , a[i][j]);
}
puts("");
}
}
void dfs(int x , int y)
{
for (int i = min[x][y]; i <= 9; i++)
{
if (check[x][y][plaid(x , y)][i] && i == 9) dfs(last[0] , last[1]);
a[x][y] = i;
min[x][y] = i;
last[0] = x;
last[1] = y;
if (y == 9) dfs(x + 1 , 1);
else dfs(x , y + 1);
}
}
int main()
{
in();
dfs(1 , 1);
out();
return 0;
}