Prison Break
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5110 Accepted Submission(s): 1406
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.
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
题意:给你一个n*m的方格,F代表机器人的起点、Y代表开关、G代表电池、D代表“墙”、S代表空地,机器人走到电池的地方是,机器人自身电量充满,但是该地方之后便成了平地,也就是一个能量池只能给机器人冲一次电,有开关的地方也是经过之后便成了平地,之后给你个起点“F”,问你机器人的电源电量未知,机器人将整个地图中的开关关掉,机器人所需的最小电源电量是多少。。
题解:题目不难写,只是有点烦,思路很明确,首先处理出地图上所有“特殊点”,也就是能量池、开关、起点。将这些点任意两点的距离用BFS求出来,然后既然不知道机器人的初始电源电量,呢就二分机器人的电源电量即可,剩下的就是走这些点的顺序,用状压搞搞就行了,思路还是很清晰的吧Orz。。。
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define mod 100000000
#define maxn 100015
#define lowbit(x) (x&-x)
#define eps 1e-10
int dp[16][1<<16],n,m,vis[16][16],b[16];
int d[16][16][16][16],r1[16],r2[16],cnt,res,num;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char s[18][18];
void init()
{
cnt=res=num=0;
memset(d,127,sizeof(d));
memset(dp,-1,sizeof(dp));
return;
}
void bfs(int x,int y)
{
d[x][y][x][y]=0;
memset(vis,0,sizeof(vis));
queue<pair<int,int>>q;
q.push(make_pair(x,y));
vis[x][y]=1;
while(!q.empty())
{
int xx=q.front().first,yy=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int t1=xx+dx[i],t2=yy+dy[i];
if(t1<1 || t1>n || t2<1 || t2>m)
continue;
if(vis[t1][t2] || s[t1][t2]=='D')
continue;
vis[t1][t2]=1;
d[x][y][t1][t2]=d[x][y][xx][yy]+1;
q.push(make_pair(t1,t2));
}
}
}
bool work(int x)
{
memset(dp,-1,sizeof(dp));
dp[0][1]=x;int i,j,k,nn=(1<<(cnt+1))-1;
for(i=0;i<=nn;i++)
{
for(j=0;j<=cnt;j++)
{
if(!(i&b[j]))
continue;
if((i&num)==num && dp[j][i]!=-1)
return 1;
if(dp[j][i]==-1)
continue;
for(k=0;k<=cnt;k++)
{
if(j==k || (i&b[k]))
continue;
int dis=dp[j][i]-d[r1[j]][r2[j]][r1[k]][r2[k]];
if(dis<0)continue;
dp[k][i|b[k]]=max(dp[k][i|b[k]],dis);
if(s[r1[k]][r2[k]]=='G')
dp[k][i|b[k]]=x;
}
}
}
return 0;
}
int main(void)
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)
break;
init();
for(i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if(s[i][j]=='F')r1[0]=i,r2[0]=j;
else if(s[i][j]=='Y')r1[++cnt]=i,r2[cnt]=j,b[cnt]=1<<(++res),num|=b[cnt];
else if(s[i][j]=='G')r1[++cnt]=i,r2[cnt]=j,b[cnt]=1<<(++res);
}
b[0]=1;//num|=b[0];
for(i=0;i<=cnt;i++)
bfs(r1[i],r2[i]);
/*for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%d ",d[2][4][i][j]);
printf("\n");
}*/
int l=0,r=1000,ans=inf;
while(l<=r)
{
int mid=(l+r)/2;
if(work(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
//printf("%d\n",ans);
if(ans>=inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}