HDU-4856 Tunnels (BFS+状压DP)
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.
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=100000;
struct TT
{
int sx,sy,ex,ey;
};
struct node
{
int x,y,t;
node(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
};
TT t[15];
char mp[20][20];
int dist[20][20];
int vis[20][20],n,m,dp[1<<15][15];
int d[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int bfs(int sx,int sy,int ex,int ey)
{
queue<node>q;
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
q.push(node(sx,sy,0));
while(!q.empty())
{
node u=q.front();
q.pop();
if(u.x==ex&&u.y==ey)
return u.t;
for(int i=0;i<4;++i){
int nx=u.x+d[i][0],ny=u.y+d[i][1];
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny]&&mp[nx][ny]!='#'){
vis[nx][ny]=1;
q.push(node(nx,ny,u.t+1));
}
}
}
return INF;
}
void init()
{
for(int i=0;i<m;++i)
for(int j=0;j<m;++j)
dist[i][j]=bfs(t[i].ex,t[i].ey,t[j].sx,t[j].sy);
}
void DP()
{
int tot=1<<m;
for(int i=0;i<tot;++i)
for(int j=0;j<m;++j)
dp[i][j]=INF;
for(int i=0;i<m;++i)
dp[1<<i][i]=0;
for(int i=1;i<tot;++i){
for(int j=0;j<m;++j){
if(i&(1<<j))
continue;
int sta=i|(1<<j);
for(int k=0;k<m;++k){
if(i&(1<<k))
dp[sta][j]=min(dp[sta][j],dp[i][k]+dist[k][j]);
}
}
}
int ans=INF;
for(int i=0;i<m;++i)
ans=min(ans,dp[tot-1][i]);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;++i)
scanf("%s",mp[i]+1);
for(int i=0;i<m;++i)
scanf("%d%d%d%d",&t[i].sx,&t[i].sy,&t[i].ex,&t[i].ey);
init();
DP();
}
return 0;
}
HDU-4856 Tunnels (BFS+状压DP)的更多相关文章
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- 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+状态压缩)
题目链接: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 ...
随机推荐
- 推荐:Java性能优化系列集锦
Java性能问题一直困扰着广大程序员,由于平台复杂性,要定位问题,找出其根源确实很难.随着10多年Java平台的改进以及新出现的多核多处理器,Java软件的性能和扩展性已经今非昔比了.现代JVM持续演 ...
- 2018跳槽面试必备之深入理解 Java 多线程核心知识
导语:多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线 ...
- 如何用tomcat发布自己的Java项目
如何用tomcat发布自己的Java项目 tomcat是什么?它是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器.我们用Java开发出来的web项目,通过tomcat发布出来,别人就可 ...
- Zigbee学习
(一)Zigbee简介和开发环境快速建立(IAR) 1.我不是很清楚控制链条,对于Zigbee不是太清楚 答案:CC2530 芯片上集成了 8051 内核(增强型) 2.性能特点:低速率远距离,这造就 ...
- POJ 2823 Sliding Window(单调队列 || 线段树)题解
题意:求每个长度为k的数组的最大值和最小值 思路: 1.用线段树创建维护最大值和最小值,遍历询问,简单复习了一下...有点手生 2.单调队列: 可以看一下详解 单调队列顾名思义就是一个单调递增或者递减 ...
- Unity3D学习笔记(二十):Rect、Canvas、Toggle、Slider、ScrollBar
Rect Transform(锚点):图片中心的四个点,界面以雪花形式显示 当四个点在一起的时候组成锚点,当四个点分开的时候组成锚框(合则锚点,分则锚框) Anchors: ----Min x:控 ...
- .Net Core集成Office Web Apps(一)
最近开始学习.Net Core,并使用Visual Studio Code工具来开发.感觉开发起来特别的方便,但是有个头疼的地方:许多的类库被修改了,一时半会儿还熟悉不了,需要查阅官方API... M ...
- 02_Kafka单节点实践
1.实践场景 开始前的准备条件: 1) 确认各个节点的jdk版本,将jdk升级到和kafka配套的版本(解压既完成安装,修改/etc/profile下的JAVA_HOME,source /etc/pr ...
- python删除所有自定义变量方法--转载
http://blog.sina.com.cn/s/blog_b2f983a50102yexs.html 当我们在pythonwin中创建多个变量后,通过dir()函数,可以看到所有已创建变量,这 ...
- WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty
参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...