Fire Drill [UvaLive 5064] BFS+0/1背包

来源:互联网 发布:西湖大学 知乎 编辑:程序博客网 时间:2024/06/03 17:32

  <=写成<<检查了好久= =!

题意:

  给你一个多层迷宫,每层迷宫有向上或者向下的楼梯,有些人被困在迷宫里面,每救一个人获得一定分数,不带人每走一格花费时间1,带人花费时间2,问在s秒内最多获得多少分数.

题解:

  关键是人跟人之间没影响,所以先BFS预处理出跟每个人的最短距离,然后距离*3即为就这个人的花费时间,做一遍0/1背包即可.

 

代码:

/*  * File:   main.cpp * Author: swordholy * * Created on 2011年4月5日, 下午1:17 */#include <cstdlib>#include <iostream>#include <string>#include <queue>#include <memory.h>using namespace std;int Map[20][150][150];int f[100005];string ss;int  visit[20][150][150];struct pt{    int l,h,w;    int step;};struct people{    pt where;    int point;    int time_use;};people pl[205];queue<pt> q;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int main(int argc, char** argv){    int i,j,t,n,s,l,h,w;    int tcase,u;    pt start;    cin>>tcase;    for(u=1;u<=tcase;u++)    {       cin>>l>>h>>w>>n>>s;       memset(Map,0,sizeof(Map));              for(i=1;i<=l;i++)       {           for(j=1;j<=h;j++)           {               cin>>ss;               for(int k=0;k<ss.length();k++)               {                   if (ss[k]=='X')                   {                      // cout<<"ddd"<<endl;                    Map[i][j][k+1]=-1;                  //  cout<<Map[i][j][k+1]<<endl;                   }                   if (ss[k]=='S')                   {                       start.l=i;start.h=j;start.w=k+1;                   }                   if (ss[k]=='U')                   {                       Map[i][j][k+1]=1;                   }                   if (ss[k]=='D')                       Map[i][j][k+1]=2;               }           }       }       for(i=1;i<=n;i++)       {           cin>>pl[i].where.l>>pl[i].where.h>>pl[i].where.w>>pl[i].point;       }       memset(visit,-1,sizeof(visit));       while(!q.empty()) q.pop();       start.step=0;       visit[1][start.h][start.w]=0;       q.push(start);       while(!q.empty())       {           pt now=q.front();           q.pop();           for(i=0;i<4;i++)           {               int hh,ww;               hh=now.h+dir[i][0];               ww=now.w+dir[i][1];               if (hh>=1&&hh<=h&&ww>=1&&ww<=w&&Map[now.l][hh][ww]!=-1&&visit[now.l][hh][ww]==-1)               {                   pt next;                   next.l=now.l;                   next.h=hh;                   next.w=ww;                   next.step=now.step+1;                   visit[next.l][next.h][next.w]=next.step;                   q.push(next);               }           }           int hh,ww;               if (Map[now.l][now.h][now.w]==1)               {                   hh=now.h;ww=now.w;                 if (hh>=1&&hh<=h&&ww>=1&&ww<=w&&Map[now.l+1][hh][ww]!=-1&&visit[now.l+1][hh][ww]==-1)               {                   pt next;                   next.l=now.l+1;                   next.h=hh;                   next.w=ww;                   next.step=now.step+1;                   visit[next.l][next.h][next.w]=next.step;                   q.push(next);               }               }                if (Map[now.l][now.h][now.w]==2)               {                   hh=now.h;ww=now.w;                 if (hh>=1&&hh<=h&&ww>=1&&ww<=w&&Map[now.l-1][hh][ww]!=-1&&visit[now.l-1][hh][ww]==-1)               {                   pt next;                   next.l=now.l-1;                   next.h=hh;                   next.w=ww;                   next.step=now.step+1;                   visit[next.l][next.h][next.w]=next.step;                   q.push(next);               }               }                  }       for(i=1;i<=n;i++)       {           if (visit[pl[i].where.l][pl[i].where.h][pl[i].where.w]==-1)               visit[pl[i].where.l][pl[i].where.h][pl[i].where.w]=20000;           pl[i].time_use=visit[pl[i].where.l][pl[i].where.h][pl[i].where.w]*3;       }       memset(f,0,sizeof(f));       int ans=0;       for(j=1;j<=n;j++)        for(i=s;i>=pl[j].time_use;i--)       {            f[i]=max(f[i],f[i-pl[j].time_use]+pl[j].point);            if (ans<f[i]) ans=f[i];       }       //for(i=0;i<=55;i++)          // cout<<i<<":"<<f[i]<<endl;       cout<<ans<<endl;    }    return 0;}

原创粉丝点击