HDU 3681 Prison Break (State compression + BFS + Shortest Path)

Source: Internet
Author: User
Tags integer numbers

Prison Break Time Limit: 5000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3214 accepted submission (s): 829


Problem descriptionrompire 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.

Inputinput 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.
Outputfor 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 5GDDSSSSSFSSYGYSSGSYSSSYSS0 0

Sample output
4


Idea: First extract the Special Points (switch position, rechargeable battery, start position), label them, and then find the shortest distance between them. Then, enumerate the possible initial energy values and perform DP to check whether the values can be in the target State.


#include<iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<queue>#include<algorithm>#include<iostream>using namespace std;#define N 20const int inf=10000;int n1,n2,n,m,f;char g[N][N];int x[N],y[N];int dir[4][2]={0,1,0,-1,-1,0,1,0};int dis[N][N],dist[N][N];int dp[1<<15][N];void inti()  //读入数据,把特殊点标号{    n1=0;    int i,j;    for(i=0; i<n; i++)    {        scanf("%s",g[i]);        for(j=0; j<m; j++)        {            if(g[i][j]=='F')            {                f=n1;                x[n1]=i;                y[n1++]=j;            }            else if(g[i][j]=='Y')            {                x[n1]=i;                y[n1++]=j;            }        }    }    n2=n1;    for(i=0; i<n; i++)        for(j=0; j<m; j++)            if(g[i][j]=='G')            {                x[n2]=i;                y[n2++]=j;            }}void bfs(int x,int y,int dis[][20]) //求一个特殊点到其他各个点最短路{    int i,j,u,v;    queue<int>q1,q2;    q1.push(x);    q2.push(y);    for(i=0;i<n;i++)        for(j=0;j<m;j++)        dis[i][j]=inf;    dis[x][y]=0;    while(!q1.empty())    {        x=q1.front();        y=q2.front();        q1.pop();        q2.pop();        for(i=0;i<4;i++)        {            u=x+dir[i][0];            v=y+dir[i][1];            if(u<0||u>=n||v<0||v>=m||g[u][v]=='D')                continue;            if(dis[u][v]>dis[x][y]+1)            {                dis[u][v]=dis[x][y]+1;                q1.push(u);                q2.push(v);            }        }    }}bool findd(int t){    int i,j,k,lim=1<<n2,tmp=(1<<n1)-1;    for(i=0;i<lim;i++)        for(j=0;j<n2;j++)        dp[i][j]=-inf;    dp[1<<f][f]=t;    for(i=1<<f;i<lim;i++)    {        for(j=0;j<n2;j++)        {            if(dp[i][j]<0)                continue;            if((i&tmp)==tmp) //不一定要相等,包含目标状态就行                return true;            for(k=0;k<n2;k++)            {                int p=1<<k;                if(i&p)                    continue;                dp[i|p][k]=max(dp[i|p][k],dp[i][j]-dis[j][k]);                if(dp[i|p][k]>=0&&k>=n1)                    dp[i|p][k]=t;            }        }    }    return false;}int main(){    int i,j;    while(scanf("%d%d",&n,&m),n||m)    {        inti();        for(i=0; i<n2; i++)        {            bfs(x[i],y[i],dist);            for(j=0; j<n2; j++)  //记录标号后的点相互之间距离            {                dis[i][j]=dist[x[j]][y[j]];             }  //i到j的距离等于i点到各个点的最短路        }                int l=0,r=300,flag=0;        while(l<=r)       //二分枚举各个可能的能量值        {            int mid=(l+r)/2;            if(findd(mid))            {                flag=1;                r=mid-1;            }            else                l=mid+1;        }        if(!flag)            l=-1;        printf("%d\n",l);    }    return 0;}


HDU 3681 Prison Break (State compression + BFS + Shortest Path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.