Test instructions: There is a three-dimensional map, there are n people trapped, now firefighters can only access from a 1 floor entrance, rescue trapped, each trapped person has a value, when firemen find a trapped person, he can rescue or let go, if saved, he must immediately back to the entrance, not stop, Do not save more people at the same time, and go back to a step to do two steps, that is, time increases by one times. The sum of the maximum value of the person who can be saved within a given time.
Solution: BFS Record each point from the starting point of the shortest distance, then save this person's cost is 3*dis, and then already know to save this person's value, then finally ask for a 01 backpack can.
Note that a person can not save the place, I began to initialize the dis to 1000000007, so if not to go, the cost will become 3*dis burst Int. Tragedy.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<queue>#defineMod 10000007using namespacestd;#defineN 50107Charmp[ the][ the][ the];intdis[ the][ the][ the];intdp[10006];intt[ the],p[ the],l,h,w;intdx[7] = {0,0,0,0,1, -1};intdy[7] = {0,1,-1,0,0,0,};intdz[7] = {1,0,0,-1,0,0,};structNode {intl,h,w,sec; Node (int_x,int_y,int_z,int_sec): L (_x), H (_y), W (_z), sec (_sec) {} node () {}}vol[135],s;BOOLOK (intXintYintz) {if(x >=0&& x < L && y >=0&& y < H && z >=0&& Z <W)return true; return false;}voidBFS (node S) {Queue<node>Q; while(!q.empty ()) Q.pop (); inti,j,k; for(i=0; i<l;i++) for(j=0; j) for(k=0; k<w;k++) Dis[i][j][k]=Mod; intx = s.l, y = S.h, z =S.W; DIS[X][Y][Z]=0; Q.push (S); while(!Q.empty ()) {Node now=Q.front (); Q.pop (); intL = now.l, H = now.h, W = now.w, sec =now.sec; if(Mp[l][h][w] = ='U') { for(k=0;k<5; k++) { intNX = L +Dx[k]; intNY = h +Dy[k]; intNZ = w +Dz[k]; if(! OK (NX,NY,NZ))Continue; if(MP[NX][NY][NZ]! ='X') { if(sec+1<Dis[nx][ny][nz]) {DIS[NX][NY][NZ]= sec+1; Node tmp= Node (nx,ny,nz,sec+1); Q.push (TMP); } } } } Else if(Mp[l][h][w] = ='D') { for(k=0;k<6; k++) { if(k = =4)Continue; intNX = L +Dx[k]; intNY = h +Dy[k]; intNZ = w +Dz[k]; if(! OK (NX,NY,NZ))Continue; if(MP[NX][NY][NZ]! ='X') { if(sec+1<Dis[nx][ny][nz]) {DIS[NX][NY][NZ]= sec+1; Node tmp= Node (nx,ny,nz,sec+1); Q.push (TMP); } } } } Else { for(k=0;k<4; k++) { intNX = L +Dx[k]; intNY = h +Dy[k]; intNZ = w +Dz[k]; if(! OK (NX,NY,NZ))Continue; if(MP[NX][NY][NZ]! ='X') { if(sec+1<Dis[nx][ny][nz]) {DIS[NX][NY][NZ]= sec+1; Node tmp= Node (nx,ny,nz,sec+1); Q.push (TMP); } } } } }}intMain () {intt,n,stime,i,j,k; scanf ("%d",&t); while(t--) {scanf ("%d%d%d%d%d",&l,&h,&w,&n,&stime); for(i=0; i<l;i++) { for(j=0; j) {scanf ("%s", Mp[i][j]); for(k=0; k<w;k++) if(Mp[i][j][k] = ='S') S= Node (i,j,k,0); } } for(i=1; i<=n;i++) {scanf ("%d%d%d%d",&vol[i].l,&vol[i].h,&vol[i].w,&P[i]); VOL[I].L--, vol[i].h--, vol[i].w--; } BFS (S); for(i=1; i<=n;i++) T[i]=3*DIS[VOL[I].L][VOL[I].H][VOL[I].W]; Memset (DP,0,sizeof(DP)); for(i=1; i<=n;i++) { for(j=stime;j>=0; j--) { if(J >=T[i]) dp[j]= Max (dp[j],dp[j-t[i]]+P[i]); } } intMaxi =0; for(i=0; i<=stime;i++) Maxi=Max (maxi,dp[i]); printf ("%d\n", Maxi); } return 0;}View Code
Uvalive 5066 Fire Drill--BFS+DP