题目大意:
经典数独, 9*9
每行每列每个九宫格都有1~9, 多解输出任意一解
大致思路:
没什么好说的数独了....和POJ 3074一样..
代码如下:
Result : Accepted Memory : 216 KB Time : 0 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; char in[800]; struct DLX { #define maxn 9*9*9 + 10 #define maxm 9*9*4 + 10 #define maxnode 9*9*9*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 = 9*9*4; int len = strlen(in); for(int i = 0; i < len; i++) if(in[i] == '0') tn += 9; else tn++; init(tn, tm); tn = 0; for(int i = 0; i < 9; i++) for(int j = 0; j < 9; j++) { char c = in[i*9 + j]; if(c == '0') { for(int k = 1; k <= 9; k++) { s[++tn] = State(i, j, k); Link(tn, i*9 + k); Link(tn, j*9 + k + 9*9); Link(tn, (i/3 * 3 + j / 3)*9 + k + 9*9*2); Link(tn, i*9 + j + 1 + 9*9*3); } } else { s[++tn] = State(i, j, c - '0'); Link(tn, i*9 + c - '0'); Link(tn, j*9 + c - '0' + 9*9); Link(tn, (i/3 * 3 + j / 3)*9 + c - '0' + 9*9*2); Link(tn, i*9 + j + 1 + 9*9*3); } } if(!Dance(0)) puts("No answer"); else { for(int i = ansd - 1; i >= 0; i--) { int sel = ans[i]; in[s[sel].x*9 + s[sel].y] = s[sel].val + '0'; } for(int i = 0; i < 9*9; i++) { putchar(in[i]); if(i % 9 == 8) putchar('\n'); } } } }; DLX dlx; int main() { int T; scanf("%d", &T); while(T--) { for(int i = 0; i < 9; i++) scanf("%s", in + i*9); dlx.solve(); } return 0; }