hdu 4856 Tunnels (bfs + 状压dp)
The input contains mutiple testcases. Please process till EOF.
For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.
The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”.
Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) in the map are both empty grid.
If it is impossible for Bob to visit all the tunnels, output -1.
题意:
一个边长为n的正方形网格图,其中有一些点' . '表示可达,' # '表示不可达,你不能走到不可达的点上,以及每一个单位时间你只能走到相邻的网格(上下左右)。现在给你m条密道,每条密道起始位置(x1,y1),终点位置(x2,y2),当你从起点进去后能瞬间从终点位置出来(不花时间),但是每条密道你只能走一遍。现在,你可以选择任意一个可达的点作为起点,问能否在满足条件下走完所有的密道,有解输出最短时间,否则输出-1。
分析:
先用bfs处理出来每个隧道之间的距离,然后就是走的各个隧道之间的顺序,可以用状压dp来做,
dp[ i ][ j ]表示已经经过的密道状态为 i (i为压缩状态,二进制的每一位表示相应的密道是否已经走过,走过为1,否则为0),最后一个经过的密道是j时的最小用时。
状态转移方程为:dp[ i | ( 1 << k ) ][ k ] = min { dp[ i | ( 1 << k ) ][ k ] , dp[ i ][ j ] + dist[ j ][ k ] } 。
其中必须保证dp[ i ][ j ]已经有了的状态,dist[ j ][ k ]不是INF ( j 出发能到达 k ),以及状态 i 中不包含第 k 个密道能才发生转移。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
const int INF = <<;
char s[maxn][maxn];
int c[maxn][maxn], d[<<][], n;
int dx[] = {, , , -};
int dy[] = {, -, , };
struct node
{
int x, y, step;
}pos, ne;
int bfs(int a, int b, int c, int d)
{
queue<node>q;
int vis[maxn][maxn], i;
memset(vis, , sizeof(vis));
ne.x = a; ne.y = b; ne.step = ;
vis[a][b] = ;
q.push(ne);
while(!q.empty())
{
pos = q.front();
q.pop();
if(pos.x == c && pos.y == d)
return pos.step;
for(i = ; i < ; i++)
{
ne.x = pos.x+dx[i];
ne.y = pos.y+dy[i];
ne.step = pos.step+;
if(!(ne.x>=&&ne.x<=n && ne.y>=&&ne.y<=n)) continue;
if(s[ne.x][ne.y]=='#') continue;
if(vis[ne.x][ne.y]) continue;
q.push(ne);
vis[ne.x][ne.y] = ;
}
}
return INF;
}
int main()
{
int m, i, j, k, ans;
int x1[maxn], y1[maxn], x2[maxn], y2[maxn];
while(~scanf("%d%d", &n, &m))
{
memset(c, , sizeof(c));
memset(s, , sizeof(s));
for(i = ; i <= n; i++)
{
getchar();
for(j = ; j <= n; j++)
scanf("%c", &s[i][j]);
}
for(i = ; i < m; i++)
cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];
for(i = ; i < m; i++)
for(j = ; j < m; j++)
{
if(i == j) continue;
c[i][j] = bfs(x2[i], y2[i], x1[j], y1[j]);
}
for(i = ; i < (<<m); i++)
for(j = ; j < m; j++)
d[i][j] = INF; for(i = ; i < m; i++)
d[<<i][i] = ; for(i = ; i < (<<m); i++)
for(j = ; j < m; j++)
if(d[i][j]!=INF)
for(k = ; k < m; k++)
{
if(!(i&(<<k)) && c[j][k]!=INF)
d[i|(<<k)][k] = min(d[i|(<<k)][k], d[i][j]+c[j][k]);
}
ans = INF;
for(i = ; i < m; i++)
ans = min(ans, d[(<<m)-][i]);
if(ans == INF) printf("-1\n");
else
printf("%d\n", ans);
}
return ;
}
hdu 4856 Tunnels (bfs + 状压dp)的更多相关文章
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4856 Tunnels(BFS+状压DP)
HDU 4856 Tunnels 题目链接 题意:给定一些管道.然后管道之间走是不用时间的,陆地上有障碍.陆地上走一步花费时间1,求遍历全部管道须要的最短时间.每一个管道仅仅能走一次 思路:先BFS预 ...
- HDU-4856 Tunnels (BFS+状压DP)
Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...
- hdu 4856 Tunnels(bfs+状态压缩)
题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定. 解题思路:首先用bfs处理出 ...
- HDU 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
- 孤岛营救问题(BFS+状压DP)
孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...
- QDUOJ 来自xjy的签到题(bfs+状压dp)
来自xjy的签到题 Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...
- HDU-3681-Prison Break(BFS+状压DP+二分)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
随机推荐
- MYSQL注入天书之后记
后记 对于工具的看法: 我之所以在每个例子中只写了几个示例,是因为我希望你能通过这一两个示例举一反三将其他的列出来.如果让我来完成每一次完整的注入,应该在知道原理的情况下,必然使用工具或者自己写代码实 ...
- windbg内核诊断方式--转载
一.WinDbg是什么?它能做什么? WinDbg是在windows平台下,强大的用户态和内核态调试工具.它能够通过dmp文件轻松的定位到问题根源,可用于分析蓝屏.程序崩溃(IE崩溃)原因,是我们日常 ...
- HDU 3518 Boring counting(后缀数组,字符处理)
题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...
- php laravel 安装
windows环境尝试学习一下laravel 1.因为SAE的php版本为5.3,因此最高只能支持到Laravel4.1.x.(Laravel4.2用到了php5.4的trait特性) 以4.1为主. ...
- iOS-CALayer图片淡入淡出动画
]; } - (.f; CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:, , ...
- WebSocket 是什么原理?为什么可以实现持久连接
你可以把 WebSocket 看成是 HTTP 协议为了支持长连接所打的一个大补丁,它和 HTTP 有一些共性,是为了解决 HTTP 本身无法解决的某些问题而做出的一个改良设计.在以前 HTTP 协议 ...
- Install wxWidgets-3.0.2 on GNU/Linux Debian
转载自 http://www.binarytides.com/install-wxwidgets-ubuntu/ wxWidgets wxWidgets is an application devel ...
- 快速创建maven 工程:simple java工程,webapp
http://www.cnblogs.com/buhaiqing/archive/2012/11/04/2754187.html 会从maven的Repository里查找所有支持的arche typ ...
- java传输json数据用md5加密过程
1.加密过程:客户端传输数据,包含两部分,一部分原始数据,一部分签名.签名就是对原始数据MD5加密后的字节序列.而原始数据就是普通的string字符串. 2.服务器端呢:将收到的原始数据,进行MD5加 ...
- Discuss!X3.2 绑定微信
完整程序 http://pan.baidu.com/s/1jGL5veQ 密码kgga Discuz!X3.2 在继承和完善 Discuz!X3.1 的基础上,针对社区移动端进行了新的尝试.新版本主要 ...