精确覆盖DLX,模板
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define FIR first
#define SEC second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxc = 1010;
const int maxr = 500;
const int maxnode = maxc*maxr;
struct DLX {
int n, sz;
int S[maxc];
int row[maxnode], col[maxnode];
int L[maxnode], R[maxnode], U[maxnode], D[maxnode];
int ansd, ans[maxr];
void init(int n) { ///n是列数
this->n = n;
for(int i = 0; i <= n; i ++) {
U[i] = i, D[i] = i;
L[i] = i - 1, R[i] = i + 1;
}
R[n] = 0; L[0] = n;
sz = n + 1;
CLR(S, 0);
}
void addRow(int r, vector<int> columns) {
int first = sz;
for(int i = 0; i < columns.size(); i ++) {
int c = columns[i];
L[sz] = sz - 1; R[sz] = sz + 1;
D[sz] = c; U[sz] = U[c];
D[U[c]] = sz; U[c] = sz;
row[sz] = r; col[sz] = c;
S[c] ++; sz ++;
}
R[sz - 1] = first; L[first] = sz - 1;
}
#define FOR(i, A, s) for(int i = A[s]; i != s; i = A[i])
void remove(int c) {
L[R[c]] = L[c];
R[L[c]] = R[c];
FOR(i, D, c)
FOR(j, R, i) {
U[D[j]] = U[j];
D[U[j]] = D[j];
-- S[col[j]];
}
}
void restore(int c) {
FOR(i, U, c)
FOR(j, L, i) {
++ S[col[j]];
U[D[j]] = j;
D[U[j]] = j;
}
L[R[c]] = c;
R[L[c]] = c;
}
bool dfs(int d) {
if(R[0] == 0) {
ansd = d;
return true;
}
int c = R[0];
FOR(i, R, 0) if(S[i] < S[c]) c = i;
remove(c);
FOR(i, D, c) {
ans[d] = row[i];
FOR(j, R, i) remove(col[j]);
if(dfs(d + 1)) return true;
FOR(j, L, i) restore(col[j]);
}
restore(c);
return false;
}
bool solve(vector<int>& v) {
v.clear();
if(!dfs(0)) return false;
for(int i = 0 ; i < ansd; i ++) v.push_back(ans[i]);
return true;
}
} sol;
char ch[20][20];
int main() {
int T;
scanf("%d", &T);
while(T --) {
gets(ch[10]);
for(int i = 0; i < 9; i ++)
gets(ch[i]);
sol.init(9*9*4);
for(int i = 0; i < 9; i ++) {
for(int j = 0; j < 9; j ++) {
for(int k = 0; k < 9; k ++) {
if(ch[i][j] == '?' || ch[i][j] == k + '1') {
vector<int> v; v.clear();
int tmp = i * 9 + j + 1;
v.push_back(tmp);
tmp = 81 + i * 9 + k + 1;
v.push_back(tmp);
tmp = 81 * 2 + j * 9 + k + 1;
v.push_back(tmp);
tmp = 81 * 3 + (i / 3 * 3 + j / 3) * 9 + k + 1;
v.push_back(tmp);
sol.addRow(i * 81 + j * 9 + k + 1, v);
}
}
}
}
vector<int> ans;
ans.clear();
if(!sol.solve(ans)) puts("impossible");
else {
for(int i = 0; i < ans.size(); i ++) {
ans[i] --;
ch[ans[i] / 81][(ans[i] / 9) % 9] = ans[i] % 9 + '1';
}
for(int i = 0; i < 9; i ++)
puts(ch[i]);
}
if(T) puts("---");
}
}