1 #include<cstdio> 2 #include<cstring> 3 #define MAXM 10 4 #define MAXL 324 5 #define MAXN 240000 6 #define INF 0x7FFFFFFF 7 char sd[MAXM][MAXM]; 8 int L[MAXN], R[MAXN], U[MAXN], D[MAXN], H[MAXN]; 9 int size, C[MAXN], S[MAXN], X[MAXN], Q[MAXN], vis[MAXL * 3]; 10 void Read() 11 { 12 int i, j; 13 for (i = 1; i < MAXM; i++) 14 { 15 for (j = 1; j < MAXM; j++) 16 scanf(" %c", &sd[i][j]); 17 } 18 } 19 void Init() 20 { 21 int i; 22 for (i = 0; i <= MAXL; i++) 23 { 24 L[i + 1] = i; 25 R[i] = i + 1; 26 U[i] = D[i] = i; 27 S[i] = 0; 28 } 29 R[MAXL] = 0; 30 size = MAXL + 1; 31 } 32 void Remove(int c) 33 { 34 int i, j; 35 L[R[c]] = L[c]; 36 R[L[c]] = R[c]; 37 for (i = D[c]; i != c; i = D[i]) 38 { 39 for (j = R[i]; j != i; j = R[j]) 40 { 41 U[D[j]] = U[j]; 42 D[U[j]] = D[j]; 43 S[C[j]]--; 44 } 45 } 46 } 47 void Resume(int c) 48 { 49 int i, j; 50 L[R[c]] = c; 51 R[L[c]] = c; 52 for (i = D[c]; i != c; i = D[i]) 53 { 54 for (j = R[i]; j != i; j = R[j]) 55 { 56 U[D[j]] = j; 57 D[U[j]] = j; 58 S[C[j]]++; 59 } 60 } 61 } 62 inline void Link(int r, int c) 63 { 64 D[size] = D[c]; 65 U[size] = c; 66 U[D[c]] = size; 67 D[c] = size; 68 if (H[r] < 0) 69 H[r] = L[size] = R[size] = size; 70 else 71 { 72 L[size] = H[r]; 73 R[size] = R[H[r]]; 74 L[R[H[r]]] = size; 75 R[H[r]] = size; 76 } 77 S[c]++; 78 X[size] = r; 79 C[size++] = c; 80 } 81 bool Dance(int now) 82 { 83 int i, j, c, temp; 84 if (R[0] == 0) 85 return true; 86 for (temp = INF,i = R[0]; i; i = R[i]) 87 { 88 if (S[i] < temp) 89 { 90 temp = S[i]; 91 c = i; 92 } 93 } 94 Remove(c); 95 for (i = D[c]; i != c; i = D[i]) 96 { 97 vis[X[i]] = true; 98 for (j = R[i]; j != i; j = R[j]) 99 Remove(C[j]); 100 if (Dance(now + 1)) 101 return true; 102 for (j = L[i]; j != i; j = L[j]) 103 Resume(C[j]); 104 vis[X[i]] = false; 105 } 106 Resume(c); 107 return false; 108 } 109 int main() 110 { 111 int i, j, k, r, ca; 112 scanf("%d", &ca); 113 while (ca--) 114 { 115 Read(); 116 Init(); 117 for (r = 0, i = 1; i < MAXM; i++) 118 { 119 for (j = 1; j < MAXM; j++) 120 { 121 if (sd[i][j] == '0') 122 { 123 for (k = 1; k < MAXM; k++) 124 { 125 H[++r] = -1; 126 Q[r] = k; 127 Link(r, (i - 1) * 9 + k); 128 Link(r, 81 + (j - 1) * 9 + k); 129 Link(r, 162 + ((i - 1) / 3 * 3 + (j - 1) / 3) * 9 + k); 130 Link(r, 243 + (i - 1) * 9 + j); 131 } 132 } 133 else 134 { 135 H[++r] = -1; 136 k = sd[i][j] - '0'; 137 Q[r] = k; 138 Link(r, (i - 1) * 9 + k); 139 Link(r, 81 + (j - 1) * 9 + k); 140 Link(r, 162 + ((i - 1) / 3 * 3 + (j - 1) / 3) * 9 + k); 141 Link(r, 243 + (i - 1) * 9 + j); 142 } 143 } 144 } 145 memset(vis, false, sizeof(vis)); 146 Dance(0); 147 for (k = 0, i = 1; i <= r; i++) 148 { 149 if (vis[i]) 150 { 151 k++; 152 printf("%d", Q[i]); 153 if (k % 9 == 0) 154 putchar('\n'); 155 } 156 } 157 } 158 return 0; 159 }