BFS-hdu-3533-Escape
来源:互联网 发布:淘宝编辑宝贝教程视频 编辑:程序博客网 时间:2024/05/18 03:37
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3533
题目大意:
给一个m*n的矩阵方格,有k门大炮,每门大炮以t为周期向c方向发射炮弹,炮弹速度为v.
求从(0,0)到(m,n)位置最短的时间,中间不能被炮弹打到,时间不能超过d.
解题思路:
预处理好后就是简单的BFS。
先预处理每个点能够被炮弹射击的所有时刻,然后用vis[x][y][t]判重,表示到达(x,y)位置时刻为t时是否被访问。
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#define eps 1e-6#define INF 0x1f1f1f1f#define PI acos(-1.0)#define ll __int64#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;/*freopen("data.in","r",stdin);freopen("data.out","w",stdout);*/bool iscan[120][120][1100]; //预处理所有点,不能停留的时间bool cas[120][120]; //是否炮弹bool vis[120][120][1100]; //是否被访问int m,n,k,d,dir[5][2]={{-1,0},{0,1},{1,0},{0,-1},{0,0}}; //最后一个方向为呆在原地map<char,int>myp;char sa[5];bool isin(int x,int y) //判断是否在网格里{ if(x<0||x>m||y<0||y>n) return false; return true;}struct Node{ int x,y,t; friend bool operator <(const Node & a,const Node & b) { return a.t>b.t; }};struct Bome{ int x,y,t,v; char sa[5];}bom[110];void bfs(){ memset(vis,false,sizeof(vis)); Node s; s.x=0,s.y=0,s.t=0; priority_queue<Node>myq; //以时间为优先因素 myq.push(s); vis[0][0][0]=true; while(!myq.empty()) { Node cur=myq.top(); myq.pop(); for(int i=0;i<5;i++) { int x=cur.x+dir[i][0],y=cur.y+dir[i][1]; int t=cur.t+1; if(!isin(x,y)||cas[x][y]||vis[x][y][t]) continue; if(!iscan[x][y][t]||t>d) continue; if(x==m&&y==n) { printf("%d\n",t); return ; } vis[x][y][t]=true; Node tmp; tmp.x=x,tmp.y=y,tmp.t=t; myq.push(tmp); } } printf("Bad luck!\n");}int main(){ int x,y,t,v; myp['N']=0,myp['E']=1,myp['S']=2,myp['W']=3; while(scanf("%d%d%d%d",&m,&n,&k,&d)!=EOF) { memset(iscan,true,sizeof(iscan)); memset(cas,false,sizeof(cas)); for(int i=1;i<=k;i++) { scanf("%s%d%d%d%d",bom[i].sa,&bom[i].t,&bom[i].v,&bom[i].x,&bom[i].y); cas[bom[i].x][bom[i].y]=true; } for(int i=1;i<=k;i++) { x=bom[i].x,y=bom[i].y,v=bom[i].v,t=bom[i].t; strcpy(sa,bom[i].sa); int dd=myp[*sa],xx,yy,sumt=0; while(true) { sumt++; //枚举时间 xx=x+dir[dd][0]*v*sumt,yy=y+dir[dd][1]*v*sumt; //到达的位置 if(!isin(xx,yy)) //跑到了外面 break; bool flag=true; for(int j=0;j<v;j++) //这之间是否有大炮 { if(cas[xx-j*dir[dd][0]][yy-j*dir[dd][1]]) { flag=false; //如果有 则这门大炮到此无用 break; } } if(!flag) break; int tmpt=sumt; while(tmpt<=d) //大炮是周期性,所以每隔t时间,又有一门大炮射来 { iscan[xx][yy][tmpt]=false; tmpt+=t; } } } bfs(); } return 0;}
- BFS-hdu-3533-Escape
- HDU-3533-Escape(BFS)
- HDU 3533 Escape(BFS)
- 【HDU 3533】Escape(BFS)
- HDU 3533 Escape 预处理+bfs
- 【搜索】 HDU 3533 Escape BFS 预处理
- HDU - 3533----Escape(BFS+预处理)
- HDU 3533 Escape (bfs + 预处理 + 剪枝)
- hdu Escape(bfs+模拟)
- hdu-2364-Escape(bfs)
- hdu 2364 Escape【模拟优先队列】【bfs】
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- HDU 3533 Escape
- HDU 3533 Escape
- HDU-3533 Escape
- HDU 3533 Escape
- hdu 2337 Escape from Enemy Territory (二分枚举+预处理+bfs)
- HDU 2337 Escape from Enemy Territory(BFS+二分优化)
- perl typeglob使用
- 图像文件读入和显示
- LeetCode-Binary Tree Inorder Traversal
- 坐标系统概念
- HDU:3779 Railroad
- BFS-hdu-3533-Escape
- 转载_uboot传递内核参数全解析
- Linux多线程编程
- 利用windows脚本实现Oracle数据库定时导出文件
- Linux策略路由基础
- hdu2544 dijkstra最短路径问题
- HDU - 3775(几何+Pick定理+好题)
- 判断矩形和圆交
- MongoDB自学笔记7---3.2 MongoDB数据类型