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
二分+dp验证
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=20;
int n,m,t1,t2,ans;
int a[maxn],b[maxn],c[maxn][maxn],dis[maxn][maxn];
int f[1<<16][16],u[1<<16][16];
char s[maxn][maxn];
struct point
{
int x,y,d;
point(int x=0,int y=0,int d=0):x(x),y(y),d(d){};
};
void bfs(int x,int y)
{
int map[maxn][maxn];
memset(map,0,sizeof(map));
queue<point> p;
int now=c[x][y]-1;
for (int i=0;i<t2;i++) dis[now][i]=-1;
p.push(point(x,y,0));
while (!p.empty())
{
point q=p.front(); p.pop();
if (map[q.x][q.y]) continue;
map[q.x][q.y]=1;
if (c[q.x][q.y]>0) dis[now][c[q.x][q.y]-1]=q.d;
if (c[q.x][q.y]>-1)
{
p.push(point(q.x-1,q.y,q.d+1));
p.push(point(q.x,q.y-1,q.d+1));
p.push(point(q.x+1,q.y,q.d+1));
p.push(point(q.x,q.y+1,q.d+1));
}
}
}
bool prove(int x)
{
n=(1<<t1)-1; m=(1<<t2)-1;
memset(f,-1,sizeof(f));
f[1][0]=x;
for (int i=1;i<=m;i+=2)
{
for (int j=0;j<t2;j++)
if ((i&(1<<j))&&f[i][j]!=-1)
for (int k=1;k<t2;k++)
if (((i|(1<<k))>i)&&dis[j][k]!=-1&&f[i][j]>=dis[j][k])
{
if ((f[i|(1<<k)][k]==-1)||(f[i|(1<<k)][k]<f[i][j]-dis[j][k]))
{
f[i|(1<<k)][k]=f[i][j]-dis[j][k];
if (k>=t1) f[i|(1<<k)][k]=x;
if (((i|(1<<k))&n)==n) return true;
}
}
}
return false;
}
int ef()
{
int q,h,mid,ans=-1;
for (q=1,h=m*n*t2;q<=h;)
{
mid=(q+h)>>1;
if (prove(mid))
{h=mid-1; ans=mid;}else q=mid+1;
}
return ans;
}
int main()
{
while (cin>>n>>m,n+m)
{
t1=t2=0;
memset(c,-1,sizeof(c));
for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (s[i][j]=='F')
{
a[t2]=i,b[t2++]=j;
c[i][j]=t2;
}
else if (s[i][j]=='S') c[i][j]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (s[i][j]=='Y')
{
a[t2]=i,b[t2++]=j;
c[i][j]=t2;
}
t1=t2;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (s[i][j]=='G')
{
a[t2]=i,b[t2++]=j;
c[i][j]=t2;
}
for (int i=0;i<t2;i++) bfs(a[i],b[i]);
cout<<ef()<<endl;
}
return 0;
}