DLX。在模板的基础上增加一个FILL属性,略修改即可。
/* 4210 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
static const int maxc = **+;
static const int maxr = ;
static const int maxn = maxr * maxc; int n, sz;
int S[maxc]; int row[maxn], col[maxn];
int L[maxn], R[maxn], U[maxn], D[maxn]; int ansd, ans[maxr]; void init(int n_) {
n = n_; rep(i, , n+) {
L[i] = i - ;
R[i] = i + ;
U[i] = i;
D[i] = i;
col[i] = i;
} L[] = n;
R[n] = ; sz = n + ;
memset(S, , sizeof(S));
} void addRow(int r, vi columns) {
int size = SZ(columns);
int first = sz; rep(i, , size) {
int c = columns[i]; L[sz] = sz - ;
R[sz] = sz + ; 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-] = first;
L[first] = sz-;
} void remove(int c) {
L[R[c]] = L[c];
R[L[c]] = R[c];
for (int i=D[c]; i!=c; i=D[i]) {
for (int j=R[i]; j!=i; j=R[j]) {
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[col[j]];
}
}
} void restore(int c) {
L[R[c]] = c;
R[L[c]] = c;
for (int i=D[c]; i!=c; i=D[i]) {
for (int j=R[i]; j!=i; j=R[j]) {
U[D[j]] = j;
D[U[j]] = j;
++S[col[j]];
}
}
} bool dfs(int d) {
if (R[] == ) {
ansd = d;
return true;
} int c = R[];
for (int i=R[]; i!=; i=R[i]) {
if (S[i] < S[c])
c = i;
} remove(c);
for (int i=D[c]; i!=c; i=D[i]) {
ans[d] = row[i];
for (int j=R[i]; j!=i; j=R[j]) {
remove(col[j]);
}
if(dfs(d + )) return true;
for (int j=L[i]; j!=i; j=L[j]) {
restore(col[j]);
}
}
restore(c); return false;
} void solve(vi& v) {
v.clr(); dfs(); rep(i, , ansd)
v.pb(ans[i]);
} } DLX; typedef struct {
int a, b;
int r, c;
int d;
} node_t; const int SLOT = ;
const int ROW = ;
const int COL = ;
const int SUB = ;
const int FILL = ;
const int maxn = ;
int M[][];
bool mark[][];
DLX solver;
node_t nd[maxn];
int rn;
vi ans; int encode(int a, int b, int c) {
return a* + b* + c+;
} void decode(int code, int& a, int& b, int& c) {
--code;
c = code % ;
code /= ;
b = code % ;
code /= ;
a = code;
} void init() {
rn = ;
memset(mark, false, sizeof(mark));
} void input(int n) {
int r, c;
int x1, x2;
char ps1[], ps2[], ps[]; solver.init(**);
init();
rep(i, , n) {
scanf("%d %s %d %s", &x1, ps1, &x2, ps2);
vi columns; // handle 1-st
r = ps1[]-'A';
c = ps1[]-'';
--x1;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, x1));
columns.pb(encode(COL, c, x1));
columns.pb(encode(SUB, r/*+c/, x1));
M[r][c] = x1; // handle 2-nd
r = ps2[]-'A';
c = ps2[]-'';
--x2;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, x2));
columns.pb(encode(COL, c, x2));
columns.pb(encode(SUB, r/*+c/, x2));
M[r][c] = x2; mark[x1][x2] = mark[x2][x1] = true;
columns.pb(encode(FILL, x1, x2));
columns.pb(encode(FILL, x2, x1));
solver.addRow(rn++, columns);
}
rep(i, , ) {
scanf("%s", ps);
r = ps[]-'A';
c = ps[]-'';
M[r][c] = i;
mark[i][i] = true;
vi columns;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, i));
columns.pb(encode(COL, c, i));
columns.pb(encode(SUB, r/*+c/, i));
columns.pb(encode(FILL, i, i));
solver.addRow(rn++, columns);
}
rep(i, , ) {
rep(j, , ) {
if (mark[i][j])
continue;
for (r=; r<=; ++r) {
for (c=; c<; ++c) {
nd[rn].a = i;
nd[rn].b = j;
nd[rn].d = ;
nd[rn].r = r;
nd[rn].c = c; vi columns;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, i));
columns.pb(encode(COL, c, i));
columns.pb(encode(SUB, r/*+c/, i));
int cc = c + ;
columns.pb(encode(SLOT, r, cc));
columns.pb(encode(ROW, r, j));
columns.pb(encode(COL, cc, j));
columns.pb(encode(SUB, r/*+cc/, j));
columns.pb(encode(FILL, i, j));
columns.pb(encode(FILL, j, i));
solver.addRow(rn++, columns);
}
} for(r=; r<; ++r) {
int rr = r + ;
for (c=; c<=; ++c) {
nd[rn].a = i;
nd[rn].b = j;
nd[rn].d = ;
nd[rn].r = r;
nd[rn].c = c; vi columns;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, i));
columns.pb(encode(COL, c, i));
columns.pb(encode(SUB, r/*+c/, i));
columns.pb(encode(SLOT, rr, c));
columns.pb(encode(ROW, rr, j));
columns.pb(encode(COL, c, j));
columns.pb(encode(SUB, rr/*+c/, j));
columns.pb(encode(FILL, i, j));
columns.pb(encode(FILL, j, i));
solver.addRow(rn++, columns);
}
}
}
}
} void solve(int n) {
solver.solve(ans);
int sz = SZ(ans), id;
n += ; rep(i, , sz) {
if (ans[i] > n) {
id = ans[i];
if (nd[id].d) {
M[nd[id].r][nd[id].c] = nd[id].a;
M[nd[id].r+][nd[id].c] = nd[id].b;
} else {
M[nd[id].r][nd[id].c] = nd[id].a;
M[nd[id].r][nd[id].c+] = nd[id].b;
}
}
} rep(i, , ) {
rep(j, , ) {
++M[i][j];
printf("%d", M[i][j]);
}
putchar('\n');
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n;
int t = ; while (scanf("%d",&n)!=EOF && n) {
memset(mark, false, sizeof(mark));
input(n);
printf("Puzzle %d\n", ++t);
solve(n);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}