poj 3133 Manhattan Wiring
http://poj.org/problem?id=3133
考虑插头 dp
用四进制表示一个插头的状态,0 表示没有插头,2 表示这个插头是连接两个 2 的,3 同理
然后就是大力分类讨论了
这题还是比较友善的一题,思路相对来说简单很多
我写的括号序列的方法状态是满的,数组必须开到 $ 3 ^ 9 $ 才能过
#include <cstdio>
#include <cstring>
#include <algorithm>
#define CIOS ios::sync_with_stdio(false);
#define rep(i, a, b) for(register int i = a; i <= b; i++)
#define per(i, a, b) for(register int i = a; i >= b; i--)
#define DEBUG(x) cerr << "DEBUG" << x << " >>> ";
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') fu = -1; c = getchar(); }
while(c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int mod = 19687, N = mod + 50, INF = 0x7fffffff;
int a[15][15], bin[15], f[2][N], v[2][N], head[N], tot[2], nxt[N], n, m, now;
// 状态只有 0, 2, 3, 表示没有插头, 2 插头和 3 插头
void ins(int zt, int val) {
int u = zt % mod;
for(register int i = head[u]; i; i = nxt[i])
if(v[now][i] == zt) {
f[now][i] = min(f[now][i], val);
return;
}
nxt[++tot[now]] = head[u]; head[u] = tot[now];
v[now][tot[now]] = zt; f[now][tot[now]] = val;
}
void sol() {
tot[now] = 1; f[now][1] = v[now][1] = 0;
for(register int i = 1; i <= n; i++) {
for(register int i = 1; i <= tot[now]; i++) v[now][i] <<= 2;
for(register int j = 1; j <= m; j++) {
now ^= 1; memset(head, 0, sizeof(head)); tot[now] = 0;
for(register int k = 1; k <= tot[now ^ 1]; k++) {
int zt = v[now ^ 1][k], val = f[now ^ 1][k];
int left = (zt >> ((j << 1) - 2)) & 3, up = (zt >> (j << 1)) & 3;
int right = (j == m) ? 1 : a[i][j + 1], down = (i == n) ? 1 : a[i + 1][j];
if(a[i][j] == 1) {
if(!left && !up) ins(zt, val);
} else if(!left && !up) {
if(!a[i][j]) {
ins(zt, val);
if(right + down == 5 || right == 1 || down == 1) continue;
if(right == 2 || down == 2) {
// 当两边有 2 插头时, 向下和向右摆 2 插头
ins(zt ^ (bin[j - 1] << 1) ^ (bin[j] << 1), val + 1);
} else if(right == 3 || down == 3) {
ins(zt ^ (bin[j - 1] * 3) ^ (bin[j] * 3), val + 1);
} else {
ins(zt ^ (bin[j - 1] << 1) ^ (bin[j] << 1), val + 1);
ins(zt ^ (bin[j - 1] * 3) ^ (bin[j] * 3), val + 1);
}
} else {
// 当前位置是一个 2 或 3
if(a[i][j] + right != 5 && right != 1) {
ins(zt ^ (bin[j] * a[i][j]), val + 1);
}
if(a[i][j] + down != 5 && down != 1) {
ins(zt ^ (bin[j - 1] * a[i][j]), val + 1);
}
}
} else if(left && up) {
if(left + up == 5 || a[i][j]) continue;
ins(zt ^ (bin[j - 1] * left) ^ (bin[j] * up), val + 1);
} else if(left && !up) {
if(a[i][j] == 0) {
if(right == 0 || right == left) ins(zt ^ (bin[j - 1] * left) ^ (bin[j] * left), val + 1);
if(down == 0 || down == left) ins(zt, val + 1);
} else if(a[i][j] == left) {
ins(zt ^ (bin[j - 1] * left), val + 1);
}
} else if(!left && up) {
if(a[i][j] == 0) {
if(right == 0 || right == up) ins(zt, val + 1);
if(down == 0 || down == up) ins(zt ^ (bin[j] * up) ^ (bin[j - 1] * up), val + 1);
} else if(a[i][j] == up) {
ins(zt ^ (bin[j] * up), val + 1);
}
}
}
}
}
}
int main() {
bin[0] = 1; for(register int i = 1; i <= 11; i++) bin[i] = bin[i - 1] << 2;
while(scanf("%d %d", &n, &m) == 2 && n && m) {
memset(a, 0, sizeof(a)); memset(head, 0, sizeof(head)); memset(tot, 0, sizeof(tot));
for(register int i = 1; i <= n; i++) {
for(register int j = 1; j <= m; j++) read(a[i][j]);
}
sol(); int ans = INF;
for(register int i = 1; i <= tot[now]; i++) ans = min(ans, f[now][i]);
if(ans == INF) ans = 2; print(ans - 2, '\n');
}
return 0;
}
poj 3133 Manhattan Wiring的更多相关文章
- POJ 3133 Manhattan Wiring (插头DP,轮廓线,经典)
题意:给一个n*m的矩阵,每个格子中有1个数,可能是0或2或3,出现2的格子数为2个,出现3的格子数为2个,要求将两个2相连,两个3相连,求不交叉的最短路(起终点只算0.5长,其他算1). 思路: 这 ...
- 【POJ】3133 Manhattan Wiring
http://poj.org/problem?id=3133 题意:n×m的网格,有2个2,2个3,他们不会重合.还有障碍1.现在求2到2的路径和3到3的路径互不相交的最短长度-2.(2<=n, ...
- poj3133 Manhattan Wiring
Manhattan Wiring Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2016 Accepted: 1162 ...
- [LA3620]Manhattan Wiring
[LA3620]Manhattan Wiring 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入” 题解 我们把“连线”的过程改为“铺地砖”的过程,总共有 11 种地砖,每种地砖上 ...
- 【poj3133】 Manhattan Wiring
http://poj.org/problem?id=3133 (题目链接) 题意 $n*m$的网格里有空格和障碍,还有两个$2$和两个$3$.要求把这两个$2$和两个$3$各用一条折线连起来.障碍里不 ...
- poj 1806 Manhattan 2025
点击打开链接 题目大意就是给定一个最大歩数,让你输出你在三维的空间中可以到达的位置的切片,注意当歩数大于9的时候就不需要输出了! #include<stdio.h> #include< ...
- uva1214 Manhattan Wiring 插头DP
There is a rectangular area containing n × m cells. Two cells are marked with “2”, and another two w ...
- [Poj3133]Manhattan Wiring (插头DP)
Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...
- caioj1496: [视频]基于连通性状态压缩的动态规划问题:Manhattan Wiring
%%%%orz苏大佬 虽然苏大佬的baff吸不得,苏大佬的梦信不得,但是膜苏大佬是少不得的囧 这题还是比较有收获的 哼居然有我不会做的插头DP 自己yy了下,2表示属于2的插头,3表示3的插头 假如当 ...
随机推荐
- 新手C#属性set,get的学习(部分转)2018.08.06
public class person { public string name; } public class person { public string Name { set; get; } } ...
- protobuf's custom-options
[protobuf's custom-options] protobuf可以设置属性,就像__attribute__可以给函数设置属性一样,protobuf更牛的是可以设置自定义属性.实际就是属性对象 ...
- Docker构建redis cluster集群
准备工作 安装gcc ruby 解压编译redis Redis 是 c 语言开发的.安装 redis 需要 c 语言的编译环境.如果没有 gcc 需要在线安装. yum install gcc-c++ ...
- transform.rotation和GetComponent<Rigidbody>().MoveRotation
同时在UPDATE和FIXED UPDATE中调整 旋转 并未出现闪,而是一直以UPDATE中的为准,可认为MoveRotation调用后在UPDATE中生效 using System.Collect ...
- 牛掰的python与unix
python的中心哲学 Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on ...
- MDK5使用技巧
1.文本美化 修改 修改字体以及颜色如下: 修改用户自定义的关键字,如下: 代码编辑技巧 1.TAB键的妙用 使用TAB键可以整体向右移动相应位,使用SHIFT+TAB键整体左移相应位. 2.快速定位 ...
- Effective Java 中文版
始读于2014年8月2日10:15,整理完成于2014年8月20日23:14:42 一图一世界,<Effective Java >是Java领域大牛Joshua Bloch的获奖之作,去年 ...
- DataStage 三、配置ODBC
DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 1 配置ODBC需要了解的基础知识 配置O ...
- java并发编程实战:第十二章---并发程序的测试
并发程序中潜在错误的发生并不具有确定性,而是随机的. 安全性测试:通常会采用测试不变性条件的形式,即判断某个类的行为是否与其规范保持一致 活跃性测试:进展测试和无进展测试两方面,这些都是很难量化的(性 ...
- lambda distinct
public ActionResult Index() { IList<RegisterModel> regList = new List<RegisterModel>() { ...