Problem Description
Rompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.
The jail area is a rectangle contains n×m little grids, each grid might be one of the following: 
1) Empty area, represented by a capital letter ‘S’. 
2) The starting position of Micheal#1, represented by a capital letter ‘F’. 
3) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s battery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.
4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor. 
5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.

In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation costs energy. Of course, Micheal#1 cannot move when his battery contains no energy.

The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.

 
Input
Input contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.
 
Output
For each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.
 
题目大意:略。太麻烦了不写了。
思路:首先此题显然可以二分答案然后判定,于是题目转化为判定问题,给定能量energy,问这个能量能否走完所有的Y。
注意到有意义的点其实只有F、G、Y,可以事先进行数次BFS,把他们之间的最短路径找出来。
注意到,Y+G不超过15,那么可以用状态压缩,记录每个点是否走过,现在在哪一个点上。用一个数组记录在某个状态下剩余的能量最多是多少。然后随便搞搞就可以水了。
PS:没有Y的时候应该是输出0吧。走到G的时候,就算没有能量了,也能充能。走到最后一个Y的时候,就算没有能量了,也能脱出。
 
代码(250MS):
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = << ;
const int MAXV = ; int fx[] = {-, , , };
int fy[] = {, , , -}; char mat[MAXV][MAXV];
int id[MAXV][MAXV], dis[MAXV][MAXV], Gcnt, Ycnt;
int n, m; bool isOn(int state, int i) {
return (state >> i) & ;
} int moveState(int state, int i) {
return state ^ ( << i);
} bool atEnd(int state) {
return state % ( << Ycnt) == ;
} bool isGYF(char c) {
return c == 'G' || c == 'Y' || c == 'F';
} int vis[MAXV][MAXV]; void bfsdis(int x, int y) {
memset(vis, 0x7f, sizeof(vis));
vis[x][y] = ;
queue<pair<int, int> > que;
que.push(make_pair(x, y));
int st = id[x][y];
while(!que.empty()) {
int x = que.front().first, y = que.front().second; que.pop();
if(isGYF(mat[x][y])) dis[st][id[x][y]] = vis[x][y];
for(int i = ; i < ; ++i) {
int tx = x + fx[i], ty = y + fy[i];
if(mat[tx][ty] != 'D' && vis[x][y] + < vis[tx][ty]) {
vis[tx][ty] = vis[x][y] + ;
que.push(make_pair(tx, ty));
}
}
}
} void init() {
Ycnt = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(mat[i][j] != 'Y') continue;
id[i][j] = Ycnt++;
}
}
Gcnt = Ycnt;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(mat[i][j] != 'G') continue;
id[i][j] = Gcnt++;
}
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(mat[i][j] != 'F') continue;
id[i][j] = Gcnt;
}
}
memset(dis, 0x7f, sizeof(dis));
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(!isGYF(mat[i][j])) continue;
bfsdis(i, j);
}
}
} int best[MAXN][MAXV];
bool inque[MAXN][MAXV]; bool check(int energy) {
memset(inque, , sizeof(inque));
memset(best, , sizeof(best));
best[( << Gcnt) - ][Gcnt] = energy;
queue<pair<int, int> > que;
que.push(make_pair(( << Gcnt) - , Gcnt));
while(!que.empty()) {
int state = que.front().first, pos = que.front().second; que.pop();
inque[state][pos] = false;
for(int i = ; i < Gcnt; ++i) {
if(!isOn(state, i)) continue;
int newState = moveState(state, i);
if(i >= Ycnt) {
if(best[state][pos] >= dis[pos][i] && best[newState][i] == ) {
best[newState][i] = energy;
inque[newState][i] = true;
que.push(make_pair(newState, i));
}
} else {
if(best[state][pos] - dis[pos][i] >= && atEnd(newState)) return true;
if(best[state][pos] - dis[pos][i] > best[newState][i]) {
best[newState][i] = best[state][pos] - dis[pos][i];
if(!inque[newState][i]) {
inque[newState][i] = true;
que.push(make_pair(newState, i));
}
}
}
}
}
return false;
} int solve() {
if(Ycnt == ) return ;
int st = Gcnt;
for(int i = ; i < Ycnt; ++i)
if(dis[i][st] > ) return -;
int l = , r = dis[st][];
for(int i = ; i < Ycnt - ; ++i) r += dis[i][i + ];
while(l < r) {
int mid = (l + r) >> ;
if(!check(mid)) l = mid + ;
else r = mid;
}
return l;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
memset(mat, 'D', sizeof(mat));
for(int i = ; i <= n; ++i) scanf("%s", &mat[i][]);
init();
printf("%d\n", solve());
}
}

HDU 3681 Prison Break(BFS+二分+状态压缩DP)的更多相关文章

  1. HDU 3681 Prison Break 越狱(状压DP,变形)

    题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...

  2. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

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

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

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

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

  5. hdu 3681 Prison Break (TSP问题)

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

  6. HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )

    题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...

  7. hdu 5067 Harry And Dig Machine (状态压缩dp)

    题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...

  8. HDU 5418 Victor and World (状态压缩dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...

  9. HDU 4649 Professor Tian(反状态压缩dp,概率)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...

随机推荐

  1. Bluetooth Low Energy介绍

    目录 1. 介绍 2. 协议栈 3. 实现方案 3.1 硬件实现方案 3.2 软件实现方案 1. 介绍 Bluetooth low energy,也称BLE(低功耗蓝牙),在4.0规范中提出 BLE分 ...

  2. Qt StyleSheet皮肤(黑色,比较好看,而且很全)

    使用方式如下 //设置皮肤样式 static void SetStyle(const QString &styleName) { QFile file(QString(":/imag ...

  3. QObject::deleteLater()并没有将对象立即销毁,而是向主消息循环发送了一个event,下一次主消息循环收到这个event之后才会销毁对象 good

    程序编译运行过程很顺利,测试的时候也没发现什么问题.但后来我随手上传了一个1G大小的文件,发现每次文件上传到70%左右的时候程序就崩溃了,小文件就没这个问题.急忙打开任务管理器,这才发现上传文件的时候 ...

  4. ActiveMQ持久化消息

    ActiveMQ的另一个问题就是只要是软件就有可能挂掉,挂掉不可怕,怕的是挂掉之后把信息给丢了,所以本节分析一下几种持久化方式: 一.持久化为文件 ActiveMQ默认就支持这种方式,只要在发消息时设 ...

  5. MongoDB直接执行js脚本

    有时候很大一段命令要执行,中间有错就得重新写,这样超麻烦的,不妨存放于js文件里,然后通过shell命令执行,重复利用率高,修改方便. 比如创建test.js print('=========WECO ...

  6. svn out of date

    out of date说明这个文件过期了,也就是已经有过一次提交的版本,当前提交的版本号小于当前的版本. 解决办法:先将文件update一下,然后再提交.

  7. FTP文件夹打开错误,Windows无法访问此文件夹

    错误提示: Windows 无法访问此文件夹,请确保输入的文件夹是正确的,并且你有权访问此文件夹.    解决方法/步骤如下    1.请确保输入的文件夹是正确的,并且你有权访问此文件夹.可以在浏览器 ...

  8. Insert BLOB && CLOB from PL/SQL and JDBC

    For PL/SQL 1)Create Directory Where BLOB resides. create or replace directory temp as '/oradata2'; - ...

  9. Linux脚本执行过程重定向

    Linux脚本执行过程重定向 一.bash调试脚本,并将执行过程重定向到指定文件 bash –x  shell.sh 2>&1 | tee shell.log

  10. dd命令使用详解

    dd命令使用详解 http://www.cnblogs.com/qq78292959/archive/2012/02/23/2364760.html 1.命令简介 dd 的主要选项: 指定数字的地方若 ...