【POJ】3133 Manhattan Wiring
http://poj.org/problem?id=3133
题意:n×m的网格,有2个2,2个3,他们不会重合。还有障碍1。现在求2到2的路径和3到3的路径互不相交的最短长度-2。(2<=n,m<=9)
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct H {
static const int M=1000007;
struct E { int next, to; }e[M<<1];
int mn[M], hash[M], cnt, ihead;
H() { cnt=ihead=0; memset(mn, 0x3f, sizeof mn); memset(hash, -1, sizeof hash); }
bool find(int x, int &pos) {
pos=x%M;
while(1) { if(hash[pos]==x) return 0; if(hash[pos]==-1) break; ++pos; if(pos==M) pos=0; }
hash[pos]=x;
return 1;
}
void ins(int a, int b) {
int pos; if(!find(a, pos)) { mn[pos]=min(mn[pos], b); return; }
mn[pos]=b; e[++cnt].next=ihead; ihead=cnt; e[cnt].to=pos;
}
void clr() { for(int i=ihead; i; i=e[i].next) hash[e[i].to]=-1, mn[e[i].to]=0x3f3f3f3f; ihead=cnt=0; }
}T[2];
int n, m, mp[12][12], all;
#define BIT(a,b) ((a)<<((b)<<1))
#define CLR(a,b) (a^=(a&BIT(3,b)))
#define GET(a,b) (3&((a)>>((b)<<1))) bool next(int s, int row, int col, bool U, bool D, bool L, bool R, int id, int &t) {
if((!row&&U) || (!col&&L) || (row==n-1&&D) || (col==m-1&&R)) return 0;
if((D&&mp[row+1][col]==1) || (R&&mp[row][col+1]==1)) return 0;
int l=GET(s, col), u=GET(s, col+1), d=0, r=0;
if((L&&!l) || (!L&&l) || (U&&!u) || (!U&&u)) return 0;
if((l&&l!=id) || (u&&u!=id)) return 0;
t=s;
if(!l && !u) { if(D) d=id; if(R) r=id; }
else if(l && u) { if(l!=u || mp[row][col]>=1) return 0; }
else if(l && !u) { if(D) d=l; if(R) r=l; }
else if(!l && u) { if(D) d=u; if(R) r=u; }
CLR(t, col); CLR(t, col+1);
t|=BIT(d, col); t|=BIT(r, col+1);
if(col==m-1) t<<=2; t&=all;
return 1;
} int bfs() {
int now=0, last=1;
T[now].clr();
T[now].ins(0, 0);
for(int row=0; row<n; ++row) for(int col=0; col<m; ++col) {
T[last].clr();
for(int i=T[now].ihead; i; i=T[now].e[i].next) {
int t, s=T[now].hash[T[now].e[i].to], d=T[now].mn[T[now].e[i].to]+1, id=mp[row][col]-1;
if(id>=1) {
if(next(s, row, col, 1, 0, 0, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 0, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 1, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 0, 1, id, t)) T[last].ins(t, d);
}
else {
if(next(s, row, col, 0, 0, 0, 0, 0, t)) T[last].ins(t, d-1);
if(id==-1) {
for(int k=1; k<=2; ++k) {
if(next(s, row, col, 1, 1, 0, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 1, 0, 1, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 1, 0, 0, 1, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 1, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 0, 1, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 1, 1, k, t)) T[last].ins(t, d);
}
}
}
}
swap(now, last);
}
int ret=0x3f3f3f3f;
for(int i=T[now].ihead; i; i=T[now].e[i].next) ret=min(ret, T[now].mn[T[now].e[i].to]);
return ret>=0x3f3f3f3f?0:ret-2;
} int main() {
while(scanf("%d%d", &n, &m), n|m) {
for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) scanf("%d", &mp[i][j]);
all=BIT(1, m+1)-1;
printf("%d\n", bfs());
}
return 0;
}
吐槽:
1、改着改着自己本来特判的东西然后改掉了QAQ,然后造数据对拍才发现....
2、为什么我写的插头dp常数那么大啊!!!
本题比简单回路简单多了吧?
自己很容易可以画出左插头和上插头的情况,然后在位中维护的是当前插头所连接的标号然后就行啦...
【POJ】3133 Manhattan Wiring的更多相关文章
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- 【POJ】【1637】Sightseeing tour
网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...
- 【poj】1001
[题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...
- 【POJ】3070 Fibonacci
[算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...
随机推荐
- 苹果开发者账号申请时报错提示错误:Legal Entity Name
he information you entered did not match your profile in the D&B database. Before submitting you ...
- 【转载】PHP使用1个crontab管理多个crontab任务
转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...
- MVC基础知识
1.View中获取Control和View: //获取控制器名称: ViewContext.RouteData.Values["controller"].ToString(); / ...
- Oracle备份 还原命令
1.备份命令 exp username/password file=d:/test/test.dmp; 2.还原命令 imp username/password full=y file=d:/test ...
- maven File encoding has not been set
原pom.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- SQL常用查询语句及函数
1.日期匹配_获取时间差 select datediff(dd,getdate(),'12/25/2006') --计算从今天到12/25/2006还有多少个月 2.不能通过IP连接数据库 在数据库 ...
- hdu 4741 2013杭州赛区网络赛 dfs ***
起点忘记录了,一直wa 代码写的很整齐,看着很爽 #include<cstdio> #include<iostream> #include<algorithm> # ...
- 解决 CentOS网卡eth0启用不了问题
转自:http://www.centoscn.com/CentosBug/osbug/2014/0423/2850.html [root@localhost Desktop]# service net ...
- 上四条只是我目前总结菜鸟们在学习FPGA时所最容易跑偏的地
长期以来很多新入群的菜鸟们总 是在重复的问一些非常简单但是又让新手困惑不解的问题.作为管理员经常要给这些菜鸟们普及基础知识,但是非常不幸的是很多菜鸟怀着一种浮躁的心态来学习 FPGA,总是急于求成. ...
- Mysql日志操作
Mysql日志 mysql的日志类型:错误日志: -log-err记录mysql服务的启动.运行.停止mysql服务时出现的问题查询日志: -log记录建立的客户端连接和执行的语句慢查询日志: -lo ...