本文主要是介绍HDU 4069 Squiggly Sudoku DLX 精确覆盖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
数独问题,给你9个连通块,每个连通块有9个位置。
现在已经有一些数字在上面,让你在空的位置上放数字。
问你是否存在方案,使得每个连通块包含1~9,并且每行每列都有1~9的数字。
输出结果参照样例。
思路:
题中并没有直接给出数独的情况,而是给了一个数值,里面包含了连通块以及是否有数字在该位置的信息。
首先根据所给的数值,bfs把每个连通块都找出来,然后编号。
剩下的,就是套个DLX的模板。(这题和lrj的大白书P410基本一样,可以去参考一下)。
code:
#include <bits/stdc++.h>
using namespace std;const int N = 12;
const int NUM = 9;
typedef long long LL;struct DLX {static const int N = 1e4+5;static const int MAXROW = 3005;int n, sz;int anscnt, ans[MAXROW], ansd;int S[N];int row[N], col[N];int real[MAXROW];int L[N], R[N], U[N], D[N];void init(int 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;anscnt = 0;memset(S, 0, sizeof(S));}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;}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) {for(int i = U[c]; i != c; i = U[i]) {for(int j = L[i];j != i; j = L[j]) {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) {anscnt++; ansd = d;for(int i = 0;i < d; i++)real[i] = ans[i];if(anscnt == 2) return true;else return false;}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[d] = row[i];for(int j = R[i];j != i; j = R[j])remove(col[j]);if(dfs(d+1)) return true;for(int j = L[i];j != i; j = L[j])restore(col[j]);}restore(c);return false;}int solve() {dfs(0);return anscnt;}
}dlx;int a[N][N], p[N][N];
bool vis[N][N];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};const int SLOT = 0;
const int ROW = 1;
const int COL = 2;
const int SUB = 3;void init() {memset(vis, false, sizeof(vis));
}struct PP {int x, y;
};
queue <PP> q;
void bfs(int s, int e, int tag) {q.push((PP){s, e});vis[s][e] = true;p[s][e] = tag;while(!q.empty()) {PP t = q.front(); q.pop();int tmp = a[t.x][t.y];a[t.x][t.y] = tmp&((1<<4)-1);for(int i = 0;i < 4; i++) {bool ca = (tmp>>(i+4)) & 1;int tx = t.x+dir[i][0], ty = t.y+dir[i][1];if(tx >= 1 && tx <= NUM && ty >= 1 && ty <= NUM) {if(!ca && !vis[tx][ty]) {vis[tx][ty] = true;p[tx][ty] = tag;q.push((PP){tx, ty});}}}}
}inline int encode(int a, int b, int c) {return a*81+b*9+c+1;
}
inline void decode(int code, int &a, int &b, int &c) {code--;a = code/81; code %= 81;b = code/9; code %= 9;c = code;
}void putres() {int d = dlx.ansd;int t, p, c;for(int i = 0;i < d; i++) {decode(dlx.real[i], t, p, c);t++, p++, c++;if(a[t][p] != 0 && c != a[t][p]) while(1);a[t][p] = c;}for(int i = 1;i <= NUM; i++) {for(int j = 1;j <= NUM; j++)printf("%d", a[i][j]);puts("");}
}void solve() {vector <int> col;for(int r = 1;r <= NUM; r++) for(int c = 1;c <= NUM; c++) for(int v = 1;v <= 9; v++) {if(a[r][c] == 0 || a[r][c] == v) {col.clear();col.push_back(encode(SLOT, r-1, c-1));col.push_back(encode(ROW, r-1, v-1));col.push_back(encode(COL, c-1, v-1));col.push_back(encode(SUB, p[r][c], v-1));dlx.addRow(encode(r-1, c-1, v-1), col);}}int res = dlx.solve();if(res == 0) puts("No solution");else if(res == 2)puts("Multiple Solutions");else putres();
}int main() {int T, cas = 0;scanf("%d", &T);while(T--) {init();dlx.init(324); //4*NUM*NUM;for(int i = 1;i <= NUM; i++) for(int j = 1;j <= NUM; j++)scanf("%d", &a[i][j]);int cnt = 0; // connecting-sub number, index base 0;for(int i = 1;i <= NUM; i++)for(int j = 1;j <= NUM; j++)if(!vis[i][j]) bfs(i, j, cnt++);printf("Case %d:\n", ++cas);solve();}return 0;
}
这篇关于HDU 4069 Squiggly Sudoku DLX 精确覆盖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!