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. [转载] nosql 数据库的分布式算法

    原文: http://juliashine.com/distributed-algorithms-in-nosql-databases/ NoSQL数据库的分布式算法 On 2012年11月9日 in ...

  2. .net ftp上传文件方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. ps aux和ps ef的区别

    ps aux 是以BSD方式显示ps -ef 是以System V方式显示,该种方式比BSD方式显示的多一重要项--(具体哪项忘了 -_- ) ps aux的输出: USER PID %CPU %ME ...

  4. easyui 进度条

    进度条创建 $.messager.progress({ title:'请稍后', msg:'正在努力...' }); 进度条关闭 $.messager.progress('close'); 弹窗对话框 ...

  5. icp算法的一些参考资料

    1.综述:迭代最近点算法综述,介绍了svd分解和四元数法,其中 svd法:http://blog.csdn.net/kfqcome/article/details/9358853 四元数法:http: ...

  6. KD-tree

    就是K维空间上的二叉查找树. 每个node对应k维空间的超矩形区域 在方差最大的维度上,比较数据与kd-tree的根节点.中间节点,在该维度上的中值处划分,得到新的子空间,直到不能再分. 用于最近邻查 ...

  7. eclipse 中发布 maven 项目到 tomcat

    第一步:在 eclipse 中 右键  你的项目 Run as --> Maven build...--> Goals: 输入 install   run 第二步:在 eclipse 中 ...

  8. js简单模仿队列

    window.meng = window.meng || {}; (function () { var items = []; meng.queue = { /** * * @param {Funct ...

  9. java中的异常和处理

    算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeAr ...

  10. 补第二周四人小组WBS/NABCD

    四人小组项目<东北师范大学论坛> 要求: 1.给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 2.不熟悉的名词,自行搜索资料并参考教材第393页开始的术语索引 ...