[LA3620]Manhattan Wiring
[LA3620]Manhattan Wiring
试题描述

输入

输出

输入示例
输出示例
数据规模及约定
见“输入”
题解
我们把“连线”的过程改为“铺地砖”的过程,总共有 11 种地砖,每种地砖上的图案连接了两个不同的边界,或只触碰了一个边界,或没有图案,具体见下图:

其中,有障碍的格子只能铺 0 号砖,有数字 2 或 3 的格子只能铺 1 到 4 号砖,空地可以铺 0 或 5 到 10 号砖。
然后我们就可以轮廓线 dp 了,把状态表示成上一行的底部是否有线,这一行的底部是否有线,当前格子的左边是否有线,具体见下图:

带绿点的格子表示当前格子。那么上图的状态就是 (02000100)3 了(我习惯先读上面一行,再读下面一行,最后读竖直边上的数字),注意这里 2 连出的线与 3 连出的线进行了区分,因为不能让 2 和 3 连到一起。
转移的时候需要判断一些不合法情况:线头接到了没有线头和它相连的地方,不同类型线头接在了一起,或是有一个线头等你去接而你没有理它。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 15
#define maxs 59060
#define maxb 11
#define oo 2147483647 struct Blo {
bool L, U, R, D; int v;
Blo() {}
Blo(bool _l, bool _u, bool _r, bool _d, int _v): L(_l), U(_u), R(_r), D(_d), v(_v) {}
} bls[maxb];
int n, m, Map[maxn][maxn], f[maxn][maxn][maxs], tri[maxn]; void up(int& a, int b) {
a = min(a, b);
return ;
} char str[maxn];
char* tri_(int x) {
int l = 0;
while(x) str[l++] = x % 3 + '0', x /= 3;
while(l <= m) str[l++] = '0';
str[l] = 0;
return str;
} int main() {
bls[0] = Blo(0, 0, 0, 0, 0);
bls[1] = Blo(1, 0, 0, 0, 1);
bls[2] = Blo(0, 1, 0, 0, 1);
bls[3] = Blo(0, 0, 1, 0, 1);
bls[4] = Blo(0, 0, 0, 1, 1);
bls[5] = Blo(1, 1, 0, 0, 2);
bls[6] = Blo(1, 0, 1, 0, 2);
bls[7] = Blo(1, 0, 0, 1, 2);
bls[8] = Blo(0, 1, 1, 0, 2);
bls[9] = Blo(0, 1, 0, 1, 2);
bls[10] = Blo(0, 0, 1, 1, 2);
tri[0] = 1;
for(int i = 1; i < maxn; i++) tri[i] = tri[i-1] * 3; while(1) {
n = read(); m = read();
if(!n && !m) break; for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) Map[i][j] = read();
int all = tri[m+1] - 1;
for(int i = 1; i <= n + 1; i++)
for(int j = 1; j <= m; j++)
for(int S = 0; S <= all; S++) f[i][j][S] = oo;
f[1][1][0] = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int S = 0; S <= all; S++) if(f[i][j][S] < oo) {
// printf("%d %d %s: %d\n", i, j, tri_(S), f[i][j][S]);
if(Map[i][j] == 1) {
if(S % 3 || S / tri[m]) continue;
if(j < m) up(f[i][j+1][S/3%tri[m-1]], f[i][j][S] + bls[0].v);
else up(f[i+1][1][S/3%tri[m-1]], f[i][j][S] + bls[0].v);
}
if(Map[i][j] == 2) {
for(int c = 1; c <= 4; c++) {
if(S % 3 > 0 ^ bls[c].U > 0) continue;
if(S % 3 && S % 3 != 1) continue;
if(S / tri[m] > 0 ^ bls[c].L > 0) continue;
if(S / tri[m] && S / tri[m] != 1) continue;
if(j == m && bls[c].R) continue;
int tS = S / 3 % tri[m-1] + (int)bls[c].D * tri[m-1] + (int)bls[c].R * tri[m];
if(j < m) up(f[i][j+1][tS], f[i][j][S] + bls[c].v);
else up(f[i+1][1][tS], f[i][j][S] + bls[c].v);
}
}
if(Map[i][j] == 3) {
for(int c = 1; c <= 4; c++) {
if(S % 3 > 0 ^ bls[c].U > 0) continue;
if(S % 3 && S % 3 != 2) continue;
if(S / tri[m] > 0 ^ bls[c].L > 0) continue;
if(S / tri[m] && S / tri[m] != 2) continue;
if(j == m && bls[c].R) continue;
int tS = S / 3 % tri[m-1] + (int)bls[c].D * 2 * tri[m-1] + (int)bls[c].R * 2 * tri[m];
if(j < m) up(f[i][j+1][tS], f[i][j][S] + bls[c].v);
else up(f[i+1][1][tS], f[i][j][S] + bls[c].v);
}
}
if(!Map[i][j]) {
for(int c = 0; c <= 10; c ? c++ : (c = 5)) {
int tp = 0;
if(S % 3 > 0 ^ bls[c].U > 0) continue;
if(S % 3) tp = S % 3;
if(S / tri[m] > 0 ^ bls[c].L > 0) continue;
if(S / tri[m] && tp && S / tri[m] != tp) continue;
if(S / tri[m]) tp = S / tri[m];
if(j == m && bls[c].R) continue;
if(tp) {
int tS = S / 3 % tri[m-1] + (int)bls[c].D * tp * tri[m-1] + (int)bls[c].R * tp * tri[m];
if(j < m) up(f[i][j+1][tS], f[i][j][S] + bls[c].v);
else up(f[i+1][1][tS], f[i][j][S] + bls[c].v);
}
else for(tp = 1; tp <= 2; tp++) {
int tS = S / 3 % tri[m-1] + (int)bls[c].D * tp * tri[m-1] + (int)bls[c].R * tp * tri[m];
if(j < m) up(f[i][j+1][tS], f[i][j][S] + bls[c].v);
else up(f[i+1][1][tS], f[i][j][S] + bls[c].v);
}
}
}
} printf("%d\n", f[n+1][1][0] < oo ? (f[n+1][1][0] >> 1) : 0);
} return 0;
}
代码贼难写。。。QAQ
[LA3620]Manhattan Wiring的更多相关文章
- poj3133 Manhattan Wiring
Manhattan Wiring Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2016 Accepted: 1162 ...
- 【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
http://poj.org/problem?id=3133 (题目链接) 题意 $n*m$的网格里有空格和障碍,还有两个$2$和两个$3$.要求把这两个$2$和两个$3$各用一条折线连起来.障碍里不 ...
- poj 3133 Manhattan Wiring
http://poj.org/problem?id=3133 考虑插头 dp 用四进制表示一个插头的状态,0 表示没有插头,2 表示这个插头是连接两个 2 的,3 同理 然后就是大力分类讨论了 这题还 ...
- 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表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...
- POJ 3133 Manhattan Wiring (插头DP,轮廓线,经典)
题意:给一个n*m的矩阵,每个格子中有1个数,可能是0或2或3,出现2的格子数为2个,出现3的格子数为2个,要求将两个2相连,两个3相连,求不交叉的最短路(起终点只算0.5长,其他算1). 思路: 这 ...
- caioj1496: [视频]基于连通性状态压缩的动态规划问题:Manhattan Wiring
%%%%orz苏大佬 虽然苏大佬的baff吸不得,苏大佬的梦信不得,但是膜苏大佬是少不得的囧 这题还是比较有收获的 哼居然有我不会做的插头DP 自己yy了下,2表示属于2的插头,3表示3的插头 假如当 ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
随机推荐
- uvm_void 寂静的空宇
空也是一种存在. ——<三体> 文件: $UVM_HOME/src/base/uvm_misc.svh virtual class uvm_void; endclass 在静寂 ...
- iOS Block的本质(二)
iOS Block的本质(二) 1. 介绍引入block本质 通过上一篇文章Block的本质(一)已经基本对block的底层结构有了基本的认识,block的底层就是__main_block_impl_ ...
- iPhone开发小工具
1.AppIcon: 可以瞬间把图片转换为应用所需要的Icon(Icon-72.png,Icon-72@2x.png,......iTunesArtwork@2x) 2.Resizer: 方便把- ...
- 原创 :xftp SFTP子系统申请已拒绝 请确保SSH链接的SFTP子系统设置有效
在出现这个错误时候 如果你的远程连接没有问题 那么就执行下面的命令 service sshd restart 搞定!
- libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件
添加了libxml2.dylb的framework ,结果还是引用不了<libxml/tree.h>, 老是提示找不到头文件. 这个问题其实比较容易解决,但是XCode的版本问题确实让开 ...
- C#中窗体边框隐藏
设置窗体属性 FormBorderStyle 为 None
- bxslider 使用帮助
“bxSlider”就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出效果: 支持图片.视频以及任意html内容: 支持触 ...
- win7旗舰版下配置IIS服务器
选择上述的插件后,Windows 需要更新一段时间,并重启电脑 测试是否安装成功:http://localhost 注意:默认端口号是 80,不能和tomcat 的 80 端口同时重启 常 ...
- 响应式web设计视图工具及插件总结----20150113
响应式web设计可以说火不火是迟早的,下面就对于最开始的视口调试的方法汇总,希望有好的方法大家一起交流. 1.火狐:从Firefox升级到29.0之后就不直接支持Firesizer了. 先安装Add- ...
- 51nod 1264 线段相交——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1264 检查点的位置就行了,具体见注释. /* (a-c)×(d-c)*(d ...