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. boost格式化输出xml

    我的boost为1.56而不是1.55 boost在xml的例子给出了一段写xml文件的代码,我简化如下: void debug_settings::save(const std::string &a ...

  2. wxpython 布局管理

    一个典型的应用程序是由不同的部件.这些小部件被放进容器部件.一个程序员必须管理应用程序的布局.这不是一项容易的任务.在wxPython我们有两个选择. *absolute positioning*si ...

  3. 用window.print()打印指定div里面的内容

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...

  4. HDU 4122 Alice's mooncake shop (单调队列/线段树)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4122 题意:好难读懂,读懂了也好难描述,亲们就自己凑合看看题意把 题解:开始计算每个日期到2000/1/ ...

  5. 在OC和Swift中使用IBDesignable/IBInspectable

    iOS8新特性IBDesignable/IBInspectable,可以直接在XIB或者Storyboard中直接,设置UI类的属性.例 如:UIView.layer.borderWidth.bord ...

  6. gdalwarp:变形工具

    1 gdalwarp:变形工具.包括投影.拼接.及相关的变形功能.此工具功能强大,但效率不高,使用时注意 gdalwarp [--help-general] [--formats]     [-s_s ...

  7. Android开发记录(转)

    一.Android模拟器相关 1. Android模拟器安装 Market 模拟器默认没有安装 Market,看到网上有较为复杂的安装方法,也有1个简单的,试了简单的,在 Android2.2 模拟器 ...

  8. ES6 let和const命令

    一.let定义变量 { let a = 1;} console.log(a);只在let所在的代码块有效,console的结果是a is not defined,报错. 不存在var的变量提升,即使用 ...

  9. 批量数据上传的sql.xml

    <!-- User.xml --><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE ...

  10. C++服务器设计(四):超时管理机制设计

    前四章介绍了系统层的设计,从这一章开始进入服务层的设计. 连接断开 在常见的服务器场景中,客户端断开连接的方式为被动关闭.即作为客户端请求完服务器的服务后,选择主动关闭同服务器的连接.在服务器的角度看 ...