UVALive 5066 Fire Drill BFS+背包

H - Fire Drill
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Joko is taking part in a fire drill which is held by the Jakarta Fire Department to recruit new firemen. The drill is about rescuing volunteers (who act as unconscious people) trapped in a building in a limited time. The building has several floors, and the volunteers are scattered throughout the building. Each volunteer has points assigned to her. The fireman candidate should rescue volunteers through carrying them to the exit. The candidate will earn the assigned points for each volunteer he rescued.

Each floor of a building can be seen as a grid of cells. Each cell can be an obstacle, an empty space, a stair or an entry/exit point.

A candidate starts at the entry point which exists only at one single cell of the first floor of the building. The candidate can move to any adjacent non-obstacle cells (north, south, west or east) or climb up or down a stair in 1 second. The movement slows down to 2 seconds when the candidate carries a volunteer. When a candidate finds a volunteer, he may decide to rescue her or not, but if he decides to rescue her, he has to carry her back to the exit without stopping. He can only carry at most one volunteer at a time.

Joko has the floor plan of the test building. Help him plan his moves, so he can get the highest possible score.

Input

The first line of input contains an integer T(T100) denoting the number of case. Each case has five integers L(1L10), H(1H100), W(1W100), N(1N100) and S(1S10, 000) denoting the number of floors, height and weight of each floor, the number of unconscious people, and the given time respectively.

The next L blocks describe the map of each floor from the 1st floor to the L-th floor respectively. Each floor consists of H lines each contains W characters. Characters that may appear in each floor are:

  • ``S" : The starting point, also serves as the exit point. There will be only one starting/exit point and it will appear in the first floor.
  • ``X" : Obstacle, cell that cannot be visited (wall, fire, etc.).
  • ``U" : Stair that connect to the upper floor. There will be a ``D" character at the same place in the upper level. This character will not appear in the highest level of the building.
  • ``D" : Stair that connect to the lower floor. There will be a ``U" character at the same place in the lower level. This character will not appear in the lowest level of the building.
  • ``." : Empty space, cell that can be visited.


The next N lines each contains four integers fi(1fiL), ri(1riH), ci(1ciW), pi(1pi1, 000) denoting the location of each volunteer (floor, row, column) and the point assigned to this volunteer respectively. You can assume that each volunteer will be located in empty space and no two volunteer occupy the same location.

Output

For each case, output in a line a single integer the highest point that he can earn by rescuing unconscious people within the given time.

Sample Input

2 

3 3 5 3 55 

XXXXX 

X..UX 

XSXXX 

XXXXX 

XU.DX 

XXXXX 

XXXXX 

XD..X 

XXXXX 

1 2 3 10 

3 2 3 50 

3 2 4 60 

2 2 6 4 27 

...... 

S..U.. 

...... 

...D.. 

1 2 3 20 

1 2 5 50 

1 2 6 50 

2 1 1 90

Sample Output

110 

100

 

#include<stdio.h>  

#include<string.h>  

#include<queue>  

using namespace std;  

  

struct node  

{  

    int x,y,z,t;  

};  

const int d[4][3]= {{0,1,0},{0,0,1},{0,-1,0},{0,0,-1}};  

char ss[11][101][101];  

int pp[11][101][101];

int h,w,L;  

int S,V[101],p[101],f[10001];  

bool zg[11][101][101];  

char sss[102];

node R[100005];

int ggg,gg;

bool check(int z,int x,int y)  

{  

    if(x>-1&&y>-1&&z>-1&&z<h&&x<w&&y<L)  

        return 1;  

    return 0;  

}  

void bfs (int xx,int yy,int zz,int tt)

{    

    int i;

    int x3,y3,z3;



    if(ss[zz][xx][yy]=='P')

    {

        p[pp[zz][xx][yy]]=tt*3;

    }

    if (ss[zz][xx][yy]=='U'&&zg[zz+1][xx][yy]==0)

    {

            zg[zz+1][xx][yy]=1;

            R[ggg].x=xx;

            R[ggg].y=yy;

            R[ggg].z=zz+1;

            R[ggg++].t=tt+1;

    }

    if (ss[zz][xx][yy]=='D'&&zg[zz-1][xx][yy]==0)

    {

            zg[zz-1][xx][yy]=1;

            R[ggg].x=xx;

            R[ggg].y=yy;

            R[ggg].z=zz-1;

            R[ggg++].t=tt+1;

    }

    for (i=0;i<4;i++)

    {

        x3=xx+d[i][1];

        y3=yy+d[i][2];

        z3=zz;

        if (check(z3,x3,y3)&&ss[z3][x3][y3]!='X'&&zg[z3][x3][y3]==0)

        {

            zg[z3][x3][y3]=1;

            R[ggg].x=x3;

            R[ggg].y=y3;

            R[ggg].z=z3;

            R[ggg++].t=tt+1;

        }

    }

    ++gg;

    if (gg!=ggg)    bfs(R[gg].x,R[gg].y,R[gg].z,R[gg].t);

}



int main()  

{

    int T,N,x,y,z,i,j,t;  

    int ex,ey;  

    scanf("%d",&T);  

    while(T--)  

    {  

        ggg=0;gg=-1;

        scanf("%d%d%d%d%d",&h,&w,&L,&N,&S);   

        for(i=0;i<h;i++)  

        {  

            for(j=0;j<w;j++)  

            {  

                 scanf("%s",sss);  

                for(t=0;t<L;t++)  

                {  

                    zg[i][j][t]=0;

                       ss[i][j][t]=sss[t];

                    if(sss[t]=='S')  

                    {

                    ex=j,ey=t; 

                    }  

                }    

            }  

        }  



        for(int i=0; i<N; i++)  

        {  

            scanf("%d%d%d%d",&z,&x,&y,&V[i]);  

            ss[z-1][x-1][y-1]='P';  

            pp[z-1][x-1][y-1]=i;

            p[i]=S+3;

        }

        zg[0][ex][ey]=1;

        bfs(ex,ey,0,0);  

        //01背包 

        memset(f,0,sizeof(f));  

        for(i=0;i<N;i++)  

        {

            for(j=S;j>=p[i];j--)  

            {  

                f[j]=max(f[j],f[j-p[i]]+V[i]);  

            }

        }

        printf("%d\n",f[S]);

    }

    return 0;  

}

 

你可能感兴趣的:(live)