#include using namespace std; int grid[9][9]; bool rows[9][10], cols[9][10], sqs[9][10]; int r[5], c[5]; bool ok; bool getInput() { for(int i=0; i<9; i++) for(int j=1; j<=9; j++) rows[i][j] = false; int ihole=0; bool rowsOk = true; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { char ch; cin >> ch; int digit = ch-'0'; grid[i][j] = digit; if (digit == 0) { r[ihole] = i; c[ihole] = j; ihole++; } else if (rows[i][digit]) rowsOk = false; else rows[i][digit] = true; } } return rowsOk; } bool checkCols() { for(int i=0; i<9; i++) for(int j=1; j<=9; j++) cols[i][j] = false; for(int j=0; j<9; j++) { for(int i=0; i<9; i++) { int digit = grid[i][j]; if (digit > 0) { if (cols[j][digit]) return false; else cols[j][digit] = true; } } } return true; } bool checkSqs() { for(int i=0; i<9; i++) for(int j=1; j<=9; j++) sqs[i][j] = false; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { int digit = grid[i][j]; if (digit > 0) { int k = 3*(i/3) + (j/3); if (sqs[k][digit]) return false; else sqs[k][digit] = true; } } } return true; } bool okPlace(int ihole, int val) { int row = r[ihole]; int col = c[ihole]; int sq = 3*(row/3) + (col/3); if (!rows[row][val] && !cols[col][val] && !sqs[sq][val]) { grid[row][col] = val; return true; } else return false; } void printGrid() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) cout << grid[i][j]; cout << endl; } } int main() { int ncases; cin >> ncases; for(int icase=1; icase<=ncases; icase++) { bool found = false; ok = getInput(); if (ok) ok = checkCols(); if (ok) ok = checkSqs(); if (ok) { for(int i1=1; i1<=9; i1++) { if (okPlace(0, i1)) for(int i2=1; i2<=9; i2++) { if (okPlace(1, i2)) for(int i3=1; i3<=9; i3++) { if (okPlace(2, i3)) for(int i4=1; i4<=9; i4++) { if (okPlace(3, i4)) for(int i5=1; i5<=9; i5++) { if (okPlace(4, i5)) { found = true; break; } } if (found) break; } if (found) break; } if (found) break; } if (found) break; } } if (ok && found) printGrid(); else cout << "Could not complete this grid." << endl; if (icase < ncases) cout << endl; } return 0; }