HDU 3533 Escape bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=3533
一道普通的bfs,但是由于代码实现出了bug还是拖了很久甚至对拍了
需要注意的是:
1.人不能经过炮台
2.能量耗尽时如果进入终点且终点没有炮弹也可以
3.炮台会遮挡住别的炮弹
前两点认为读题时不能确定
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int maxn=102,maxm=102,maxk=102;
const int inf=0x3fffffff;
const char badmess[20]={"Bad luck!"};
const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1}; struct castle{
int d,t,v,x,y;
castle(){d=t=v=x=y=-1;}
castle(int _d,int _t,int _v,int _x,int _y){_d=d;_t=t;_v=v;_x=x;_y=y;}
}cas[maxk];
vector<int>row[maxn],col[maxm]; struct man{
int x,y,t;
man(){x=y=t=-1;}
man(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
}; int charind[255];
char buff[3];
bool vis[maxn][maxm][maxk];
int m,n,k,d; queue<man>que; bool in(int x,int y){return x>=0&&x<=n&&y>=0&&y<=m;}
bool ok(int x,int y,int t,int i){
castle c=cas[i];
if(c.v==0&&(c.x!=x||c.y!=y))return true;
if(dx[c.d]!=0&&abs(c.x-x)%c.v==0){
int tt=abs(c.x-x)/c.v;
if(tt&&t>=tt&&(t-tt)%c.t==0)return false;//存在炮弹恰巧在该时间经过该点
if(tt==0&&t%c.t==0)return false;//如果本来就在炮台上
}
if(dy[c.d]!=0&&abs(c.y-y)%c.v==0){
int tt=abs(c.y-y)/c.v;
if(tt&&t>=tt&&(t-tt)%c.t==0)return false;
if(tt==0&&t%c.t==0)return false;
}
return true;
}
bool judge(int x,int y,int t){
int sind=-1,nind=-1,wind=-1,eind=-1;
for(int i=0;i<(int)col[y].size();i++){//找到四个方向上最近的,这些不会遮挡其他
int c=col[y][i];
if(cas[c].x>=x){
if(nind==-1||cas[nind].x>cas[c].x){
nind=c;
}
}
if(cas[c].x<=x){
if(sind==-1||cas[sind].x<cas[c].x){
sind=c;
}
}
}
for(int i=0;i<(int)row[x].size();i++){
int r=row[x][i];
if(cas[r].y>=y){
if(wind==-1||cas[wind].y>cas[r].y){
wind=r;
}
}
if(cas[r].y<=y){
if(eind==-1||cas[eind].y<cas[r].y){
eind=r;
}
} }
if(nind!=-1&&nind==sind)return false;//不能经过炮台
if(wind!=-1&&wind==eind)return false;
if(nind!=-1&&cas[nind].d==0)if(!ok(x,y,t,nind))return false;
if(sind!=-1&&cas[sind].d==1)if(!ok(x,y,t,sind))return false;
if(wind!=-1&&cas[wind].d==2)if(!ok(x,y,t,wind))return false;
if(eind!=-1&&cas[eind].d==3)if(!ok(x,y,t,eind))return false;
return true;
} int bfs(){
while(!que.empty())que.pop();
vis[0][0][0]=true;
que.push(man(0,0,0));
while(!que.empty()){
int nx=que.front().x,ny=que.front().y,nt=que.front().t;que.pop();
//printf("(%d %d) %d\n",nx,ny,nt);
if(nx==n&&ny==m)return nt;
if(nt==d)continue;
if(vis[nx][ny][nt+1]||!judge(nx,ny,nt+1)){}//原地停留
else {
vis[nx][ny][nt+1]=true;
que.push(man(nx,ny,nt+1));
}
for(int i=0;i<4;i++){
int tx=nx+dx[i],ty=ny+dy[i];
if(!in(tx,ty)||vis[tx][ty][nt+1])continue;
if(judge(tx,ty,nt+1)){//移动
vis[tx][ty][nt+1]=true;
que.push(man(tx,ty,nt+1));
}
}
}
return -1;
}
int main()
{
charind['N']=0;charind['S']=1;charind['W']=2;charind['E']=3; while(scanf("%d%d%d%d",&n,&m,&k,&d)==4){
memset(vis,false,sizeof(vis));
for(int i=0;i<maxn;i++){row[i].clear();}
for(int i=0;i<maxm;i++){col[i].clear();} for(int i=0;i<k;i++){
scanf("%s",buff);
cas[i].d=charind[(int)buff[0]];
scanf("%d%d%d%d",&cas[i].t,&cas[i].v,&cas[i].x,&cas[i].y);
col[cas[i].y].push_back(i);
row[cas[i].x].push_back(i);
}
int ans=bfs();
if(ans==-1)puts(badmess);
else printf("%d\n",ans);
} return 0;
}
HDU 3533 Escape bfs 难度:1的更多相关文章
- HDU 3533 Escape (BFS + 预处理)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 3533 Escape(bfs)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 3533 Escape BFS搜索
题意:懒得说了 分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的 #include <iostream> #include <cstdio> ...
- 【搜索】 HDU 3533 Escape BFS 预处理
要从0,0 点 跑到m,n点 路上会有k个堡垒发射子弹.有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒 能够上下左右或者站着不动 每步都须要消耗能量 一共同拥有en ...
- HDU 3533 Escape(大逃亡)
HDU 3533 Escape(大逃亡) /K (Java/Others) Problem Description - 题目描述 The students of the HEU are maneu ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- HDU 1495 非常可乐 bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=1495 第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现 #includ ...
- HDU3533 Escape —— BFS / A*算法 + 预处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others) ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
随机推荐
- jsonp实现原理详细介绍
主要是浏览器的同源同域(协议相同,域名相同及端口相同)策略需要使用跨域获取数据,故需要jsonp跨域获取数据.重点:img的src,link的href及script的src不遵循浏览器的同源同域策略, ...
- poj2002Squares(点集组成正方形数)
链接 可以枚举两个点,因为是正方形两外两点可以由已知求出,据说可以根据三角形全等求出下列式子,数学渣不会证... 已知: (x1,y1) (x2,y2) 则: x3=x1+(y1-y2) y ...
- Python学习(4)运算符
目录 Python 算术运算符 Python 比较运算符 Python 赋值运算符 Python 位运算符 Python 逻辑运算符 Python 成员运算符 Python 身份运算符 Python ...
- Forbidden You don't have permission to access / on this server. You don't have permission to access /phpmyadmin/ on this server. 解决办法
Forbidden You don't have permission to access / on this server. 解决办法 打开 httpd.conf 文件, 将 # onli ...
- hibernate.properties与hibernate.cfg.xml 区别
Hibernate的数据库连接信息是从配置文件中加载的. Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件. 一)hibernate.cfg.xml ...
- a、b交换与比较
1.有两个变量a,b,不用if.?: .switch 或其他判断语句,找出两个数中 较大的: int max = ((a+b)+abs(a-b))/2 较小的: int min = ((a+b)-ab ...
- 【转】 C++中的new VS C语言中的malloc
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 前几天一个朋友去面试百度空间的一个职位,被问及这个问题,我听后说了几点,不过感觉还是不透彻,所以上网查阅了一些资 ...
- 由函数clock想到的
今天介绍一下clock这个函数的使用,它是C标准库的一部分,声明在头文件<time.h>中,返回处理器使用的时间值,函数声明为: clock_t clock(void); 这个函数看起来很 ...
- C#中的ManagementClass类
C# 提供了ManagementClass类来获取本机的一些基本信息,比如CPU的个数,CPU的频率,网卡的MAC,内存的大小,硬盘的大小等. 获取本机MAC地址: /// <summary&g ...
- Qt之QRoundProgressBar(圆形进度条)
简述 QRoundProgressBar类能够实现一个圆形进度条,继承自QWidget,并且有和QProgressBar类似的API接口. 简述 详细说明 风格 颜色 字体 共有函数 共有槽函数 详细 ...