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#. Now it’s time to escape, but Micheal# 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:
) Empty area, represented by a capital letter ‘S’.
) The starting position of Micheal#, represented by a capital letter ‘F’.
) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal# can use it to charge his battery ONLY ONCE. After the charging, Micheal#’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.
) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal# cannot step into a grid with a laser sensor.
) Power switch, represented by a capital letter ‘Y’. Once Micheal# steps into a grid with a Power switch, he will certainly turn it off. In order to escape from the jail, Micheal# 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 unit of energy and only moving operation costs energy. Of course, Micheal# 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# 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# 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  . 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 <=n,m<=, and the sum of energy pools and power switches is less than .
 
Output
For each test case, output one integer in a line, representing the minimum size of the battery Micheal# needs. If Micheal# can’t escape, output -.
Sample Input
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
 
Sample Output

 
Source
 

状态压缩dp+bfs

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 17
#define inf 1e12
int n,m;
char mp[N][N];
int states;
int final_state;//要达到的目标状态
int start;
int dis[N][N][N][N];
int dp[<<N][N]; struct Node{
int x,y;
}node[N*N];
void bfs(int st){//每一点和其他点的最短距离
int x=node[st].x;
int y=node[st].y;
queue<Node>q;
q.push(node[st]);
dis[x][y][x][y]=;
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
//t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
if(mp[t2.x][t2.y]=='D') continue;
if(dis[x][y][t2.x][t2.y]!=-) continue;
dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
q.push(t2);
}
}
}
bool DP(int limit){
memset(dp,-,sizeof(dp));
dp[(<<start)][start]=limit;
int res=-;
for(int i=;i<(<<states);i++){
for(int j=;j<states;j++){
if((i&(<<j))==)continue;
if(dp[i][j]==-) continue;
if((i&(final_state))==final_state){
res=max(res,dp[i][j]);
} for(int k=;k<states;k++){
if((i&(<<k))!=) continue;
if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-) continue;
if(j==k) continue;
if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
dp[i|(<<k)][k]=max(dp[i|(<<k)][k],dp[i][j]-dis[node[j].x][node[j].y][node[k].x][node[k].y]);
if(mp[node[k].x][node[k].y]=='G') dp[i|(<<k)][k]=limit;
} }
}
return res>=; }
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
states=;
final_state=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='F'){
node[states].x=i;
node[states].y=j;
start=states;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='Y'){
node[states].x=i;
node[states].y=j;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='G'){
node[states].x=i;
node[states].y=j;
states++;
}
}
} memset(dis,-,sizeof(dis));
for(int i=;i<states;i++){
bfs(i);
}//两两之间的最短距离已求出,保存于dis int low=;
int high=;
int ans=-;
while(low<=high){
int mid=(low+high)>>;
if(DP(mid)){
ans=mid;
high=mid-;
}
else{
low=mid+;
}
}
printf("%d\n",ans); }
return ;
}

TLE代码,想不通

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 17
#define inf 1e12
int n,m;
char mp[N][N];
int states;
int final_state;//要达到的目标状态
int start;
int dis[N][N][N][N];
int dp[<<N][N];
int vis[N]; struct Node{
int x,y;
}node[N*N];
void bfs(int st){//每一点和其他点的最短距离
int x=node[st].x;
int y=node[st].y;
queue<Node>q;
q.push(node[st]);
dis[x][y][x][y]=;
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
//t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
if(mp[t2.x][t2.y]=='D') continue;
if(dis[x][y][t2.x][t2.y]!=-) continue;
dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+;
q.push(t2);
}
}
}
/*bool DP(int limit){
memset(dp,-1,sizeof(dp));
dp[(1<<start)][start]=limit;
int res=-1;
for(int i=0;i<(1<<states);i++){
for(int j=0;j<states;j++){
if((i&(1<<j))==0)continue;
if(dp[i][j]==-1) continue;
if((i&(final_state))==final_state){
res=max(res,dp[i][j]);
} for(int k=0;k<states;k++){
if((i&(1<<k))!=0) continue;
if(dis[node[j].x][node[j].y][node[k].x][node[k].y]==-1) continue;
if(j==k) continue;
if(dp[i][j]<dis[node[j].x][node[j].y][node[k].x][node[k].y]) continue;
dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-dis[node[j].x][node[j].y][node[k].x][node[k].y]);
if(mp[node[k].x][node[k].y]=='G') dp[i|(1<<k)][k]=limit;
} }
}
return res>=0; }
*/ bool dfs(int st,int sta,int limit,int mid){ //if(limit<0) return false; if( (sta & final_state) == final_state){
return true;
} for(int i=;i<states;i++){
if(dis[node[st].x][node[st].y][node[i].x][node[i].y]==-) continue;
if(vis[i] || limit<dis[node[st].x][node[st].y][node[i].x][node[i].y]) continue; if(mp[node[i].x][node[i].y]=='G'){
vis[i]=;
if(dfs(i,sta|(<<i),mid,mid)){
return true;
}
vis[i]=;
}
else{
vis[i]=;
if(dfs(i,sta|(<<i),limit-dis[node[st].x][node[st].y][node[i].x][node[i].y],mid)){
return true;
}
vis[i]=;
} }
return false; }
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
states=;
final_state=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='F'){
node[states].x=i;
node[states].y=j;
start=states;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='Y'){
node[states].x=i;
node[states].y=j;
final_state+=(<<states);
states++;
}
else if(mp[i][j]=='G'){
node[states].x=i;
node[states].y=j;
states++;
}
}
} memset(dis,-,sizeof(dis));
for(int i=;i<states;i++){
bfs(i);
}//两两之间的最短距离已求出,保存于dis int low=;
int high=;
int ans=-;
while(low<=high){
int mid=(low+high)>>;
memset(vis,,sizeof(vis));
vis[start]=;
if(dfs(start,<<start,mid,mid)){
ans=mid;
high=mid-;
}
else{
low=mid+;
}
}
printf("%d\n",ans); }
return ;
}

hdu 3681 Prison Break(状态压缩+bfs)的更多相关文章

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

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

  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. HDU 3681 Prison Break 越狱(状压DP,变形)

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

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

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

  6. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

  7. hdu 3681 Prison Break

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

  8. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  9. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. JSP通过SmartUpload上传文件实例

    httpRequest.setCharacterEncoding("gbk"); String preName = genName.doMake();//设置文件前缀名 Strin ...

  2. PHP商城购物车类

    <?php /* 购物车类 */ // session_start(); class Cart { //定义一个数组来保存购物车商品 private $iteams; private stati ...

  3. TabBarItem图片大小改变

    在TabBarItem设计的时候不需要title只要image的时候,如何将image居中显示. tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, ...

  4. 用cflow工具生成代码函数调用关系

    1. 安装 sudo apt-get install cflow 2.使用 cflow [options...] [file]... 例: cflow main.c 生成main.c文件例的函数调用关 ...

  5. CCS v5 无法启动解决办法及Launchpad仿真器电脑无法识别解决方法

    安装ccs_setup_5.1.1.00028.exe后(无论是自己装eclipse还是在原来的基础上安装eclipse的插件),ccs5的应用无法打开,错误为:An error has occurr ...

  6. 注册界面设计及实现之(三)SharedPerferences实现数据暂存

    开发步骤: 创建一个SharedPerferences接口对象,并使用其putString方法放入相关的公共数据 将验证通过的注册账号写入到该文件中 将数据进行提交 给出客户提示 //Register ...

  7. Java Timer触发定时器

    XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...

  8. (转)Eclipse Shortcuts

    原文地址: http://javapapers.com/core-java/eclipse-shortcuts/ Editors are an integral part of a programme ...

  9. C# 对类中的保护成员进行写操作(邀请大家拍砖)

    假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...

  10. css样式书写顺序

    这里推荐先写显示属性,再写自身属性,再写文字属性:并不代表非得按这个顺序写,但这种写法可以使css结构更清晰易读,修改起来比较方便. 而且在团队项目中能更好地提高效率. //显示属性 display ...