【题目描述】
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.
今天我们玩一个异性数独,目标是用数字填充9*9网格,这样每一列、每一行和组成网格的九个连接子网格中的每一个都包含从1到9之间的所有数字。左边的图形是谜题,右边的图形是一个解决方案。

Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
现在,给你拼图的信息,请告诉我,是没有解决方案,还是多个方案或是一个解决方案。

【输入】
The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.
第一行是一个整数T(1<=T<=2500),表示样例个数,后面T块每个表示一个样例。
Each case contains nine lines, Each line contains nine integers.
每组样例包含9行,每行9个数字。
Each module number tells the information of the gird and is the sum of up to five integers:
每个模数表示网格信息,是以下5个数字之和:
0~9: ‘0’ means this gird is empty, ‘1’ - ‘9’ means the gird is already filled in.
0~9:‘0’表示网格是空的,‘1’-‘9’表示网格被填了数字
16: wall to the up
16:最上面的墙
32: wall to the right
32:最右边的墙
64: wall to the down
64:最下面的墙
128: wall to the left
128:最左边的墙
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
保证一定有九个连接子网格,每个都有九个网格。

【输出】
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)
对于每个样例,如果有多个解或没有解就只输出"Multiple Solutions"或"No solution"。否则输出唯一的解决办法。(像样例输出一样)

【样例输入】
3
144 18 112 208 80 25 54 144 48
135 38 147 80 121 128 97 130 32
137 32 160 144 114 167 208 0 32
192 100 160 160 208 96 183 192 101
209 80 39 192 86 48 136 80 114
152 48 226 144 112 160 160 149 48
128 0 112 166 215 96 160 128 41
128 39 153 32 209 80 101 136 35
192 96 200 67 80 112 208 68 96

144 48 144 81 81 16 53 144 48
128 96 224 144 48 128 103 128 38
163 208 80 0 37 224 209 0 32
135 48 176 192 64 112 176 192 104
192 101 128 89 80 82 32 150 48
149 48 224 208 16 48 224 192 33
128 0 114 176 135 0 80 112 169
137 32 148 32 192 96 176 144 32
192 96 193 64 80 80 96 192 96

144 88 48 217 16 16 80 112 176
224 176 129 48 128 40 208 16 37
145 32 128 96 196 96 176 136 32
192 32 227 176 144 80 96 192 32
176 192 80 98 160 145 80 48 224
128 48 144 80 96 224 183 128 48
128 36 224 144 51 144 32 128 105
131 64 112 136 32 192 36 224 176
224 208 80 64 64 116 192 83 96

【样例输出】
Case 1:
521439678
763895124
984527361
346182795
157964832
812743956
235678419
479216583
698351247
Case 2:
No solution
Case 3:
Multiple Solutions

题目链接:https://cn.vjudge.net/problem/HDU-4069

POJ3074的升级版本,多了个DFS建图的过程
这个见图自己写起来觉得好麻烦,看了别人的建图怎么这么简单。。。怎么这么有道理。。。
因为要考虑多解和无解情况,有几个细节特别容易WA

代码如下:

#include <iostream>
using namespace std;
static const int MAXN = 9 * 9 * 9 + 10;
static const int MAXM = 9 * 9 * 4 + 10;
static const int MAXNODE = MAXN * MAXM;
int sud[15][15], vis[15][15];
int num;
struct DLX {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];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 d){if (R[0] == 0){num++;ansd=d;if(num>=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]){if(num==0)ans[d] = Row[i];for (int j = R[i]; j != i; j = R[j])remove(Col[j]);if (Dance(d + 1))return true;for (int j = L[i]; j != i; j = L[j])resume(Col[j]);}resume(c);return false;}
};
void dfs(int x, int y, int k)
{if (x < 0 || x >= 9 || y < 0 || y >= 9 || vis[x][y] >= 0)return;vis[x][y] = k;if (!(sud[x][y] & 16))dfs(x - 1, y, k);if (!(sud[x][y] & 32))dfs(x, y + 1, k);if (!(sud[x][y] & 64))dfs(x + 1, y, k);if (!(sud[x][y] & 128))dfs(x, y - 1, k);return;
}
DLX dlx;
int main()
{std::ios::sync_with_stdio(false);std::cin.tie(0), cout.tie(0);int T;cin >> T;for (int kase = 1; kase <= T; kase++){for (int i = 0; i < 9; i++)for (int j = 0; j < 9; j++)cin >> sud[i][j];int cnt = 0;for (int i = 0; i < 9; i++)for (int j = 0; j < 9; j++)vis[i][j] = -1;for (int i = 0; i < 9; i++)for (int j = 0; j < 9; j++){if (vis[i][j] >= 0)continue;dfs(i, j, cnt);cnt++;}dlx.init(9 * 9 * 9, 9 * 9 * 4);for (int i = 0; i < 9; i++)for (int j = 0; j < 9; j++)for (int k = 1; k <= 9; k++){if (sud[i][j] % 16 == 0 || sud[i][j] % 16 == k){dlx.Link((i * 9 + j) * 9 + k, i * 9 + j + 1);dlx.Link((i * 9 + j) * 9 + k, 9 * 9 + i * 9 + k);dlx.Link((i * 9 + j) * 9 + k, 9 * 9 * 2 + j * 9 + k);dlx.Link((i * 9 + j) * 9 + k, 9 * 9 * 3 + vis[i][j] * 9 + k);}}num=0;dlx.Dance(0);cout << "Case " << kase << ":" << endl;if (num == 0)cout << "No solution" << endl;else if (num >= 2)cout << "Multiple Solutions" << endl;else{for (int i = 0; i < dlx.ansd; i++)sud[(dlx.ans[i] - 1) / 9 / 9][(dlx.ans[i] - 1) / 9 % 9] = (dlx.ans[i] - 1) % 9 + 1;for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++)cout << sud[i][j];cout << endl;}}}return 0;
}

[kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】相关推荐

  1. Kuangbin专题三Dancing Links

    Kuangbin专题三Dancing Links 没写完所有的,因为要去上课了赶紧先预习一下,这就先发出来吧. 跳舞链这东西以前在hihocoder上翻到过,当时看的模模糊糊的,现在好好学一学. 暂时 ...

  2. DLX (Dancing Links/舞蹈链)算法——求解精确覆盖问题

    精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 例如:如下的矩阵 就包含了这样一个集合(第1.4.5行) 如何利用给定的矩阵求出相应的行的集合 ...

  3. dancing links(舞蹈链)——求解精准覆盖及重复覆盖问题

    以下转自:https://blog.csdn.net/the_star_is_at/article/details/53425736 问题描述: 给定一个n*m的矩阵,有些位置为1,有些位置为0.如果 ...

  4. HDU 4398 whosyourdaddy 精确覆盖,允许重复覆盖

    题目大意:有n个点,其中一些点是相连的.领主的攻击具有溅射,即攻击一个点,此点相邻的点也会收到攻击.问,领主最少攻击多少次,使得每个点都至少被攻击一次. 与传统精确覆盖相比,此题允许重复覆盖.那么,我 ...

  5. 老鱼的-kuangbin专题题解

    kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...

  6. dancing links x(舞蹈链算法)详解

    dancing links x 详解 大佬万仓一黍的blog 夜深人静写算法(九)- Dancing Links X(跳舞链) 精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合, ...

  7. 算法帖——用舞蹈链算法(Dancing Links)求解俄罗斯方块覆盖问题

    问题的提出:如下图,用13块俄罗斯方块覆盖8*8的正方形.如何用计算机求解? 解决这类问题的方法不一而足,然而核心思想都是穷举法,不同的方法仅仅是对穷举法进行了优化 用13块不同形状的俄罗斯方块(每个 ...

  8. kuangbin 专题一 简单搜索

    kuangbin 专题一 简单搜索 1.POJ1321棋盘问题[DFS] 代码 自己的想法 2.POJ2251Dungeon Master[三维空间BFS] 代码 自己的想法 3.POJ3278 Ca ...

  9. 双向循环链表、dancing links

    目录 双向循环链表 力扣 426. 将二叉搜索树转化为排序的双向链表 十字交叉双向循环链表(dancing links) 精确覆盖问题 dancing links X算法(V1递归版) POJ 374 ...

最新文章

  1. 计算机科学班(原acm班),计算机科学创新实验班(以下简称ACM班)培养计划.doc
  2. aes算法的地位_aes算法最后一轮为什么没有列混淆?
  3. 嵌入式Linux文件提取,嵌入式 Linux系统编程(四)——文件属性
  4. 使用OData API批量删除Marketing Cloud里的contact
  5. html5游戏开发box2djs,Box2D.js简易示例
  6. 自己常用的C/C++小技巧
  7. 【技术博客】基于JsPlumb和JQuery-UI的流程图的保存和再生成
  8. windows脚本编制引擎_从零开始的场景编辑器(二):脚本系统
  9. User Agent跨站攻击
  10. 监控mysql的存储引擎
  11. 特斯拉为什么要“干掉”保险丝和继电器?
  12. matlab实现色彩迁移,图像的色彩风格迁移
  13. html写一个轮播图响应式布局,响应式banner图片轮播布局代码
  14. 使用Tiled编辑铁锈战争自定义地图
  15. java基础扫盲_Java学习:扫盲
  16. python如何把矩阵转换为图片_如何将numpy数组转换为(并显示)图片
  17. python爬取站酷海洛图片_站酷海洛图片爬取
  18. 从大数据应用案例中理解大数据的应用价值
  19. 送给电路设计新人:PCB经典设计流程
  20. 【转载】周易大象传原文及译文

热门文章

  1. 《证券投资基金投资流通受限股票估值指引(试行)》解读
  2. Linux 运维安全策略(一)
  3. Elasticsearch DSL语法中queries/filters执行顺序探秘
  4. 欧拉回路(简单判断是否有欧拉回路存在)
  5. 光谱分布、光谱辐射通量密度与不同时间段分布光谱(图示)
  6. 在Javascript 中的Base64加密,支持中文加密及emoji表情的unicode编码的base64加密
  7. ML之VC维:VC维(Vapnik-Chervonenkis Dimension)理论的概述(衡量模型复杂度和预测能力的指标)的简介、案例理解之详细攻略
  8. 使用IIS部署网站步骤
  9. ssm项目——CRM客户管理系统开发准备
  10. Linux之命令scp远程拷贝文件