您的位置:首页 > 其它

UVALive 5066 - Fire Drill (BFS+01背包)

2013-07-17 15:43 381 查看

5066 - Fire Drill

Time limit: 3.000 seconds

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(T

100)
denoting the number of case. Each case has five integersL
(1

L

10),H
(1

H

100),W
(1

W

100),N
(1

N

100)
and S (1

S

10,
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 theL-th floor respectively. Each floor consists ofH 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 (1

fi

L),ri
(1

ri

H),ci
(1

ci

W),pi
(1

pi

1,
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


题意:有l层楼,每层楼h*w这么大,按楼层顺序给出楼的平面图,然后给出你要救的人的位置,你每一次只能救一个人,去的时候一个单位时间一格,带着人2个单位时间一格(上下楼梯算一格),救完人必须回到入口再继续,每个人都有一定的价值,问在规定时间可以得到的最大价值。

题解:先用bfs求出救每个人所需的最短时间,然后用01背包计算出所给时间可以救人的最大价值(代码AC说明数据中的人不在楼梯上的)

#include<stdio.h>
#include<string.h>
int m[12][102][102],mark[12][102][102];
int dp[10005],val[105],wei[105];
int judge(char c)
{
return c!='.'&&c!='S'&&c!='X'&&c!='U'&&c!='D';
}
struct QUEUE
{
int a,b,c,t;
}que[1000005],temp;
int MAX(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k,a,b,c,d,l,h,w,n,s;
int t,sx,sy,sta,fin;
char ch;

scanf("%d",&t);
while(t--)
{
memset(m,0,sizeof(m));
memset(mark,0,sizeof(mark));
memset(dp,0,sizeof(dp));
scanf("%d%d%d%d%d",&l,&h,&w,&n,&s);
for(i=1;i<=l;i++)
{
for(j=1;j<=h;j++)
for(k=1;k<=w;k++)
{
while(scanf("%c",&ch),judge(ch));
if(ch=='.') m[i][j][k]=-1;
else if(ch=='U') m[i][j][k]=-2;
else if(ch=='D') m[i][j][k]=-3;
else if(ch=='S') sx=j,sy=k;
}
}
while(n--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
m[a][b][c]=d;
}
n=0;
que[0].a=1,que[0].t=0;
que[0].b=sx,que[0].c=sy;
sta=0,fin=1;
while(sta<fin)
{
temp=que[sta++];
i=temp.a,j=temp.b,k=temp.c;
if(m[i][j][k-1]&&!mark[i][j][k-1])
{
mark[i][j][k-1]=1;
que[fin].a=i,que[fin].b=j;
que[fin].c=k-1,que[fin++].t=temp.t+1;
}
if(m[i][j][k+1]&&!mark[i][j][k+1])
{
mark[i][j][k+1]=1;
que[fin].a=i,que[fin].b=j;
que[fin].c=k+1,que[fin++].t=temp.t+1;
}
if(m[i][j-1][k]&&!mark[i][j-1][k])
{
mark[i][j-1][k]=1;
que[fin].a=i,que[fin].b=j-1;
que[fin].c=k,que[fin++].t=temp.t+1;
}
if(m[i][j+1][k]&&!mark[i][j+1][k])
{
mark[i][j+1][k]=1;
que[fin].a=i,que[fin].b=j+1;
que[fin].c=k,que[fin++].t=temp.t+1;
}
if(m[i][j][k]==-2&&!mark[i+1][j][k])
{
mark[i+1][j][k]=1;
que[fin].a=i+1,que[fin].b=j;
que[fin].c=k,que[fin++].t=temp.t+1;
}
if(m[i][j][k]==-3&&!mark[i-1][j][k])
{
mark[i-1][j][k]=1;
que[fin].a=i-1,que[fin].b=j;
que[fin].c=k,que[fin++].t=temp.t+1;
}
if(m[i][j][k]>0)
{
val
=m[i][j][k];
wei[n++]=temp.t*3;  //来回时间
}
}
for(i=0;i<n;i++)
for(j=s;j>=wei[i];j--)
dp[j]=MAX(dp[j],dp[j-wei[i]]+val[i]);
printf("%d\n",dp[s]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: