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 ...
随机推荐
- js问的我醉的不要不要的。
function a(b){ console.log(b); function b(){ console.log(b); } b();} a(1); 两个console.log会输出什么?竟然一个1都 ...
- css处理浏览器兼容问题
浏览器的兼容: _ ie6认识 格式:_width:100px;要写在最后面来覆盖前面的. * ie6和ie7都认识 格式:* width:100px;要写在最后面来覆盖前面的. \0 ie8认识 ...
- 算法_栈与队列的Java链表实现
链表是一个递归的数据结构,它或者为null,或者是指向一个结点的引用,该结点含有一个泛型的元素和指向另一个链表的引用.可以用一个内部类来定义节点的抽象数据类型: private class Node ...
- epoll中et+多线程模式中很重要的EPOLL_ONESHOT实验
因为et模式需要循环读取,但是在读取过程中,如果有新的事件到达,很可能触发了其他线程来处理这个socket,那就乱了. EPOLL_ONESHOT就是用来避免这种情况.注意在一个线程处理完一个sock ...
- Android_程序结构分析
一.Android程序运行过程 二.Android项目结构
- matlab 自动阈值白平衡算法 程序可编译实现
一种效果很好的自动白平衡技术(WhiteBalance) 白平衡是图像处理的一个极重要概念.所谓白平衡(英文名称为White Balance),就是对白色物体的还原.当我们用肉眼观看这大千世界时,在不 ...
- mac 下基于firebreath 开发多浏览器支持的浏览器插件
mac 下基于firebreath 开发多浏览器支持的浏览器插件 首先要区分什么是浏览器扩展和浏览器插件;插件可以像本地程序一样做的更多 一. 关于 firebreath http://www.fir ...
- VR应用里面的Photogrammetry技术是什么
http://www.manew.com/thread-49556-1-1.html 具体使用 http://www.didayin.com/archives/632 软件下载 http://labs ...
- 使用TCP/IP的套接字(Socket)进行通信
http://www.cnblogs.com/mengdd/archive/2013/03/10/2952616.html 使用TCP/IP的套接字(Socket)进行通信 套接字Socket的引入 ...
- Cache模拟器(CacheSim)
最近写了一个Cache的模拟器,由于平时空余时间比较分散,前前后后用了一周多的时间,基本实现的Cache的模拟功能(通过读取trace文件得到相应的命中率),能够实现直接映射.全相联.组相联三种 ...