题目链接

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.

Output
For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels.
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)的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 4856 Tunnels(BFS+状压DP)

    HDU 4856 Tunnels 题目链接 题意:给定一些管道.然后管道之间走是不用时间的,陆地上有障碍.陆地上走一步花费时间1,求遍历全部管道须要的最短时间.每一个管道仅仅能走一次 思路:先BFS预 ...

  4. HDU-4856 Tunnels (BFS+状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...

  5. hdu 4856 Tunnels(bfs+状态压缩)

    题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定. 解题思路:首先用bfs处理出 ...

  6. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  7. 孤岛营救问题(BFS+状压DP)

    孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...

  8. QDUOJ 来自xjy的签到题(bfs+状压dp)

    来自xjy的签到题   Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...

  9. HDU-3681-Prison Break(BFS+状压DP+二分)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

随机推荐

  1. UML构建模块(转载)

    UML描述的实时系统,这是非常重要的一个概念模型,然后进行逐渐. UML的概念模型可以通过学习掌握以下三大要素: UML构建模块 规则连接构建模块 UML的公共机制 本章介绍了所有的UML构建块. U ...

  2. BZOJ 1071组队

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1071 题目很好,居然写了很久,题解找了真多: 主要两种做法: O(n^2lgn),通过优先 ...

  3. codeforces #236 div2 简洁题解

    A:A. Nuts time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  4. 偏序集的Dilworth定理

    定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...

  5. ZOJ3560 Re:the Princess(高斯消元法)

    题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理.先来说说题意吧: 总共有n个人,n个人都会有一段话 ...

  6. iOS-xib(自定义UITableViewCell)

    1.创建一个自定义的xib

  7. URAL 1586 Threeprime Numbers(DP)

    题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...

  8. 最近在看 ASP.NET 5,有关官方实现的 OAuth 2 包

    有了官方实现的 OAuth 2 包,再扩展到国内就方便多了(懒得找第三方). 官方实现的有关授权和验证的包:https://github.com/aspnet/Security 根据这些,我就扩展了几 ...

  9. Learn CSS

    韩顺平老师的CSS讲的还是很简单的,仅作入门. div+css的介绍    div+css是什么. div元素是用来为HTML文档内大块(block-level)的内容提供结构和背景的元素. css是 ...

  10. linux c截断文件

    http://www.cnblogs.com/zhuxiongfeng/archive/2010/08/24/1807505.html