您的位置:首页 > 其它

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

2015-09-22 17:03 453 查看
[align=left]Problem Description[/align]

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.


[align=left]Input[/align]

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.


[align=left]Output[/align]

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.


[align=left]Sample Input[/align]

5 5
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
0 0


[align=left]Sample Output[/align]

4


[align=left]Source[/align]
2010 Asia Hangzhou Regional Contest

状态压缩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[]={0,0,-1,1};
int diry[]={-1,1,0,0};
#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

;
int states;
int final_state;//要达到的目标状态
int start;
int dis

;
int dp[1<<N]
;
int vis
;

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]=0;
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=0;i<4;i++){
//t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
if(mp[t2.x][t2.y]=='D') continue;
if(dis[x][y][t2.x][t2.y]!=-1) continue;
dis[x][y][t2.x][t2.y]=dis[x][y][t1.x][t1.y]+1;
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=0;i<states;i++){
if(dis[node[st].x][node[st].y][node[i].x][node[i].y]==-1) 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]=1;
if(dfs(i,sta|(1<<i),mid,mid)){
return true;
}
vis[i]=0;
}
else{
vis[i]=1;
if(dfs(i,sta|(1<<i),limit-dis[node[st].x][node[st].y][node[i].x][node[i].y],mid)){
return true;
}
vis[i]=0;
}

}
return false;

}
int main()
{
while(scanf("%d%d",&n,&m)==2){
if(n==0 && m==0){
break;
}
states=0;
final_state=0;
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
for(int j=0;j<m;j++){
if(mp[i][j]=='F'){
node[states].x=i;
node[states].y=j;
start=states;
final_state+=(1<<states);
states++;
}
else if(mp[i][j]=='Y'){
node[states].x=i;
node[states].y=j;
final_state+=(1<<states);
states++;
}
else if(mp[i][j]=='G'){
node[states].x=i;
node[states].y=j;
states++;
}
}
}

memset(dis,-1,sizeof(dis));
for(int i=0;i<states;i++){
bfs(i);
}//两两之间的最短距离已求出,保存于dis

int low=0;
int high=300;
int ans=-1;
while(low<=high){
int mid=(low+high)>>1;
memset(vis,0,sizeof(vis));
vis[start]=1;
if(dfs(start,1<<start,mid,mid)){
ans=mid;
high=mid-1;
}
else{
low=mid+1;
}
}
printf("%d\n",ans);

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: