Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4867    Accepted Submission(s): 1329

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.
 
Sample Input
5 5
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
0 0
 
Sample Output
4
 
Source
 
Recommend
lcy&zhengfeng

动态规划 状压DP 二分

停在普通点的状态肯定不需要保留,只记录每个特殊点(起点 开关 能量点)的位置,预处理出它们之间的距离。

压缩状态,f[bit][ter]表示当前到达过的点集为bit,停留在ter号点的最大剩余能量。

假定所有的点都只能停留一次(预处理时按可以多次经过来计算距离,而表示状态时多停留并没有用)

二分答案,判定是否可行

顺带一提,由于这份代码中用了大量STL,并且HDU貌似没有-O2,耗时成功垫底233

题目不算难,但状压dp细节好麻烦,位运算时候总是一眼花就写错变量,看好半天才能发现,就很气。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int mxn=;
const int mx[]={,,,,-};
const int my[]={,,,-,};
int f[][<<];
char mp[][];
int n,m;
int sx,sy;
int yx[],yy[],yct=;
int b[],bct=;int tar;
int dis[][][][];
bool vis[][];
void init(){
memset(dis,0x3f,sizeof dis);
memset(f,-,sizeof f);
return;
}
void BFS(int sx,int sy){
dis[sx][sy][sx][sy]=;
memset(vis,,sizeof vis);
queue<pair<int,int> >q;
q.push(make_pair(sx,sy));
vis[sx][sy]=;
while(!q.empty()){
int x=q.front().first,y=q.front().second;q.pop();
for(int i=;i<=;i++){
int nx=x+mx[i],ny=y+my[i];
if(nx< || nx>n || ny< || ny>m)continue;
if(vis[nx][ny] || mp[nx][ny]=='D')continue;
vis[nx][ny]=;
dis[sx][sy][nx][ny]=dis[sx][sy][x][y]+;
q.push(make_pair(nx,ny));
}
}
return;
}
bool solve(int lim){
memset(f,-,sizeof f);
f[][]=lim;
int i,j,k,ed=(<<(yct+))-;
for(i=;i<=ed;i++){
for(j=;j<=yct;j++){
if(!(i&b[j]))continue;
if((i&tar)==tar && f[j][i]!=-)return ;
if(f[j][i]==-)continue;
for(k=;k<=yct;k++){
if(j==k || (i&b[k]))continue;
int dist=f[j][i]-dis[yx[j]][yy[j]][yx[k]][yy[k]];
if(dist<)continue;
f[k][i|b[k]]=max(f[k][i|b[k]],dist);
if(mp[yx[k]][yy[k]]=='G'){f[k][i|b[k]]=lim;}
}
}
}
return ;
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m) && n && m){
init();bct=yct=;
tar=;
for(i=;i<=n;i++)scanf("%s",mp[i]+);
for(i=;i<=n;i++)
for(j=;j<=m;j++){
if(mp[i][j]=='F'){yx[]=i,yy[]=j;}
else if(mp[i][j]=='Y'){yx[++yct]=i;yy[yct]=j;b[yct]=<<(++bct);tar|=b[yct];}
else if(mp[i][j]=='G'){yx[++yct]=i;yy[yct]=j;b[yct]=<<(++bct);}
}
b[]=;
/* for(i=0;i<=yct;i++){
printf("%d %d",yx[i],yy[i]);
printf(" type:%c\n",mp[yx[i]][yy[i]]);
printf("b:%d\n",b[i]);
}
printf("tat:%d\n",tar);*/
for(i=;i<=yct;i++)BFS(yx[i],yy[i]);
/* for(i=0;i<=yct;i++)
for(j=0;j<=yct;j++){
printf("(%d %d) to (%d %d):",yx[i],yy[i],yx[j],yy[j]);
printf("%d\n",dis[yx[i]][yy[i]][yx[j]][yy[j]]);
}
*/
int l=,r=n*m*,ans=1e8;
while(l<=r){
int mid=(l+r)>>;
if(solve(mid)){
ans=mid;
r=mid-;
}
else l=mid+;
}
if(ans<=n*m*)printf("%d\n",ans);
else printf("-1\n");
}
return ;
}

HDU3681 Prison Break的更多相关文章

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

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

  2. hdu 3681 Prison Break (TSP问题)

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

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

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

  4. Prison Break

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

  5. hdu3511 Prison Break 圆的扫描线

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

  6. 1254 - Prison Break

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

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

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

  8. hdu 3681 Prison Break

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...

  9. light oj 1254 - Prison Break 最短路

    题目大意:n个点m条边的有向图,q次询问c,s,t,表示汽车邮箱容量为c,求从起点s到终点t的最小费用.汽车在每个点可以加任意的油,每个点的单位油价为a[i]. 题目思路:利用最小费优先队列优化最短路 ...

随机推荐

  1. python基础(一)简单入门

    一.第一个python程序 1.交互式编程 直接在命令行里面输入python即可进入python交互式命令行,linux下一样: 在 python 提示符中输入以下文本信息,然后按 Enter 键查看 ...

  2. 这些天php面试的总结

    面试总结 记录一些本人在面试中遇到的觉得有些掌握不好的面试题,下面的答案都是本人回答的,如果哪里不对的话,希望各位能够指出. 1.Git fetch和git pull的区别 Git fetch相当于从 ...

  3. windows系统,可以ping通IP但是不能ping通网址的解决方法

    之前慌忙之中遇到过一次,当时是客户比较着急使用就没有怎么折腾,什么数据当时都没留下反正是各种方法都尝试过了,但是就是ping IP是可以通的,但是域名就是不解析,后来有个群友也是遇见了这个问题(我当时 ...

  4. 【转】MySQL数据类型

    1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) mediumint(m) ...

  5. jdbc 小结

    1,PreparedStatement/Statement区别: 1,防止sql注入式攻击(sql注入:就是通过非正常手段(比如在url中添加参数)),将sql文执行(比如or 1=1) 2,Prep ...

  6. 网络流量统计using ADB

    /proc/net/xt_qtaguid/stats 基本覆盖目前所有机型且统计流量全面 adb shell cat /proc/net/xt_qtaguid/stats | grep (uid#) ...

  7. 【bzoj1005】[HNOI2008]明明的烦恼 Prufer序列+高精度

    题目描述 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? 输入 第一行为N(0 < N < = 1000),接下来N行,第i+1行给出第i ...

  8. 【BZOJ3028】食物(生成函数)

    [BZOJ3028]食物(生成函数) 题面 一个人要带\(n\)个物品,共有\(8\)种物品,每种的限制分别如下: 偶数个;0/1个;0/1/2个;奇数个; 4的倍数个;0/1/2/3个;0/1个;3 ...

  9. BZOJ4735 你的生命已如风中残烛 【数学】

    题目链接 BZOJ4735 题解 给定一个序列,有的位置为\(w_i - 1\),有的位置为\(-1\),问有多少种排列,使得任意前缀和非负? 我们末尾加上一个\(-1\),就是要保证除了末尾外的前缀 ...

  10. NOI2013 矩阵游戏 【数论】

    题目描述 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i ...