HDU4069-Squiggly Sudoku
来源:互联网 发布:优化布林线 编辑:程序博客网 时间:2024/06/11 00:17
Squiggly Sudoku
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1973 Accepted Submission(s): 825
Problem Description
Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each column, each row, and each of the nine Connecting-sub-grids that compose the grid contains all of the digits from 1 to 9.
Left figure is the puzzle and right figure is one solution.
Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
Left figure is the puzzle and right figure is one solution.
Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
Input
The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.
Each case contains nine lines, Each line contains nine integers.
Each module number tells the information of the gird and is the sum of up to five integers:
0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.
16: wall to the up
32: wall to the right
64: wall to the down
128: wall to the left
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
Each case contains nine lines, Each line contains nine integers.
Each module number tells the information of the gird and is the sum of up to five integers:
0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.
16: wall to the up
32: wall to the right
64: wall to the down
128: wall to the left
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
Output
For each case, if there are Multiple Solutions or no solution just output "Multiple Solutions" or "No solution". Else output the exclusive solution.(as shown in the sample output)
Sample Input
3144 18 112 208 80 25 54 144 48135 38 147 80 121 128 97 130 32137 32 160 144 114 167 208 0 32192 100 160 160 208 96 183 192 101209 80 39 192 86 48 136 80 114152 48 226 144 112 160 160 149 48128 0 112 166 215 96 160 128 41128 39 153 32 209 80 101 136 35192 96 200 67 80 112 208 68 96 144 48 144 81 81 16 53 144 48128 96 224 144 48 128 103 128 38163 208 80 0 37 224 209 0 32135 48 176 192 64 112 176 192 104192 101 128 89 80 82 32 150 48149 48 224 208 16 48 224 192 33128 0 114 176 135 0 80 112 169137 32 148 32 192 96 176 144 32192 96 193 64 80 80 96 192 96144 88 48 217 16 16 80 112 176224 176 129 48 128 40 208 16 37145 32 128 96 196 96 176 136 32192 32 227 176 144 80 96 192 32176 192 80 98 160 145 80 48 224128 48 144 80 96 224 183 128 48128 36 224 144 51 144 32 128 105131 64 112 136 32 192 36 224 176224 208 80 64 64 116 192 83 96
Sample Output
Case 1:521439678763895124984527361346182795157964832812743956235678419479216583698351247Case 2:No solutionCase 3:Multiple Solutions
Source
The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest
Recommend
lcy
题意:也是数独问题,只是它是要求不规则的九个格子中要有1~9
解题思路:因为是不规则的九个格子,所以要先进行dfs,找出哪些格子在一个块中,其他操作和舞蹈链解决普通数独一样
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cctype> #include <map> #include <cmath> #include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional> using namespace std;#define LL long long const int INF = 0x3f3f3f3f;const int maxn = 300005;int n, m, a[20][20], tot, p[1000][3];int dir[20][20][4], id[20][20], res, sz;struct DLX{int L[maxn], R[maxn], U[maxn], D[maxn];int row[maxn], col[maxn], ans[maxn], sum[maxn];int n, m, num, cnt;void add(int k, int l, int r, int u, int d, int x, int y){L[k] = l; R[k] = r; U[k] = u;D[k] = d; row[k] = x; col[k] = y;}void reset(int n, int m){this->n = n; this->m = m;for (int i = 0; i <= m; i++){add(i, i - 1, i + 1, i, i, 0, i);sum[i] = 0;}L[0] = m, R[m] = 0, cnt = m + 1;}void insert(int x, int y){int temp = cnt - 1;if (row[temp] != x){add(cnt, cnt, cnt, U[y], y, x, y);U[D[cnt]] = cnt; D[U[cnt]] = cnt;}else{add(cnt, temp, R[temp], U[y], y, x, y);R[L[cnt]] = cnt; L[R[cnt]] = cnt;U[D[cnt]] = cnt; D[U[cnt]] = cnt;}sum[y]++, cnt++;}void remove(int k){R[L[k]] = R[k], L[R[k]] = L[k];for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = D[j];U[D[j]] = U[j];sum[col[j]]--;}}void resume(int k){R[L[k]] = k, L[R[k]] = k;for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = j;U[D[j]] = j;sum[col[j]]++;}}void dfs(int k){if (!R[0]){num = k; sz++;if (sz == 1){for (int i = 0; i < num; i++)a[p[ans[i]][0]][p[ans[i]][1]] = p[ans[i]][2];}return;}int now = R[0];for (int i = now; i != 0; i = R[i])if (sum[now] > sum[i]) now = i;remove(now);for (int i = D[now]; i != now; i = D[i]){ans[k] = row[i];for (int j = R[i]; j != i; j = R[j]) remove(col[j]);dfs(k + 1);if (sz >= 2) return;for (int j = L[i]; j != i; j = L[j]) resume(col[j]);}resume(now);}void display(){for (int i = 1; i <= 9; i++)for (int j = 1; j <= 9; j++)printf("%d%s", a[i][j], j == 9 ? "\n" : "");}}dlx;void insert(int x, int y, int z){p[++tot][0] = x, p[tot][1] = y, p[tot][2] = z;dlx.insert(tot, 9 * (x - 1) + y);dlx.insert(tot, 81 + 9 * (x - 1) + z);dlx.insert(tot, 162 + 9 * (y - 1) + z);dlx.insert(tot, 243 + 9 * (id[x][y] - 1) + z);}void check(int x, int y, int &z){if (z >= 128) z -= 128, dir[x][y][0] = 1;if (z >= 64) z -= 64, dir[x][y][1] = 1;if (z >= 32) z -= 32, dir[x][y][2] = 1;if (z >= 16) z -= 16, dir[x][y][3] = 1;}void dfs(int x, int y, int z){if (id[x][y]) return;id[x][y] = z;if (!dir[x][y][0]) dfs(x, y - 1, z);if (!dir[x][y][1]) dfs(x + 1, y, z);if (!dir[x][y][2]) dfs(x, y + 1, z);if (!dir[x][y][3]) dfs(x - 1, y, z);}int main(){int t, cas = 0;scanf("%d", &t);while (t--){tot = sz = (res = 1) - 1;dlx.reset(729, 324);memset(dir, 0, sizeof dir);memset(id, 0, sizeof id);for (int i = 1; i <= 9; i++)for (int j = 1; j <= 9; j++)scanf("%d", &a[i][j]), check(i, j, a[i][j]);for (int i = 1; i <= 9; i++)for (int j = 1; j <= 9; j++)if (!id[i][j]) dfs(i, j, res++);for (int i = 1; i <= 9; i++){for (int j = 1; j <= 9; j++){if (a[i][j]) insert(i, j, a[i][j]);else{for (int k = 1; k <= 9; k++)insert(i, j, k);}}}dlx.dfs(0);printf("Case %d:\n", ++cas);if (!sz) printf("No solution\n");else if (sz == 2) printf("Multiple Solutions\n");else dlx.display();}return 0;}
阅读全文
0 0
- hdu4069 Squiggly Sudoku
- HDU4069-Squiggly Sudoku
- DLX精确覆盖 hdu4069 Squiggly Sudoku
- hdu 4069 squiggly sudoku
- HDU 4069 Squiggly Sudoku
- HDU 4069 Squiggly Sudoku DLX
- hdu4069
- HDOJ 4069 Squiggly Sudoku 精确覆盖+搜索
- HDU 4069 Squiggly Sudoku DLX 精确覆盖
- [DLX+bfs] hdu 4069 Squiggly Sudoku
- 【dfs预处理+DLX】hdu 4069 Squiggly Sudoku
- HDU 4069 Squiggly Sudoku 数独DLX 福州网络赛
- HDU 4069 Squiggly Sudoku【Dancing Links精确覆盖】
- HDU 4069 Squiggly Sudoku Dancing-Links(DLX)+Floodfill
- hdu 4069 Squiggly Sudoku——11年福州网络赛DLX
- HDU4069(未AC)
- DLX(变形数独)hdu4069
- Sudoku
- 3Sum
- Linux网络编程--(1)网络体系架构
- Kafka LEADER_NOT_AVAILABLE问题解决
- HTTP请求行、请求头、请求体详解
- 多个按钮调用同一个方法
- HDU4069-Squiggly Sudoku
- 超前-滞后型DPLL提取位同步时钟的FPGA实现
- LeetCode 452 射气球问题
- SSL2810 2017年10月30日提高组T2 数论(math)
- MapReduce MapTask任务数量,切片大小笔记
- Leetcode 645 FindErrorNum
- Mycat安装部署简单使用
- Okhttp带证书封装
- Android视频编辑器(四)通过OpenGL给视频增加不同滤镜效果