题目大意:
解一个16*16形式的数独
要求每行每列每个16宫格都包含A~P
大致思路:
和POJ 3074一个道理...继续练手
刚开始数组开小调了一段时间...
代码如下:
Result : Accepted Memory : 500 KB Time : 3391 ms
/* * Author: Gatevin * Created Time: 2015/10/2 8:52:27 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; /* * DLX解经典数独问题 * 考虑到每行都要有1~16 * 用第1~16*16列表示第i行是否有数字j(第(i - 1)*9 + j列的状态) * 由于每列都要有1~16 * 用第16*16 + 1 ~ 16*16*2列表示第i列是否有数字j (第(i - 1)*16 + j + 16*16列的状态) * 考虑到每个九宫格都要有1~16 * 用第16*16+1~16*16*2列表示第i个九宫格是否有数字j (第(i - 1)*16 + j + 16*16列的状态) * 但是仅仅依靠这些是不够的 * 还有一个限制条件每个格子只能有1个数字 * 所以用第16*16*2+1~16*16*3列表示格子中是否填了数 * 那么对于给定的数字, 就在对应的列上写4个1 * 如果是.那么就添加16行, 分别对应哪个位子填1~16的情况 * 那么最坏情况下有16*16*16行可选 */ char in[800]; struct DLX { #define maxn 16*16*16 + 4 #define maxm 16*16*4 + 4 #define maxnode 16*16*16*4 + 100 int n, m, size; int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode]; int H[maxn], S[maxm]; int ansd, ans[maxn]; struct State { int x, y, val; State(int _x, int _y, int _val) { x = _x, y = _y, val = _val; } State(){} }; State s[maxn]; void init(int _n, int _m) { n = _n; m = _m; for(int i = 0; i <= m; i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i - 1; R[i] = i + 1; } R[m] = 0; L[0] = m; size = m; for(int i = 1; i <= n; i++) H[i] = -1; } void Link(int r, int c) { ++S[Col[++size] = c]; Row[size] = r; D[size] = D[c]; U[D[c]] = size; U[size] = c; D[c] = size; if(H[r] < 0) H[r] = L[size] = R[size] = size; else { R[size] = R[H[r]]; L[R[H[r]]] = size; L[size] = H[r]; R[H[r]] = size; } } 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 resume(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) ++S[Col[U[D[j]] = D[U[j]] = j]]; L[R[c]] = R[L[c]] = c; } bool Dance(int dep) { if(R[0] == 0) { ansd = dep; return true; } int c = R[0]; for(int i = R[0]; i != 0; i = R[i]) if(S[i] < S[c]) c = i; remove(c); for(int i = D[c]; i != c; i = D[i]) { ans[dep] = Row[i]; for(int j = R[i]; j != i; j = R[j]) remove(Col[j]); if(Dance(dep + 1)) return true; for(int j = L[i]; j != i; j = L[j]) resume(Col[j]); } resume(c); return false; } void solve() { int tn = 0, tm = 16*16*4; int len = strlen(in); for(int i = 0; i < len; i++) if(in[i] == '-') tn += 16; else tn++; init(tn, tm); tn = 0; for(int i = 0; i < 16; i++) for(int j = 0; j < 16; j++) { char c = in[i*16 + j]; if(c == '-') { for(int k = 1; k <= 16; k++) { s[++tn] = State(i, j, k); Link(tn, i*16 + k); Link(tn, j*16 + k + 16*16); Link(tn, (i/4 * 4 + j / 4)*16 + k + 16*16*2); Link(tn, i*16 + j + 1 + 16*16*3); } } else { s[++tn] = State(i, j, c - 'A' + 1); Link(tn, i*16 + c - 'A' + 1); Link(tn, j*16 + c - 'A' + 1 + 16*16); Link(tn, (i/4 * 4 + j / 4)*16 + c - 'A' + 1 + 16*16*2); Link(tn, i*16 + j + 1 + 16*16*3); } } if(!Dance(0)) puts("No answer"); else { for(int i = ansd - 1; i >= 0; i--) { int sel = ans[i]; in[s[sel].x*16 + s[sel].y] = s[sel].val + 'A' - 1 ; } for(int i = 0; i < 16*16; i++) { putchar(in[i]); if(i % 16 == 15) putchar('\n'); } } } }; DLX dlx; int main() { bool fir = 1;//每组数据间要空行不然会PE... while(scanf("%s", in) != EOF) { //if(strcmp(in, "end") == 0) break; if(!fir) putchar('\n'); fir = 0; for(int i = 1; i < 16; i++) scanf("%s", in + i*16); dlx.solve(); } return 0; }