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的更多相关文章

  1. HDU 3533 Escape (BFS + 预处理)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 3533 Escape BFS搜索

    题意:懒得说了 分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的 #include <iostream> #include <cstdio> ...

  4. 【搜索】 HDU 3533 Escape BFS 预处理

    要从0,0 点 跑到m,n点  路上会有k个堡垒发射子弹.有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒 能够上下左右或者站着不动 每步都须要消耗能量  一共同拥有en ...

  5. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  6. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  7. HDU 1495 非常可乐 bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现 #includ ...

  8. HDU3533 Escape —— BFS / A*算法 + 预处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others)  ...

  9. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

随机推荐

  1. 控制反转和spring在项目中可以带来的好处

    Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...

  2. Linux之free命令

    from http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html free命令可以显示Linux系统中空闲的.已用的物理内存及swap ...

  3. (八)shell中的循环结构

    1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...

  4. 【Todo】【读书笔记】机器学习实战(Python版)

    还是把这本书的读书笔记,单独拎出来吧,因为内容比较多. P38. Logistic 回归. 觉得还蛮实用的.囫囵吞枣看的.要细看.

  5. laravel 开启sql查询日志

    \DB::enableQueryLog(); dd(\DB::getQueryLog());

  6. [转]Oracle中INITRANS和MAXTRANS参数

    每个块都有一个块首部.这个块首部中有一个事务表.事务表中会建立一些条目来描述哪些事务将块上的哪些行/元素锁定.这个事务表的初始大小由对象的INITRANS 设置指定.对于表,这个值默认为2(索引的IN ...

  7. Hbase之Exception

    [hadoop@master hbase-1.2.2]$ ./bin/hbase shell2016-08-25 13:53:56,898 WARN [main] util.NativeCodeLoa ...

  8. 晒幸福, qq空间晒法

    qq空间晒法 1.成为老婆之后,还是说新交的女朋友,这会让女朋友感动

  9. Android 开源简单控件

    Android开源系列分类 查看 CircleImageView 自定义圆形控件的使用 添加依赖 ‘de.hdodenhof:circleimageview:2.1.0' 作用:无论你设置的图片是什么 ...

  10. C/C++深度copy和浅copy

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...