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;}
- Fire Drill [UvaLive 5064] BFS+0/1背包
- UVALive 5066 - Fire Drill (BFS+01背包)
- UVALive 5066 Fire Drill
- UVALive 5006 (BFS+heap+01背包)
- soj 3960: The Bovine Fire Drill
- UVALive - 4356 Fire-Control System
- UVA 11624 BFS Fire
- UVa Fire! 11624 (BFS)
- uva 11624 - Fire! bfs
- UVa 11624 Fire! / BFS
- UVa 11624 Fire! bfs
- uva 11624 - Fire!(BFS)
- Fire Game(BFS)
- Fire Game(BFS)
- UVA11624-Fire!(两次bfs)
- UVA - 11624 Fire! BFS
- uva 11624Fire! (bfs)
- 【UVA】 11624 Fire!【BFS】
- makefile写法
- __builtin_offsetof(a,b)的用法
- JavaScript可否多线程? 深入理解JavaScript定时机制
- android opengl es--三角形,四边形
- Android学习笔记(二三): 多页显示-Flipper的使用
- Fire Drill [UvaLive 5064] BFS+0/1背包
- 不能不会用的命令——netstat | arp欺骗,攻击
- Winform Tabcontrol控件隐藏tabpage头部的方法
- Android Gesture Detector
- 全文索引的使用方法(一)
- Chrome源码剖析、上
- 全文索引的使用(二)
- iPad 2全球售价有别:美国最低丹麦最贵
- ipconfig命令详解