#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std; short r[9][10], c[9][10], block[9][10]; int board[9][9]; struct Pos { int r, c; Pos(int rr, int cc): r(rr), c(cc) { } }; vector<Pos> blank; inline int Getblock (int r, int c) { int rr = r / 3; int cc = c / 3;; return rr * 3 + cc; } void Setflag (int i, int j, int num, int f) { r[i][num] = f; c[j][num] = f; block[Getblock(i, j)][num] = f; } bool ok (int i, int j, int num) { return !r[i][num] && !c[j][num] && !block[Getblock(i, j)][num]; } bool Dfs (int n) { if (n < 0) return true; int r = blank[n].r, c = blank[n].c; for (int num = 1; num <= 9; ++num) { if (ok(r, c, num)) { board[r][c] = num; Setflag(r, c, num, 1); if (Dfs(n - 1)) return true; Setflag(r, c, num, 0); } } return false; } int main () { int t; cin >> t; while (t--) { memset(r, 0, sizeof(r)); memset(c, 0, sizeof(c)); memset(block, 0, sizeof(block)); blank.clear(); for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { char c; cin >> c; board[i][j] = c - '0'; if (board[i][j]) Setflag(i, j, board[i][j], 1); else blank.push_back(Pos(i, j)); } } if (Dfs(blank.size() - 1)) { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) cout << char(board[i][j] + '0'); cout << endl; } } } return 0; }
|