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

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

题意:

  给你一个多层迷宫,每层迷宫有向上或者向下的楼梯,有些人被困在迷宫里面,每救一个人获得一定分数,不带人每走一格花费时间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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值