UVALive 2659 数独 DLX模板
建图:
从1到16枚举所有的行、列上放的数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = ;
const int MAXNODE = ;
const int MAXR = ; struct DLX {
// 行编号从1开始,列编号为1~n,结点0是表头结点;结点1~n是各列顶部的虚拟结点
int n, sz; // 列数,结点总数
int S[MAXN]; //各列结点数 int row[MAXNODE], col[MAXNODE]; //各结点行列编号
int L[MAXNODE], R[MAXNODE], U[MAXNODE], D[MAXNODE]; //十字链表 int ansd, ans[MAXR]; // 解 void init(int n) { //n是列数
this->n = n; //虚拟结点
for (int i = ; i <= n; i++) {
U[i] = i; D[i] = i; L[i] = i-; R[i] = i+;
}
R[n] = ; L[] = n;
sz = n+;
memset(S, , sizeof(S));
} void addRow(int r, vector<int> columns) {
//这一行的第一个结点
//行没有设头结点,每一行连成一个环形
int first = sz;
for (int i = ; i < columns.size(); i++) {
int c = columns[i];
L[sz] = sz-; R[sz] = sz+; 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-] = first; L[first] = sz-;
} //顺着链表A,遍历s外的其他元素
#define FOR(i, A, s) for (int i = A[s]; i != s; i = A[i]) void remove(int c) { //删除c列
L[R[c]] = L[c]; R[L[c]] = R[c];
for (int i = D[c]; i != c; i = D[i]) // 对于每一个c列不为0的所有行
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) { //回连c列
for (int i = U[c]; i != c; i = U[i])
for (int j = L[i]; j != i; j = L[j]) {
U[D[j]] = j; D[U[j]] = j; S[col[j]]++;
}
L[R[c]] = c; R[L[c]] = c;
} bool dfs(int d) { //d为递归深度
if (R[] == ) { //找到解
ansd = d; //记录解的长度
return true;
} //找S最小的C列
int c = R[]; //第一个未删除的列
for (int i = R[]; i != ; i = R[i]) if (S[i]<S[c]) c = i; remove(c); //删除第c列
for (int i = D[c]; i != c; i = D[i]) { //用结点i所在的行覆盖第c列
ans[d] = row[i];
for (int j = R[i]; j != i; j = R[j]) remove(col[j]); //删除节结点i所在行覆盖第c列
if (dfs(d+)) return true;
for (int j = L[i]; j != i; j = L[j]) restore(col[j]); // 恢复
}
restore(c); //恢复
return false;
} bool solve(vector<int> &v) {
v.clear();
if(!dfs()) return false;
for (int i = ; i < ansd; i++) v.push_back(ans[i]);
return true;
}
}; const int SLOT = ;
const int ROW = ;
const int COL = ;
const int SUB = ; inline int encode(int a, int b, int c) {
return a* + b* + c + ;
} inline void decode(int code, int &a, int &b, int &c) {
code--;
c = code%; code /= ;
b = code%; code /= ;
a = code;
} char puzzle[][]; bool read() {
for (int i = ; i < ; i++)
if (scanf("%s", puzzle[i])!=) return false;
return true;
} DLX solver; int main() {
#ifdef Phantom01
freopen("LA2659.txt", "r", stdin);
#endif //Phantom01 int T= ; while (read()) {
if (T!=) puts(""); T++;
solver.init();
for (int r = ; r < ; r++)
for (int c = ; c < ; c++)
for (int v = ; v < ; v++)
if (puzzle[r][c]=='-'|| puzzle[r][c]=='A'+v) {
vector<int> columns;
columns.push_back(encode(SLOT, r, c));
columns.push_back(encode(ROW, r, v));
columns.push_back(encode(COL, c, v));
columns.push_back(encode(SUB, (r/)*+c/, v));
solver.addRow(encode(r, c, v), columns);
}
vector<int> ans;
if (!solver.solve(ans)) while () ; for (int i = ; i < ans.size(); i++) {
int r, c, v;
decode(ans[i], r, c, v);
puzzle[r][c] = 'A'+v;
}
for (int i = ; i < ; i++) {
printf("%s\n", puzzle[i]);
}
} return ;
}
UVALive 2659 数独 DLX模板的更多相关文章
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
- hdu 5046 二分+DLX模板
http://acm.hdu.edu.cn/showproblem.php?pid=5046 n城市建k机场使得,是每个城市最近机场的距离的最大值最小化 二分+DLX 模板题 #include < ...
- [ACM] POJ 3740 Easy Finding (DLX模板题)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 4343 Des ...
- DLX模板
对于数独问题 ; //3*3数独 ; // 一格能填9个数 9*9格 +; // 9*9*4=(9+9+9)*9+9*9 (9+9+9)是9行 9列 9格 *9是9个数 9*9是81个格子 +MaxM ...
- [ACM] HUST 1017 Exact cover (Dancing Links,DLX模板题)
DESCRIPTION There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- Poj3074-Sudoku(数独DLX)
题意: 给出一个9*9的矩阵,有一些格子已经填了数,有一些是.代表未填.求任意一组解使得每行包含1~9,每列包含1~9,每个小矩形(3*3)包含1~9. 解析: 精确覆盖DLX的经典题目,每一行代表要 ...
- 51 nod 1211 数独 DLX
原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1211 调了挺久的,自己的一份舞蹈链模板…… 算是在网上见到的模 ...
- 舞蹈链 DLX
欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 舞蹈链是一个非常玄学的东西…… 问题模型 精确覆盖问题:在一个01矩阵中,是否可以选出一些行的集合,使得在这些行的集 ...
- 【UVA1309】Sudoku(DLX)
点此看题面 大致题意: 让你填完整一个\(16*16\)的数独. 解题思路 我们知道,数独问题显然可以用\(DLX\)解决. 考虑对于一个数独,它要满足的要求为:每个位置都必须有数,每一行都必须有全部 ...
随机推荐
- CSS3的常用属性(二)
边框 边框圆角 border-radius: 100px 每个角可以设置两个值,x和y 补充: 可分别设置长,短半径,以“/”进行分隔,遵循顺时针的顺序,“/”之前为横轴半径,“/”之后为纵轴半径,如 ...
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- activity(工作流)初步学习记录
1.概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期 ...
- STM8S103汇编文档和注意
1.官方文档<STM8 CPU programming manual>介绍了指令和寻址方式 2.辅助类文档,boot loader文档<STM8 bootloader> 3.关 ...
- rman参数
rman 参数 RMAN> show all; 参数是存放在控制文件中的 改参数:(直接改) eg: CONFIGURE RETENTION POLICY TO REDUNDANCY 3 参数: ...
- Web前端为什么这么火爆?
Web前端为什么这么火爆? 互联网发展到今天,全球已有28.9亿互联网用户,中国有355万网站,6.5亿网民,13亿手机用户,5亿微信用户,当步入互联网+时代后,互联网已经越来越复杂,纷繁复杂的互联网 ...
- vue 键盘监听事件
<template> <div class="hello"> <input v-on:keyup.enter="submit" t ...
- 【BZOJ4444】国旗计划 - 决策单调性
Description A国正在开展一项伟大的计划——国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了N名优秀的边 ...
- 在学校机房联想硬盘保护下安装Linux,并配置锐捷客户端
最近几天一直在机房里刷题,空调开着非常舒服.但是机房电脑里全是windows系统,不太好用,挺膈应人的. 一直打算换个系统,刚才终于搞定网络问题了,以后用电脑就可以爽到了. 联想硬盘保护系统下u盘安装 ...
- java 实现顺序结构线性列表
package com.ncu.list; /** * * 顺序结构线性列表 * * @author liuhao * */ public class SquenceList<T> { p ...