BFS最短路+状态dp(hdu3681)好

Prison Break

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
思路:首先处理处任意两个Y,G的最短路,然后二分,dp
dp[i][j]表示当前访问的G,Y的集合为i,处于j的最大剩余能量

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn=20;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
struct node{
    int x,y;
    node(){}
    node(int _x,int _y):x(_x),y(_y){}
}p[maxn];
char g[maxn][maxn];
int N,M;
int sx,sy;
int cnt;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int id[maxn][maxn];
int st;
int dis[maxn][maxn][maxn][maxn];
int SY;
void BFS(int x,int y){
    queue q;
    q.push(node(x,y));
    dis[x][y][x][y]=0;
    while(!q.empty()){
        node a=q.front();q.pop();
        for(int i=0;i<4;i++){
            int tx=a.x+dx[i],ty=a.y+dy[i];
            if(tx<0||tx>=N||ty<0||ty>=M||g[tx][ty]=='D')continue;
            if(dis[x][y][tx][ty]!=-1)continue;
            dis[x][y][tx][ty]=dis[x][y][a.x][a.y]+1;
            q.push(node(tx,ty));
        }
    }
}
int dp[1<<16][maxn];
bool can(int sz){
    memset(dp,-1,sizeof(dp));
    dp[1<int ans=0;
    for(int i=0;i<(1<for(int j=0;jif(dp[i][j]==-1||((i&(1<0))continue;
            if((i&SY)==SY){
                if(dp[i][j]>=0)return true;
            }
            for(int k=0;kif((i&(1<continue;
                int d=dis[p[j].x][p[j].y][p[k].x][p[k].y];
                if(dp[i][j]-d<0||d<0)continue;
                dp[i|(1<1<if(g[p[k].x][p[k].y]=='G')dp[i|(1<return false;
}
int main(){
    while(scanf("%d%d",&N,&M)!=EOF,N+M){
        cnt=SY=0;
        memset(id,-1,sizeof(id));
        for(int i=0;iscanf("%s",&g[i]);
            for(int j=0;jif(g[i][j]=='F'){
                    st=cnt;
                    SY|=(1<else if(g[i][j]=='Y'){
                    id[i][j]=cnt;
                    SY|=(1<else if(g[i][j]=='G'){
                    p[cnt].x=i,p[cnt].y=j;
                    cnt++;
                }
            }
        }
        memset(dis,-1,sizeof(dis));
        for(int i=0;iint l=0,r=1000;
        while(lint mid=(l+r)>>1;
            if(can(mid))r=mid;
            else l=mid+1;
        }
        printf("%d\n",l>=1000?-1:l);
    }
    return 0;
}

你可能感兴趣的:(最佳路径,动态规划,BFS)