今天就一直在做DLX的题目了,目前也只是入门而已,就做了几个数独的问题
将数独转化为DLX的模型是很繁琐的一部分。
首先,必须明白,有多少种选择就要建立多少行,有多少个条件约束就要建立多少个列
对于一个9*9的数独,我们可以在81个位置中选择一个地方,放1~9的数,所以选择数目就是9*9*9了,就要建立这么些个行
而约束呢,首先是81个格子,每个格子有且只能有1个数,那么就是9*9个列,然后对于某个位置的某个数,同一行的不能再有一样的数,也是9*9个列,同一列,同一宫都是一个原理,所以总共就是4*9*9个列了
然后问题就转化为了,从9*9*9行中选择某些行,使得每一列都只有1个1,这里的1代表的就是唯一性,比如每一宫中只能有1个1,如果你选的两行致使1个宫内有两个1,那么显然这是不符合约束的,必然在某一列中这个冲突会得到体现。同样的是同一列和同一行。
对于每个格子有且只能有1个数这个条件,也是不可少的。虽说插入的时候只要放置的位置不同,一定不会产生冲突,但是加入此条件就能保证最后输出的结果,每一格都充满了数字。
然后建图的时候就是按着行和列用十字循环链表建图了。
怎么说呢,今天这三道题做的,感觉学了不少东西,但是写起来,却又写挫了,16*16那个数独也不知他们怎么写的,我花了300+ms,前边一堆100ms以内的
首先是2676
/* ID: sdj22251 PROG: inflate LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 9*9*9*9 #define INF 1000000000 #define N 9 #define M 9*9*9+5 using namespace std; int L[MAXN], R[MAXN], C[MAXN], S[M], U[MAXN], D[MAXN], H[M], O[M], X[MAXN]; //X用来存储行,C用来存储列,H用来存储每行中的第一个结点,O用来存储结果 int cnt, head; char mp[N + 5][N + 5], ans[N * N + 5]; bool vis[N * N * 4 + 5]; void link(int r, int c) { S[c]++; C[cnt] = c; X[cnt] = r; U[cnt] = c; D[cnt] = D[c]; U[D[c]] = cnt; D[c] = cnt; if(H[r] == -1) { H[r] = cnt; L[cnt] = R[cnt] = cnt; } else { L[cnt] = H[r]; R[cnt] = R[H[r]]; L[R[H[r]]] = cnt; R[H[r]] = cnt; } cnt++; } void init() { cnt = 0; head = 0; for(int i = 0; i <= N * N * 4; i++) { S[i] = 0; vis[i] = 0; D[i] = U[i] = i; R[i] = (i + 1) % (N * N * 4 + 1); L[i] = (i + N * N * 4) % (N * N * 4 + 1); cnt++; } memset(H, -1, sizeof(H)); } void cal(int &r, int &cx, int &cy, int &ck, int &cg, int i, int j, int k) { r = (i * N + j) * N + k - 1; //代表所属的行 cg = i * N + j + 1; //代表的是数独中i,j位置所属的列 cx = N * N + i * N + k; //代表数独中同一行所属的列 cy = N * N * 2 + j * N + k; //代表数独中同一列所属的列 int LN = (int)sqrt(N * 1.0); ck = N * N * 3 + (i / LN * LN + j / LN) * N + k;//代表数独中同一宫所属的列 } void readdata() { int r, cx, cy, ck, cg; for(int i = 0; i < N; i++) scanf("%s", mp[i]); for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) if(mp[i][j] != '0') { cal(r, cx, cy, ck, cg, i, j, mp[i][j] - '0'); link(r, cx); link(r, cy); link(r, ck); link(r, cg); vis[cx] = vis[cy] = vis[ck] = vis[cg] = 1; } for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) for(int k = 1; k <= N; k++) { cal(r, cx, cy, ck, cg, i, j, k); if(vis[cx] || vis[cy] || vis[ck] || vis[cg]) continue; link(r, cx); link(r, cy); link(r, ck); link(r, cg); } } void removes(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[C[j]]--; } } void resumes(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) { U[D[j]] = j; D[U[j]] = j; S[C[j]]++; } L[R[c]] = c; R[L[c]] = c; } bool dfs(int k) { if(R[head] == head) { for(int i = 0; i < k; i++) //根据所选择的行 能推出选择的位置和数字 ans[X[O[i]] / N] = X[O[i]] % 9 + '1'; for(int i = 0; i < N * N; i++) { printf("%c", ans[i]); if((i + 1) % N == 0) printf("\n"); } return true; } int s = INF, c; for(int i = R[head]; i != head; i = R[i]) if(s > S[i]) { s = S[i]; c = i; } removes(c); for(int i = U[c]; i != c; i = U[i]) { O[k] = i; for(int j = R[i]; j != i; j = R[j]) removes(C[j]); if(dfs(k + 1)) return true; for(int j = L[i]; j != i; j = L[j]) resumes(C[j]); } resumes(c); return false; } int main() { int T; scanf("%d", &T); while(T--) { init(); readdata(); dfs(0); } return 0; }
/* ID: sdj22251 PROG: inflate LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 9*9*9*9 #define INF 1000000000 #define N 9 #define M 9*9*9+5 using namespace std; int L[MAXN], R[MAXN], C[MAXN], S[M], U[MAXN], D[MAXN], H[M], O[M], X[MAXN]; int cnt, head; char mp[N * N + 5], ans[N * N + 5]; bool vis[N * N * 4 + 5]; void link(int r, int c) { S[c]++; C[cnt] = c; X[cnt] = r; U[cnt] = c; D[cnt] = D[c]; U[D[c]] = cnt; D[c] = cnt; if(H[r] == -1) { H[r] = cnt; L[cnt] = R[cnt] = cnt; } else { L[cnt] = H[r]; R[cnt] = R[H[r]]; L[R[H[r]]] = cnt; R[H[r]] = cnt; } cnt++; } void init() { cnt = 0; head = 0; for(int i = 0; i <= N * N * 4; i++) { S[i] = 0; vis[i] = 0; D[i] = U[i] = i; R[i] = (i + 1) % (N * N * 4 + 1); L[i] = (i + N * N * 4) % (N * N * 4 + 1); cnt++; } memset(H, -1, sizeof(H)); } void cal(int &r, int &cx, int &cy, int &ck, int &cg, int i, int j, int k) { r = (i * N + j) * N + k - 1; cg = i * N + j + 1; cx = N * N + i * N + k; cy = N * N * 2 + j * N + k; int LN = (int)sqrt(N * 1.0); ck = N * N * 3 + (i / LN * LN + j / LN) * N + k; } void readdata() { int r, cx, cy, ck, cg; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) if(mp[i * N + j] != '.') { cal(r, cx, cy, ck, cg, i, j, mp[i * N + j] - '0'); link(r, cx); link(r, cy); link(r, ck); link(r, cg); vis[cx] = vis[cy] = vis[ck] = vis[cg] = 1; } for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) if(mp[i * N + j] == '.') for(int k = 1; k <= N; k++) { cal(r, cx, cy, ck, cg, i, j, k); if(vis[cx] || vis[cy] || vis[ck] || vis[cg]) continue; link(r, cx); link(r, cy); link(r, ck); link(r, cg); } } void removes(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[C[j]]--; } } void resumes(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) { U[D[j]] = j; D[U[j]] = j; S[C[j]]++; } L[R[c]] = c; R[L[c]] = c; } bool dfs(int k) { if(R[head] == head) { for(int i = 0; i < k; i++) ans[X[O[i]] / N] = X[O[i]] % N + '1'; for(int i = 0; i < N * N; i++) { printf("%c", ans[i]); } printf("\n"); return true; } int s = INF, c; for(int i = R[head]; i != head; i = R[i]) if(s > S[i]) { s = S[i]; c = i; } removes(c); for(int i = U[c]; i != c; i = U[i]) { O[k] = i; for(int j = R[i]; j != i; j = R[j]) removes(C[j]); if(dfs(k + 1)) return true; for(int j = L[i]; j != i; j = L[j]) resumes(C[j]); } resumes(c); return false; } int main() { while(scanf("%s", mp) != EOF && mp[0] != 'e') { init(); readdata(); dfs(0); } return 0; }
这是16*16的数独
/* ID: sdj22251 PROG: inflate LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 16*16*16*16+16*16*4 #define INF 1000000000 #define N 16 #define M 16*16*16+5 using namespace std; int L[MAXN], R[MAXN], C[MAXN], S[M], U[MAXN], D[MAXN], H[M], O[M], X[MAXN]; int cnt, head; char mp[N + 5][N + 5], ans[N * N + 5]; bool vis[N * N * 4 + 5]; void link(int r, int c) { S[c]++; C[cnt] = c; X[cnt] = r; U[cnt] = c; D[cnt] = D[c]; U[D[c]] = cnt; D[c] = cnt; if(H[r] == -1) { H[r] = cnt; L[cnt] = R[cnt] = cnt; } else { L[cnt] = H[r]; R[cnt] = R[H[r]]; L[R[H[r]]] = cnt; R[H[r]] = cnt; } cnt++; } void init() { cnt = 0; head = 0; for(int i = 0; i <= N * N * 4; i++) { S[i] = 0; vis[i] = 0; D[i] = U[i] = i; R[i] = (i + 1) % (N * N * 4 + 1); L[i] = (i + N * N * 4) % (N * N * 4 + 1); cnt++; } memset(H, -1, sizeof(H)); } void cal(int &r, int &cx, int &cy, int &ck, int &cg, int i, int j, int k) { r = (i * N + j) * N + k - 1; cg = i * N + j + 1; cx = N * N + i * N + k; cy = N * N * 2 + j * N + k; int LN = (int)sqrt(N * 1.0); ck = N * N * 3 + (i / LN * LN + j / LN) * N + k; } void readdata() { int r, cx, cy, ck, cg; for(int i = 1; i < N; i++) scanf("%s", mp[i]); for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) if(mp[i][j] != '-') { cal(r, cx, cy, ck, cg, i, j, mp[i][j] - 'A' + 1); link(r, cx); link(r, cy); link(r, ck); link(r, cg); vis[cx] = vis[cy] = vis[ck] = vis[cg] = 1; } for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) if(mp[i][j] == '-') for(int k = 1; k <= N; k++) { cal(r, cx, cy, ck, cg, i, j, k); if(vis[cx] || vis[cy] || vis[ck] || vis[cg]) continue; link(r, cx); link(r, cy); link(r, ck); link(r, cg); } } void removes(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[C[j]]--; } } void resumes(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) { U[D[j]] = j; D[U[j]] = j; S[C[j]]++; } L[R[c]] = c; R[L[c]] = c; } bool dfs(int k) { if(R[head] == head) { for(int i = 0; i < k; i++) ans[X[O[i]] / N] = X[O[i]] % N + 'A'; for(int i = 0; i < N * N; i++) { printf("%c", ans[i]); if((i + 1) % N == 0) printf("\n"); } printf("\n"); return true; } int s = INF, c; for(int i = R[head]; i != head; i = R[i]) if(s > S[i]) { s = S[i]; c = i; } removes(c); for(int i = U[c]; i != c; i = U[i]) { O[k] = i; for(int j = R[i]; j != i; j = R[j]) removes(C[j]); if(dfs(k + 1)) return true; for(int j = L[i]; j != i; j = L[j]) resumes(C[j]); } resumes(c); return false; } int main() { while(scanf("%s", mp[0]) != EOF) { init(); readdata(); dfs(0); } return 0; }