题意:

数独问题,给你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 精确覆盖相关推荐

  1. HDU 4069 Squiggly Sudoku DLX

    这是昨天周赛的题,我竟然不会怎么判断多解,后来一google,卧槽,我想复杂了......直接看能搜出来几次就行了. 这题就是个变形,先floodfill一下,然后就是模板了 然后发现比大华的快了好几 ...

  2. HDU 4069 Squiggly Sudoku【Dancing Links精确覆盖】

    跟普通的数独有一点点不同,先预处理一下再用Dancing Links进行精确覆盖即可. #include <iostream> #include <cstdio> #inclu ...

  3. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)...

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  4. HDU 4069 Squiggly Sudoku 【DLX+BFS】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 ★这题一开始题都看不懂,后来发现还是挺有意思的 题意: 给你一个9x9的矩阵, 矩阵里面有一些墙 ...

  5. HDU 4069 Squiggly Sudoku Dancing-Links(DLX)+Floodfill

    题目大意:..还是数独,不同的是原先的九宫格约束条件变为了给定的任意形状... 我们跑一遍floodfill 得出每一个格子属于哪一个形状 然后就是裸的数独了 这题T<=2500 绝对不能开动态 ...

  6. [DLX+bfs] hdu 4069 Squiggly Sudoku

    题意: 给你9*9的矩阵.对于每个数字,能减16代表上面有墙,能减32代表下面有墙... 最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了. 然后通过墙会被数字分成9块. 然后做数独,这里 ...

  7. HDOJ 4069 Squiggly Sudoku 精确覆盖+搜索

    //HDOJ 4069 Squiggly Sudoku 精确覆盖+搜索 /* 题意:数独变形 9块弯弯曲曲的区域 每块有9个小格往这81个格子里面填写1-9的数字,使得每行,每列,每个区域都含有1.2 ...

  8. DLX精确覆盖 hdu4069 Squiggly Sudoku

    传送门:点击打开链接 题意:将9*9的棋盘分割成了9个部分,每个部分都是9个格子,然后现在要求每个部分的数字恰是1~9的排列,每一行每一列恰是1~9的排列,问是否有解,有多少组解,如果只有1组解打印出 ...

  9. LA 2659 poj 3076 zoj 3122 Sudoku(精确覆盖 + DLX)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

最新文章

  1. 用fgets()函数以字符串形式读取磁盘文件并输出到屏幕
  2. 世界首条柔性人造触觉神经诞生,有望应用于义肢感触等领域
  3. 【扫盲】小白基础-SDN详解
  4. 登录验证---过滤器(Fileter)
  5. mysql保持开启事件调度器_MySQL 5.1.6开始的事件调度器
  6. E. Sign on Fence(整体二分 + 线段树维护区间最大连续 1 的个数)
  7. 知乎python练手的_Python—爬虫之初级实战项目:爬取知乎任一作者的文章练手
  8. 就业信息网进行服务器维护,服务器安全武汉大学黄石理工学院就业信息网.pptx...
  9. OPPO全球营销总裁沈义人宣布卸任,网友:第二天宣布入职小米?
  10. 我的成长笔记20210402(测试文档编写)
  11. Android APK系列3-------使用platform密钥来给apk文件签名
  12. PHP的日期时间函数date()
  13. matlab条形图颜色矩阵,Matlab条形图 – 根据符号和大小填充不同颜色的条形图
  14. Vue实现仿豆瓣电影
  15. 世界上公认最快的学习法 - 弗曼学习法
  16. 关于“户口,干部身份,职称”等系列问题
  17. 一步到位Composer直接打开SOLIDWORKS贴图
  18. 小雷的冰茶几 3386
  19. markdown 不等于
  20. c语言校友通讯录毕业论文,校友录毕业论文(C_+sql2005).doc

热门文章

  1. Qt——用在ARM板上的Mplayer(1.3.0,1.0rc4)安装编译步骤!还有,Mplayer背景抖动闪烁问题解答,终于来了!
  2. Unity Shader:Unity网格(1)---顶点,三角形朝向,法线,uv,以及双面渲染三角形
  3. Android CardView卡片布局详解(八)
  4. LiTCTF by lingfeng - (crypto全解)
  5. 图划分(Graph PartitionRe-ordering): METIS(5.x)ParMETIS(4.x)使用实践
  6. web界面测试用例(shelley_shu)
  7. 通信应用中数字上变频DUC与数字下变频DDC详细原理(带图)
  8. Java行业薪资待遇一般都多少钱?
  9. 服务器常见状态码以及对应关系
  10. eclipse打开时报错: