Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4 0 0 2 2 1 2 1 1 2 0 3 5
Sample Output
5
题意:
有M颗流星坠落,(Xi,Yi)表示坠落的位置 ,Ti表示坠落的时间,坠落的同时,上下左右四个地方也同样会受到攻击。输出在最小时间,在这个时间内能走到安全的地方,如果不能逃走,则输出-1.
思路:
先初始化把所有的点都设为无穷大,然后通过输入M次坐标(Xi,Yi)来改变更新这个点及周围四个点的最小时间。最后从(0,0)点开始进行广搜,从上下左右位置搜((0,0)点只有上与右),如果找到这个点到(0,0)的距离小于这个格子的最短距离时间,则判断这个点是不是无穷大(即是不是安全地),如果不是则放入队列中以备下一次搜索该点的周围点时使用,如果这个点是无穷大,说明是安全地,这时候输出这个点到(0,0)点的时间即为最小时间。
AC:
#include<stdio.h> #include<string.h> #define BOR 300+5 //最大宽度 #define INT 10000000 //用来表示无穷大 typedef struct //模拟队列 { int x; //x坐标 int y; //y坐标 int s; //距离 }queen; int map[BOR][BOR]; //表示整个图 queen q[BOR*BOR]; int vis[BOR][BOR]; //记录有无走过该地方 int dis[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; //坐标位置的上下左右 int bfs(int a,int b) { queen from,to; int f=0,t=1,i; from.x=0;from.y=0;from.s=0; q[0]=from; memset(vis,0,sizeof(vis)); if(map[0][0]==INT) return 0; //如果本身地方通过一开始初始化后还是无穷数,说明该地方安全 if(map[0][0]==0) return -1; //如果该地方在0秒是就被轰炸,说明逃不了了 while(f!=t) { from=q[f++]; if(f==BOR) f=0; //也许走到最边边位置也逃不了 for(i=0;i<4;i++) //循环四次,表示走该位置的上下左右4个地方 { to.x=from.x+dis[i][0]; to.y=from.y+dis[i][1]; to.s=from.s+1; //记录深度,当换下一个深度的时候才+1,不是这个每次循环都+1 if(to.x>=0&&to.y>=0&&to.x<=BOR&&to.y<=BOR&&vis[to.x][to.y]==0&&map[to.x][to.y]>to.s) { //如果在范围内,且还没访问过,且这个距离小于初始化后此时的大小 if(map[to.x][to.y]==INT) return to.s; //如果这个地方是无穷大,则说明这个地方安全,返回这个时候的距离最短长度,即最短时间 vis[to.x][to.y]=1; //如果不满足上面条件,则标记这个点为已放访问点 q[t++]=to; //放入模拟队列中,下一次判断该点周围的点 if(t==BOR) t=0; //也许走到最边边位置也逃不了 } } } return -1; //如果循环上面的步骤的无法返回一个最短时间,则说明逃不了了,返回-1 } int main() { int N,i,j; int X,Y,T; scanf("%d",&N); for(i=0;i<BOR;i++) //每个区域位置都未无穷大,假设全都为安全 for(j=0;j<BOR;j++) map[i][j]=INT; for(i=0;i<N;i++) //周围点及中心点为所给坐标时间的最小值 { //对输入坐标点及坐标周围点初始化 scanf("%d%d%d",&X,&Y,&T); if(map[X][Y]>T) map[X][Y]=T; if(map[X+1][Y]>T) map[X+1][Y]=T; if(X>0&&map[X-1][Y]>T) map[X-1][Y]=T; if(Y>0&&map[X][Y-1]>T) map[X][Y-1]=T; if(map[X][Y+1]>T) map[X][Y+1]=T; } printf("%d\n",bfs(0,0)); //输出最短时间 return 0; }
STL的queue写的AC代码:
#include<cstdio> #include<queue> #include<string.h> #define BOR 300+5 #define INT 1000000 using namespace std; struct pos { int x; int y; int s; }; int map[BOR][BOR]; int vis[BOR][BOR]; int dis[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; queue<pos> q; int bfs(int a,int b) { int t; pos from,to; memset(vis,0,sizeof(vis)); from.x=0,from.y=0,from.s=0; q.push(from); if(map[0][0]==INT) return 0; if(map[0][0]==0) return -1; while(!q.empty()) { from=q.front(); for(t=0;t<4;t++) { to.x=from.x+dis[t][0]; to.y=from.y+dis[t][1]; to.s=from.s+1; if(to.x>=0&to.y>=0&&to.x<=BOR&&to.y<=BOR&&map[to.x][to.y]>to.s&&vis[to.x][to.y]==0) { if(map[to.x][to.y]==INT) return to.s; vis[to.x][to.y]=1; q.push(to); } } q.pop(); } return -1; } int main() { int N,i,j; int x,y,time; scanf("%d",&N); for(i=0;i<BOR;i++) for(j=0;j<BOR;j++) map[i][j]=INT; for(i=0;i<N;i++) { scanf("%d%d%d",&x,&y,&time); if(map[x][y]>time) map[x][y]=time; if(map[x+1][y]>time) map[x+1][y]=time; if(map[x][y+1]>time) map[x][y+1]=time; if(x>0&&map[x-1][y]>time) map[x-1][y]=time; if(y>0&&map[x][y-1]>time) map[x][y-1]=time; } printf("%d\n",bfs(0,0)); }
总结:
对广搜也有了进一步的了解认识了,一边光想怎么做还是不行,还是需要找资料找下模板看看如何实现广搜才行。很多东西也许看是看懂了,但是一动手写就会出现一堆错误,第一次做出来还是很不踏实,所以一下子Delete所有代码重新写一遍。果然,写了半小时多才写了出来,但是写出来还是一堆错误,而且我不是用STL的queue写的,所以还需要再写一遍来加深加深印象。(第二次修改已经把queue的版本写出来了)
相关推荐
A jQuery meteor shower animation special effect download, the JS code is a simple jQuery meteor animation,
NULL 博文链接:https://8214qiu.iteye.com/blog/1843523
在上一个开发的版本上,修复了无法剔除连续两跳为相同IP的错误,后续也会更加听取用户们的体验,谢谢使用
自动为您在GitHub上访问的页面加注星标。 你还记得迄今为止你看到的所有GitHub仓库吗?我想过使用我以前看过的仓库,但是当时忘记只装上一颗星星。不是这样的经历吗? 这个扩展解决了所有这些问题。...
Meteor1.08Meteor1.08Meteor1.08
meteor-ios, Meteor iOS将本机iOS应用程序与 Meteor 平台集成( http Meteor iOSMeteor iOS通过DDP将本机iOS应用与 Meteor 平台( http://www.meteor.com ) 集成。 它为延迟补偿提供全面支持,并支持核心数据编程模型...
meteor-electron, Meteor 电子,创建桌面 Meteor 应用程序最简单的方法 电子meteor电子让你轻松地将 Meteor webapp转换为桌面应用。 它的最终目标是构建 meteor add-platform desktop 。它所做的一些事情:自动生成...
meteor-whatsapp, Meteor 博客的代码,带有 Meteor 和的Whatsapp克隆 Meteor步骤 1---创建项目安装 Meteor $ curl https://install.meteor.com/| sh创建 Meteor 项目 $ meteor create whatsapp添加 Angular 和
meteor-up-legacy, 生产质量 Meteor 部署 Meteor这里版本不再维护。Mupx 是稳定版本。新开发移至以下位置: https://github.com/kadirahq/meteor-up 。产品质量 Meteor 部署Meteor 是一个支持你将任何 Meteor
meteor-spin, 用于 Meteor的简单 Spinner 包 **NOTE: 现在是 Meteor 支持的,没有好的理由使用这个软件包。 仅使用原始的NPM软件包。 **通过 Npm.depends() 实现的用于 Spin.js的Meteor 包包装器。安装meteor add ...
Meteor 是一个构建在 Node.js 之上的平台,用来开发实时网页程序。Meteor 位于程序数据库和用户界面之间,保持二者之间的数据同步更新。在过去的几年中,我们一直在开发很多个 Meteor 项目,范围从网站到移动应用,...
meteor-postgresql, 用于 Meteor的PostgreSQL连接器 Meteor 连接器这个包仅仅包装了非常好的编写和维护的npm包 pg,由 brianc 。 pg文档陨石安装$ mrt add postgresql示例应用程序$ cd example && meteor测
meteor-graphql, GraphQL支持Lokka的Meteor 支持与 Lokka的Meteor这是 Meteor的可选数据层,带有 GraphQL 。 基本上,它将允许你向客户机公开GraphQL模式。然后,你可以使用Lokka与客户端中的GraphQL模式交互。用法...
meteor-starter, 启动 Meteor 项目 Meteor 启动器一个 Meteor 样板,里面装了很多。 用Coffeescript编写。教程MIT许可证由 Meteor 工厂维护。 专业 Meteor 开发。 设置git clone https://gith
Meteor Web Application Development Cookbook Meteor应用开发参考
meteor-devtools, 用于 Meteor的非常方便的开发工具 Meteor 玩具Meteor 玩具是应用开发工具中的一组,用于转换你的开发体验。 因为它们是debugOnly包,所以它们不会编译到你的生产构建中。 :玩具提供的功能是什么?...
meteor.sh, 设置 Meteor 服务器并将 Meteor 应用程序部署到它 这个项目不再被维护 !你可能已经注意到了,这里没有发生过。脚本很可以能在各种方式中被破坏,请不要在任何重要的情况下使用它。这里项目现在主要作为...
meteor-redux-middlewares, 中间件将 Meteor 反应源与redux存储同步 meteor-redux-middlewares 中间件将 Meteor 反应源与redux存储同步。现场演示插件GitHub上的演示源在 npm 上的包在大气环境中的包。安装使用 npm...
meteor-tutorials, 为 Meteor 应用创建超酷动画教程 流星教程这是什么?轻松为你的Meteor 应用创建超酷动画教程。 这个软件包为你提供了一种简单的方法来制作多个步骤教程,用于聚集用户界面。 多么伟大的方式展示你...