DLX精确覆盖 hdu4069 Squiggly Sudoku
来源:互联网 发布:ca检测不到网络 编辑:程序博客网 时间:2024/05/20 06:26
传送门:点击打开链接
题意:将9*9的棋盘分割成了9个部分,每个部分都是9个格子,然后现在要求每个部分的数字恰是1~9的排列,每一行每一列恰是1~9的排列,问是否有解,有多少组解,如果只有1组解打印出来
思路:先通过DFS求出所在的部分,然后剩下的和DLX精确覆盖求数独就是一样的了
#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<cstdio>#include<cctype>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define fuck printf("fuck")#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w+",stdout)using namespace std;typedef long long LL;const int MS = 10 + 5;const int MX = 1000 + 5;const int MN = 300000 + 5;const int INF = 0x3f3f3f3f;int vis[3][MS][MS], Z[MS][MS], r;int A[MS][MS], W[MS][MS], pre[MS][MS];struct DLX { int m, n; int H[MX], S[MX], ans; int Row[MN], Col[MN], rear; int L[MN], R[MN], U[MN], D[MN]; void Init(int _m, int _n) { m = _m; n = _n; rear = n; ans = 0; for(int i = 0; i <= n; i++) { S[i] = 0; L[i] = i - 1; R[i] = i + 1; U[i] = D[i] = i; } L[0] = n; R[n] = 0; for(int i = 1; i <= m; i++) { H[i] = -1; } } void Link(int r, int c) { int rt = ++rear; Row[rt] = r; Col[rt] = c; S[c]++; D[rt] = D[c]; U[D[c]] = rt; U[rt] = c; D[c] = rt; if(H[r] == -1) { H[r] = L[rt] = R[rt] = rt; } else { int id = H[r]; R[rt] = R[id]; L[R[id]] = rt; L[rt] = id; R[id] = rt; } } void Remove(int c) { R[L[c]] = R[c]; L[R[c]] = L[c]; for(int i = D[c]; i != c; i = D[i]) { for(int j = R[i]; j != i; j = R[j]) { D[U[j]] = D[j]; U[D[j]] = U[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]) { D[U[j]] = U[D[j]] = j; S[Col[j]]++; } } R[L[c]] = L[R[c]] = c; } bool Dance(int cnt) { if(R[0] == 0) { for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { if(pre[i][j]) W[i][j] = pre[i][j]; } } ans++; if(ans >= 2) return true; 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]) { for(int j = R[i]; j != i; j = R[j]) Remove(Col[j]); int r = Row[i]; pre[(r - 1) / 81 + 1][((r - 1) % 81) / 9 + 1] = ((r - 1) % 81) % 9 + 1; if(Dance(cnt + 1)) return true; for(int j = L[i]; j != i; j = L[j]) Resume(Col[j]); } Resume(c); return false; }} G;void Link(int x, int y, int z, int k) { int id = ((x - 1) * 9 + y - 1) * 9 + k; G.Link(id, (x - 1) * 9 + y); G.Link(id, 9 * 9 + (x - 1) * 9 + k); G.Link(id, 2 * 9 * 9 + (y - 1) * 9 + k); G.Link(id, 3 * 9 * 9 + (z - 1) * 9 + k);}void DFS(int x, int y, int id) { int w = A[x][y]; Z[x][y] = id; if(w >= 128) w -= 128; else if(y - 1 >= 1 && !Z[x][y - 1]) DFS(x, y - 1, id); if(w >= 64) w -= 64; else if(x + 1 <= 9 && !Z[x + 1][y]) DFS(x + 1, y, id); if(w >= 32) w -= 32; else if(y + 1 <= 9 && !Z[x][y + 1]) DFS(x, y + 1, id); if(w >= 16) w -= 16; else if(x - 1 >= 1 && !Z[x - 1][y]) DFS(x - 1, y, id); W[x][y] = w; vis[0][x][w] = vis[1][y][w] = vis[2][id][w] = 1;}int main() { int T, ansk = 0; //FIN; scanf("%d", &T); while(T--) { r = 0; memset(Z, 0, sizeof(Z)); memset(pre, 0, sizeof(pre)); memset(vis, 0, sizeof(vis)); G.Init(9 * 9 * 9, 4 * 9 * 9); for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { scanf("%d", &A[i][j]); } } for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { if(!Z[i][j]) DFS(i, j, ++r); } } for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { if(W[i][j]) Link(i, j, Z[i][j], W[i][j]); else for(int k = 1; k <= 9; k++) { int z = Z[i][j]; if(!vis[0][i][k] && !vis[1][j][k] && !vis[2][z][k]) { Link(i, j, z, k); } } } } G.Dance(0); printf("Case %d:\n", ++ansk); if(G.ans == 0) printf("No solution\n"); else if(G.ans == 2) printf("Multiple Solutions\n"); else for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { printf("%d", W[i][j]); } printf("\n"); } } return 0;}
0 0
- DLX精确覆盖 hdu4069 Squiggly Sudoku
- HDU 4069 Squiggly Sudoku DLX 精确覆盖
- hdu4069 Squiggly Sudoku
- HDU4069-Squiggly Sudoku
- HDOJ 4069 Squiggly Sudoku 精确覆盖+搜索
- DLX精确覆盖 poj2676 Sudoku
- HDU 4069 Squiggly Sudoku【Dancing Links精确覆盖】
- HDU 4069 Squiggly Sudoku DLX
- poj 3074 Sudoku (精确覆盖,DLX,搜索)
- DLX(精确覆盖) POJ 3074 Sudoku
- POJ 3074 Sudoku DLX精确覆盖
- POJ 3076 Sudoku DLX精确覆盖
- POJ 3074 Sudoku(DLX+精确覆盖)
- POJ 3076 Sudoku 精确覆盖问题DLX
- [DLX+bfs] hdu 4069 Squiggly Sudoku
- 【dfs预处理+DLX】hdu 4069 Squiggly Sudoku
- POJ 3074 Sudoku 转化精确覆盖问题DLX
- HDU 3909 Sudoku(数独转DLX精确覆盖)
- 一个数组中只有0,1,2三个元素,进行排序,要求时间复杂度为O(n)
- Android学习笔记(十一)BroadcastReceiver动态注册、注销示例
- 关于sqlite3 not found
- KL距离的应用一(转)
- mongoDB高级篇①】聚集运算之group,aggregate
- DLX精确覆盖 hdu4069 Squiggly Sudoku
- 读写锁
- stack栈的基本操作
- lintcode做题总结, Oct 05
- Java/C++之 public、protected、private ; virtual & abstract
- PS之1.光与色的关系(1)
- Swift 与众不同的地方
- 【leetcode】102. Binary Tree Level Order Traversal
- JVM 垃圾收集器及GC参数