好难的一道题。

题意:一个机器人要逃出监狱,每走一步消耗一点电量,初始时电量是满的。给一个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+二分)的更多相关文章

  1. HDU3681 Prison Break

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  2. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

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

  3. hdu 3681 Prison Break (TSP问题)

    Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. Prison Break

    Prison Break 时间限制: 1 Sec  内存限制: 128 MB提交: 105  解决: 16[提交][状态][讨论版] 题目描述 Scofild又要策划一次越狱行动,和上次一样,他已经掌 ...

  5. hdu 3681 Prison Break(状态压缩+bfs)

    Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...

  6. hdu3511 Prison Break 圆的扫描线

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...

  7. 1254 - Prison Break

    1254 - Prison Break   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Mic ...

  8. HDU 3681 Prison Break (二分 + bfs + TSP)

    题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...

  9. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

随机推荐

  1. 几条特殊的SQL语句

    1, 有case情况. select trunc(exf_payment_receipt.work_date),exf_payment_receipt.exchange_code,exf_paymen ...

  2. AppExtention - today

    声明: 本文转自王巍 WWDC 2014 Session笔记 - iOS 通知中心扩展制作入门 本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 Creating Exten ...

  3. 使用struts2标签<s:action无法显示引用页面问题

    使用过程中参考:http://www.cnblogs.com/lihuiyy/archive/2012/03/23/2411601.html 个人使用: 1.引用页面 <s:action nam ...

  4. SpeedPHP关于一对一和一对多关联关系的建立 model建立

    新闻表:t_news 新闻类型表:b_type_to_name 其中一个新闻类型可以包含多个新闻(hasmany),一个新闻只能属于一种新闻类型(hasone) 下面是新闻model类: <?p ...

  5. 如何让Activiti-Explorer使用sql server数据库

    从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...

  6. VC常用数据类型使用转换

    我们先定义一些常见类型变量借以说明 int i = 100; long l = 2001; float f=300.2; double d=12345.119; char username[]=&qu ...

  7. ArcGIS Engine生成等值线(C#)

    原文:ArcGIS Engine生成等值线(C#) 本文介绍c#写的利用ArcGIS Engine生成等值线的方法.c#写的根据雨量站的降雨量值内插出降雨量等值线的功能.做几点说明:根据离散点生成等值 ...

  8. iOS,Android网络抓包教程之tcpdump

    现在的移动端应用几乎都会通过网络请求来和服务器交互,通过抓包来诊断和网络相关的bug是程序员的重要技能之一.抓包的手段有很多:针对http和https可以使用Charles设置代理来做,对于更广泛的协 ...

  9. Android-锁屏功能

    当屏幕多久没有点击的时候,进行某种操作就是所谓的锁屏功能. onCreate: public void addRunnable() { handler.postDelayed(runnable, Co ...

  10. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...