hdu3681--Prison Break(TSP+二分)
好难的一道题。
题意:一个机器人要逃出监狱,每走一步消耗一点电量,初始时电量是满的。给一个n*m(n,m<=15)的字符数组代表监狱,F代表起始点,G代表补充满电量,每个G只能补充一次,Y代表开关,D不能经过,S表示空地。要求打开所有开关,也就是经过所有Y点,电池的满电量最少是多少。如果不能逃出输出-1。G和Y的个数和不会超过15。
题解:二分答案。通过bfs预处理出G,Y,F两两之间的距离,然后转化成TSP求解。借鉴了别人的代码。
#include <bits/stdc++.h>
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std; struct Point {
int x, y;
int d;
Point(int x, int y, int d) : x(x), y(y), d(d) {}
Point(int x, int y) : x(x), y(y), d(0) {}
Point() {} bool operator ==(const Point a) const {
if (x == a.x && y == a.y) return true;
return false;
} } p[50]; int m, n;
char mp[20][20];
int cf;
int dis[20][20];
int cnt = 0;
int ac = 0; // 所有y的集合
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int vis[20][20];
int dp[1<<17][20]; bool ok(int x, int y)
{
if (x < n && x >= 0 && y < m && y >= 0 &&
mp[x][y] != 'D' && !vis[x][y])
return true;
return false;
} int bfs(int a, int b)
{
clr(vis, 0);
queue<Point> q;
q.push(p[a]);
vis[p[a].x][p[a].y] = 1;
while (!q.empty()) {
Point now = q.front();
q.pop(); if (now == p[b]) return now.d;
for (int i = 0; i < 4; ++i) {
int nx = now.x + dir[i][0];
int ny = now.y + dir[i][1];
if (!ok(nx, ny)) continue;
int nd = now.d + 1;
q.push(Point(nx, ny, nd));
vis[nx][ny] = 1;
}
}
return -1;
} void getDis()
{
for (int i = 0; i < cnt; ++i) {
for (int j = i; j < cnt; ++j) {
if (i == j) dis[i][j] = 0;
else dis[j][i] = dis[i][j] = bfs(i, j);
}
}
} bool canGo(int en)
{
clr(dp, -1);
int st = (1 << cnt);
dp[1 << cf][cf] = en;
for (int i = 0; i < st; ++i) {
for (int j = 0; j < cnt; ++j) {
if ( !((1 << j) & i) || dp[i][j] == -1 ) continue;
if ((i & ac) == ac) return true;
for (int k = 0; k < cnt; ++k) {
if ( (1 << k) & i || dis[j][k] == -1 || dp[i][j] < dis[j][k] ) continue;
int nt = (1 << k) | i;
dp[nt][k] = max(dp[nt][k], dp[i][j] - dis[j][k]);
if (mp[ p[k].x ][ p[k].y ] == 'G') dp[nt][k] = en;
}
}
}
return false;
} int main()
{
while (~scanf("%d%d", &n, &m)) {
if (n == 0 && m == 0) break; for (int i = 0; i < n; ++i)
scanf("%s", mp[i]); ac = cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mp[i][j] == 'F') {
cf = cnt;
ac += (1 << cnt);
p[cnt++] = Point(i, j);
} else if (mp[i][j] == 'G') {
p[cnt++] = Point(i, j);
} else if (mp[i][j] == 'Y') {
ac += (1 << cnt);
p[cnt++] = Point(i, j);
}
}
} getDis(); int l = 0, r = 300;
while (l <= r) {
int mid = (l + r) >> 1;
if (canGo(mid)) r = mid - 1;
else l = mid + 1;
}
if (l >= 300) l = -1;
printf("%d\n", l);
}
return 0;
}
hdu3681--Prison Break(TSP+二分)的更多相关文章
- HDU3681 Prison Break
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 3681 Prison Break(BFS+二分+状态压缩DP)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- hdu 3681 Prison Break (TSP问题)
Prison Break Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Prison Break
Prison Break 时间限制: 1 Sec 内存限制: 128 MB提交: 105 解决: 16[提交][状态][讨论版] 题目描述 Scofild又要策划一次越狱行动,和上次一样,他已经掌 ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- hdu3511 Prison Break 圆的扫描线
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...
- 1254 - Prison Break
1254 - Prison Break PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Mic ...
- HDU 3681 Prison Break (二分 + bfs + TSP)
题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
随机推荐
- mysql UNIX时间戳与日期的相互转换 查询表信息
UNIX时间戳转换为日期用函数FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数UNIX_TIMESTAMP() Sel ...
- C++ dll调用-动态(显式)
C++ dll调用-动态(显式) 废话不说上代码, dll 头文件 j_test.h #pragma once extern "C"_declspec(dllexport) voi ...
- 将cocos2dx项目从VS移植到Eclipse
本文转自:http://www.cnblogs.com/Z-XML/p/3349518.html 引言:我们使用cocos2d-x引擎制作了一款飞行射击游戏,其中创新性地融入了手势识别功能.但是我们在 ...
- ASP.NET Application_Error错误日志写入
一个web项目开发已完成,测试也通过,但是,bug是测不完的,特别是在一个大的网络环境下.那么,我们就应该记录这些错误,然后改正.这里,我的出错管理页面是在global.asax里面的,利用里面的Ap ...
- java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.
java的类必须大写 java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致. 在dos用上下箭头,调用已用过的命令
- [jobdu]扑克牌顺子
一开始看到题还以为要DFS还是BFS,后来发现完全不用.排个序,然后看看大小王能不能弥补缺口就行,但后来又发现还要排除有相同大小牌的情况. #include <iostream> #inc ...
- OpenSSH for Windows,CopSSH
https://www.oschina.net/p/openssh+for+windows https://www.oschina.net/p/copssh
- Android TabActivity与Activity之间的动画跳转(主要Tabhost中跳转出来的动画效果解决)
首先,要说的是ActivityA到ActivityB的切换这个相对简单,只要overridePendingTransition(In,out). 这里不就说了.但是这里要说名的ActivityA不能T ...
- Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞
漏洞名称: Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞 CNNVD编号: CNNVD-201311-054 发布时间: 2013-11-06 更新时间: 2013- ...
- 【随笔】这段时间没有写博客是因为一边看Qt5的帮助文档一边写小程序
长话短说,因为和做程序员的以前的同学联系了一下,知道自己有很多不足,加之接到一个培训机构的人打来的电话稍微打击了一下,虽然那个人满嘴跑火车想装做和我很谈得来,但是我依然看到了自己没有写过任何命令行以外 ...