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 ... 
随机推荐
- iOS常用define宏定义
			1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ... 
- mysql 主主复制的配置流程
			1.先关闭B,把A的数据导出来,mysqldump -hlocalhost -uroot -p123456 --database ibprpu >ibprpu.sql2.关闭A,启动B,进入my ... 
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
			1. HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ... 
- hostapd and wpa_supplicant
			一.Hostapd hostapd 是一个用户态用于AP和认证服务器的守护进程.它实现了IEEE 802.11相关的接入管理,IEEE 802.1X/WPA/WPA2/EAP 认证, RADIUS客户 ... 
- Android设置Activity启动和退出时的动画
			业务开发时遇到的一个小特技,要求实现Activity启动时自下向上弹出,退出时自上向下退出. 此处不关注启动和退出时其他Activity的动画效果,实现方法有两种: 1.代码方式,通过Activity ... 
- 【linux 命令】:查看系统开机,关机时间【转载】
			转载原文:http://www.cnblogs.com/kerrycode/p/3759395.html 看Linux开机关机时间的方法(非常全面) 1: who 命令查看 who -b 查看最后一次 ... 
- 多线程调用HttpWebRequest并发连接限制
			.net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows 7 下默认是2,在服务器操作 ... 
- iOS开发 设置状态栏样式
			[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO]; 
- 优之星S900平板电脑root方法
			优之星S900root方法: 尝试百度一键root安卓的apk版本root失败. 电脑上下载百度一键root的Windows版本,平板设置→开发者选项→允许USB调试√,保持唤醒状态√,安全设置→允许 ... 
- java 导入包
			导入包 问题:类名冲突时,要如何解决. 解决:sun提供导入包语句让我们解决该问题. 导入包语句的作用:简化书写. 导入包语句的格式:import 包名.类名;(导入xxx包的XX类) 导入包语句的细 ... 
