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 ...
随机推荐
- frequentism-and-bayesianism-chs-iv
frequentism-and-bayesianism-chs-iv 频率主义与贝叶斯主义 IV:Python的贝叶斯工具 这个notebook出自Pythonic Perambulations的 ...
- .NET设计模式(4):建造者模式(Builder Pattern)(转)
概述 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...
- Unity3D判断鼠标向右或向左滑动,响应不同的事件
private var first = Vector2.zero; private var second = Vector2.zero; function Update () { } function ...
- 用 VIPER 构建 iOS 应用架构(1)
[编者按]本篇文章由 Jeff Gilbert 和 Conrad Stoll 共同编写,通过构建一个基础示例应用,深入了解 VIPER,并从视图.交互器等多个部件理清 VIPER 的整体布局及思路.通 ...
- POJ 2823
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 35941 Accepted: 10636 ...
- solr的collection,shard,replica,core概念
一.collection 1.由多个cores组成一个逻辑索引叫做一个collection.一个collection本质上是一个可以跨越多个核的索引,同时包含冗余索引. 2.collection由不同 ...
- Jquery---用jQuery实现的智能隐藏、滑动效果的返回顶部代码
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script& ...
- CentOS 6.5下安装Zabbix 2.2.x
操作系统:CentOS Mini 6.5 yum install httpd.x86_64 httpd-manual.x86_64 php-xml php-mbstring mysql-server ...
- C#中毫米与像素的换算方法
C#中以像素作为尺寸单位,像素是一种相对的尺寸概念,与毫米的转换与当前显示器的分辨率有关.在不同分辨率下转换的系数不同. 借助GDI可以完成毫米至像素的转换. public static double ...
- MongoDB (一) MongoDB 介绍
MongoDB 是一个跨平台的,面向文档的数据库,提供高性能,高可用性和可扩展性方便. MongoDB工作在收集和文件的概念. 数据库 数据库是一个物理容器集合.每个数据库都有自己的一套文件系统上的文 ...