[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大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
随机推荐
- 海康威视采集卡结合opencv使用(两种方法)-转
(注:第一种方法是我的原创 ^_^. 第二种方法是从网上学习的.) 第一种方法:利用 板卡的API: GetJpegImage 得到 Jpeg 格式的图像数据,然后用opencv里的一个函数进行解码 ...
- CodeForces 149D Coloring Brackets (区间DP)
题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...
- Python学习日志9月14日
今天早晨又没有专心致志的学习,我感觉我可能是累了,需要减轻学习的程度来调整一下咯.这几天装电脑弄的昏天暗地的,身体有点吃不消了.时间真是神奇的魔法,这半个月来,每隔几天都有想要改变策略的想法.今天早晨 ...
- 解决android studio设置版本号
获取版本号代码 /** * 获取版本号 * @return 当前应用的版本号 */ public static String getVersion(Context context) { try { P ...
- 基本编程题 --python
1.让Python帮你随机选一个饮品吧! import random listC = ['加多宝', '雪碧', '可乐', '勇闯天涯', '椰子汁'] print(random.choices(l ...
- 国庆集训 || Wannafly Day4
链接:https://www.nowcoder.com/acm/contest/205#question 一场题面非常 有趣 但是题目非常 不友好的比赛 QAQ L.数论之神 思维(?) 题意:求 ...
- c语言产生随机数的方法
在C语言中,rand()函数可以用来产生随机数,但是这不是真真意义上的随机数,是一个伪随机数,是根据一个数,我们可以称它为种子,为基准以某个递推公式推算出来的一系数,当这系列数很大的时候,就符合正态公 ...
- Bootstrap 网格系统(Grid System)实例2
Bootstrap 网格系统(Grid System):堆叠水平,两种样式 <!DOCTYPE html><html><head><meta http-equ ...
- HashMap允许将null用作键 也允许将null作为值
HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...
- 对Fiddler设置【Decrypt HTTPS traffic】后火狐浏览器打开https【您的连接并不安全】的解决方法
火狐浏览器在打开https页面的时候出现[您的连接并不安全]的提示页面: 在设置Fiddler的HTTPS解密的时候,会对下面图中的红线框的选项点击一次生成一个Fiddler 根证书在桌面上: 点击火 ...