hdu 3681 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
 

Source
The 35th ACM-ICPC Asia Regional Contest (Hangzhou)
 

Recommend
lcy&zhengfeng


这题做死人了……coding能力好差啊……突然发现……

这题题目意思是说有个机器人在F位置

Y表示开关

D表示障碍

G表示一次性充电器

机器人每次只能上下左右走,每走一步消耗一点电量,如果木有电量当然就走不了了,然后走到一次性充电器可以充满电,但是这个充电器就报废了(一次性的嘛),障碍是不能走的地方,问机器人遍历一次开关最少需要带电容量为多大的电池

把所有的G和Y找出来,然后做一次TSP即可,注意G可以不用完全遍历。

首先先找G和Y,再加上起点做一次BFS求两两之间的最短路,之后二分答案,对每个答案用状态dp判断是否能够遍历所有的Y。

送上代码

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>

#define INF 99999999

using namespace std;

typedef struct
{
    int x,y;
    int val;//'G':1;'Y':2
    int step;
}point;

int n,m,up;
char map[20][20];
int bfs[20][20];
point hash[20];
int start[20];
queue <point> q;
int vis[20][20];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dp[(1<<15)+2][16];

bool cmp(point a,point b)
{
    return a.val<b.val;
}

int BFS(point a,point b)
{
    point tag,tag1;
    int i;
    while(!q.empty())
    {
        q.pop();
    }
    a.step=0;
    q.push(a);
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
        tag=q.front();
        q.pop();
        if (tag.x==b.x && tag.y==b.y) return tag.step;
        if (tag.x<0 || tag.x>=n || tag.y<0 || tag.y>=m) continue;
        if (vis[tag.x][tag.y]==1) continue;
        if (map[tag.x][tag.y]=='D') continue;
        vis[tag.x][tag.y]=1;
        tag1=tag;
        tag1.step++;
        for (i=0;i<4;i++)
        {
            tag1.x=tag.x+dx[i];
            tag1.y=tag.y+dy[i];
            q.push(tag1);
        }
    }
    return INF;
}

bool Check(int t)
{
    int i,j,k,l,p;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for (j=0;j<(1<<up);j++)
    {
        for (k=0;k<up;k++)
        {
            if (dp[j][k]==-1) continue;
       //     printf("%d..%d..%d..\n",j,k,dp[j][k]);
            if (j==0)
            {
                for (l=0;l<up;l++)
                {
                    if (t<start[l]) continue;
                    if (hash[l].val==1) dp[1<<l][l]=t;
                    else dp[1<<l][l]=t-start[l];
                }
                continue;
            }
            for (l=0;l<up;l++)
            {
                if ((j & (1<<l))!=0) continue;
                p=(j | (1<<l));
                if (bfs[k][l]>dp[j][k]) continue;
                if (hash[l].val==1) dp[p][l]=t;
                else dp[p][l]=max(dp[p][l],dp[j][k]-bfs[k][l]);
            }
        }
    }
    for (i=0;i<up;i++)
    {
        if (hash[i].val==2) break;
    }
    for (j=(1<<up)-(1<<i);j<(1<<up);j++)
    {
        for (k=0;k<up;k++)
        {
        //    printf("%d...%d...%d\n",j,k,dp[j][k]);
            if (dp[j][k]>=0) return true;
        }
    }
    return false;
}

int main()
{
    int i,j,l,r,ok,mid;
    point beg;
    while(1)
    {
        scanf("%d%d",&n,&m);
        if (n==0 && m==0) break;
        for (i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        up=0;
        for (i=0;i<n;i++)
        {
            for (j=0;j<m;j++)
            {
                if (map[i][j]=='G')
                {
                    hash[up].x=i;
                    hash[up].y=j;
                    hash[up++].val=1;
                }
                if (map[i][j]=='Y')
                {
                    hash[up].x=i;
                    hash[up].y=j;
                    hash[up++].val=2;
                }
                if (map[i][j]=='F')
                {
                    beg.x=i;
                    beg.y=j;
                }
            }
        }
        if (up==0)
        {
            printf("1\n");
            continue;
        }
        ok=0;
        sort(hash,hash+up,cmp);
        for (i=0;i<up;i++)
        {
            for (j=i+1;j<up;j++)
            {
                bfs[i][j]=bfs[j][i]=BFS(hash[i],hash[j]);
            }
            start[i]=BFS(beg,hash[i]);
        }
        for (i=0;i<up;i++)
        {
            if (start[i]==INF && hash[i].val==2) break;
        }
        if (i<up)
        {
            printf("-1\n");
            continue;
        }
        l=0;
        r=n*m;
        while(l<=r)
        {
            mid=(l+r)/2;
     //       printf("%d..%d..%d\n",l,r,mid);
            if (Check(mid)==true) r=mid-1;
            else l=mid+1;
        }
        printf("%d\n",r+1);
    }
    return 0;
}


你可能感兴趣的:(Integer,equals,UP,each,output,Numbers)