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 ...
随机推荐
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- WCF服务中,[DataMember]属性标记的属性一定要有set访问器
WCF服务中,如果实体类中,包含有[DataMember]属性标记时,该属性一定要有set访问器.当系统必须调用到[DataMember]标记的属性时,如果该属性没有set访问器,则会出错.
- IOS Crash捕获
IOS Crash ,就两种情况:一种是异常,另一种是中断[信号量]. #include <libkern/OSAtomic.h> #include <execinfo.h> ...
- 初识layer 快速入门
http://layer.layui.com/hello.html 如果,你初识layer,你对她不知所措,你甚至不知如何绑定事件… 那或许你应该用秒做单位,去认识她. 开始了解 第一步:部署 下载l ...
- 【译】 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击
王龑 - MAY 27, 2015 原文连接 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https:/ ...
- Python中编码问题?
一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...
- HDU 2529 Shot (物理数学题)
题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- iOS获取手机相关信息
iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t ...
- 构建iOS稳定应用架构时方案选择的思考,主要涉及工程结构,数据流思想和代码规范
工程结构架构,减少耦合混乱以及防治需求大改造成结构重构,如何构建稳定可扩展可变换的工程结构的思考 我打算采用Information flow的方式自上而下,两大层分为基础层和展现层的结构.基础层分为多 ...