hdu 4856 Tunnels(bfs+状态压缩)
题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定。
解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 20;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
struct point {
int x, y;
point (int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
};
struct pipe {
point s, e;
} pi[maxn];
int N, M, d[maxn][maxn];
int dp[(1<<15)+5][maxn];
char g[maxn][maxn];
int bfs (point s, point e) {
int vis[maxn][maxn];
memset(vis, -1, sizeof(vis));
queue<point> que;
vis[s.x][s.y] = 0;
que.push(s);
while (!que.empty()) {
point u = que.front();
que.pop();
if (u.x == e.x && u.y == e.y)
return vis[u.x][u.y];
for (int i = 0; i < 4; i++) {
int x = u.x + dir[i][0];
int y = u.y + dir[i][1];
if (x <= 0 || x > N || y <= 0 || y > N)
continue;
if (vis[x][y] != -1 || g[x][y] == '#')
continue;
vis[x][y] = vis[u.x][u.y] + 1;
que.push(point(x, y));
}
}
return -1;
}
void init () {
for (int i = 1; i <= N; i++)
scanf("%s", g[i] + 1);
memset(d, 0, sizeof(d));
for (int i = 0; i < M; i++) {
scanf("%d%d%d%d", &pi[i].s.x, &pi[i].s.y, &pi[i].e.x, &pi[i].e.y);
for (int j = 0; j < i; j++) {
d[i][j] = bfs(pi[i].e, pi[j].s);
d[j][i] = bfs(pi[j].e, pi[i].s);
}
}
}
int solve () {
memset(dp, INF, sizeof(dp));
for (int i = 0; i < M; i++)
dp[1<<i][i] = 0;
for (int s = 0; s < (1<<M); s++) {
for (int j = 0; j < M; j++) {
if (dp[s][j] == INF)
continue;
for (int k = 0; k < M; k++) {
if (s&(1<<k))
continue;
if (d[j][k] == -1)
continue;
dp[s|(1<<k)][k] = min(dp[s|(1<<k)][k], dp[s][j] + d[j][k]);
}
}
}
int ans = INF;
for (int i = 0; i < M; i++)
ans = min(ans, dp[(1<<M)-1][i]);
return ans == INF ? -1 : ans;
}
int main () {
while (scanf("%d%d", &N, &M) == 2) {
init();
printf("%d\n", solve());;
}
return 0;
}
hdu 4856 Tunnels(bfs+状态压缩)的更多相关文章
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- hdu 1429(BFS+状态压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 4856 Tunnels(BFS+状压DP)
HDU 4856 Tunnels 题目链接 题意:给定一些管道.然后管道之间走是不用时间的,陆地上有障碍.陆地上走一步花费时间1,求遍历全部管道须要的最短时间.每一个管道仅仅能走一次 思路:先BFS预 ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- cocos2d_x_05_Box2D物理引擎
一.认识Box2D 帮助文档,共69页 二.创建一个物理世界 先导入主头文件 #include <Box2D/Box2D.h> 三.物理世界一览 像素转成米 的比例因子 就是32 三.运动 ...
- Linux+Apache+Mysql+Php
CentOS 6.3下源码安装LAMP(Linux+Apache+Mysql+Php)环境 一.简介 什么是LAMP LAMP是一种Web网络应用和开发环境,是Linux, Apache, My ...
- FZU 2113(数位dp)
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=38054 题意:求区间[a,b]中包含'1'的个数. 分析:数位dp ...
- [WPF] 使用Grid与GridSplitter排版布局
原文:[WPF] 使用Grid与GridSplitter排版布局 前言 在開發應用程式時,一個很重要的工作項目就是設計使用者介面的排版布局.WPF中所提供的Grid控制項,讓開發人員擁有將版面分割為欄 ...
- java 通过sftp服务器上传下载删除文件
最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...
- 【ECSHOP插件】商品颜色尺寸仿淘宝选择功能免费发布
先放效果图,如此实用的功能是不是迫不及待的要添加到自己的网店中了呢 牵涉到的修改文件(default模板为例) /themes/default/style.css /themes/default/ ...
- java split小结(转)
2016.03.27下午参加华为机试,简单扫了一眼几个题的标题,选择了一道字符串问题,其实该题非常非常的简单,可以说是简单的不能再简单了,而且有很多种解法,上机时我选择了直接借用java提供的一些函数 ...
- C#四舍五入保留两位小数
- T-SQL基础(7) - 透视,逆透视和分组集
透视转换: use tempdb;if object_id('dbo.Orders', 'U') is not null drop table dbo.Orders;create table dbo. ...
- php zip文件内容比較类
php zip 文件比較类,比較两个zip文件的内容,返回新增,删除,及同样的文件列表.临时仅仅支持单层. 需求:上传一个zip文件,zip内有非常多图片文件.须要对图片文件进行一系列非常耗时的处理. ...