/**
Description
Input
Output
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
Source
**/
#include <iostream>
#define clear(array) memset(array,false,sizeof(array))
using namespace std;
bool Row[9][10]; //用于标识第i行用到了1-9中的哪些数。R[i][j] = true表示第i行数j已经使用过了
bool Column[9][10]; //用于标识第i列用到了1-9中的哪些数。说明同上
bool Block[9][10]; //用于标识每一个3*3列的方块中用到了1-9中的哪些数
//bool Tag[9][9];
int A[9][9]; //存储输入数据
void init()
{
clear(Row); //初始化数组,下同
clear(Column);
clear(Block);
//clear(Tag);
memset(A,0,sizeof(A));
}
void input()
{
int temp = 0;
for (int i=0; i<9; ++i)
{
for (int j=0; j<9; ++j)
{
scanf("%1d",&temp);
if (temp) //如果输入不为0
{
A[i][j] = temp;
Row[i][temp] = true;
Column[j][temp] = true;
Block[3*(i/3)+j/3][temp] = true;
}
}
}
cout << endl;
}
bool sudoku(int n)
{
int i = n / 9; //9*9中数组的行号
int j = n % 9; //列号
if (n > 80) return true; //搜索到最后一个节点返回
if (!A[i][j])
{
for (int k=1; k<=9; ++k)
{
if (!Row[i][k] && !Column[j][k] && !Block[3*(i/3)+j/3][k])//k可以放在当前位置
{
A[i][j] = k;
Row[i][k] = true;
Column[j][k] = true;
Block[3*(i/3)+j/3][k] = true;
if (sudoku(n+1)) return true; //搜索解决下一个位置
A[i][j] = 0;
Row[i][k] = false;
Column[j][k] = false;
Block[3*(i/3)+j/3][k] = false;
}
}
return false; //此处的return 很重要。切记:回溯递归时正确return很重要。一个if一个return
}
if (sudoku(n+1)) return true;
return false;
}
void output()
{
for (int i=0; i<9; ++i)
{
for (int j=0; j<9; ++j)
{
cout << A[i][j];
}
cout << endl;
}
}
int main()
{
int n = 0;
cin >> n;
for (int i=0; i<n; ++i)
{
init();
input();
sudoku(0);
output();
}
return 0;
}