Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26455   Accepted: 6856

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

题意:流星来袭,在一个方格中,贝茜在(0,0)点,为了防止被流星击中,贝茜需要移动到安全的位置。一共有n颗流星,给出流星落下的坐标(xi,yi)和时间ti,每一颗流星落地时上下左右的格子也为遭受毁灭。贝茜经过的路程是不能被毁灭的。

题解:首先要建图,用map[][]数组储存好每个点遭受流星打击的时间,不受流星打击的用-1涵盖。(不能用0覆盖,部分点可能在0时刻遭受打击) 然后遍历整张图,找到不受流星打击的点即可,因为要求最短时间,所有要用到优先队列,优先时间最小的点出队列。根据题意我们知道在图内的不受流星打击的点和没有经过流星打击的点能进入队列。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int dx[]={,,-,},dy[]={,,,-};
int maze[maxn][maxn];
int vis[maxn][maxn]; struct node
{
int x,y,step;
}temp,a;
bool operator < (const node &a,const node &b)
{
return a.step>b.step;
} int min(int a,int b)
{
return a>b?b:a;
}
void bfs()
{
int i;
memset(vis,,sizeof(vis));
priority_queue<node>q;
a.x=; a.y=;
a.step=;
q.push(a);
vis[][]=;
while(!q.empty())
{
a=q.top();
q.pop();
if(maze[a.x][a.y]==-)
{
cout<<a.step<<endl;
return ;
}
for(i=;i<;i++)
{
temp.x=a.x+dx[i];
temp.y=a.y+dy[i];
temp.step=a.step+;
if(temp.x>= && temp.y>= && (maze[temp.x][temp.y]==- || maze[temp.x][temp.y]>temp.step))
{
if(!vis[temp.x][temp.y])
q.push(temp);
vis[temp.x][temp.y]=;
}
} }
cout<<-<<endl; } int main()
{
int n,x,y,t;
memset(maze,-,sizeof(maze));
cin>>n;
for(int i=;i<n;i++)
{
cin>>x>>y>>t;
if(maze[x][y]==-)
maze[x][y]=t;
else
maze[x][y]=min(maze[x][y],t);
for(int j=;j<;j++)
{
int nx=x+dx[j];
int ny=y+dy[j];
if(nx>= && ny>=)
{
if(maze[nx][ny]==-)
maze[nx][ny]=t;
else
maze[nx][ny]=min(t,maze[nx][ny]);
}
} }
bfs();
return ;
}

Meteor Shower POJ - 3669 (bfs+优先队列)的更多相关文章

  1. BFS:Meteor Shower(POJ 3669)

         奔跑吧,傻牛 题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下 ...

  2. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  3. poj 3635(bfs+优先队列)

    题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...

  4. poj3669 Meteor Shower(预处理+bfs)

    https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...

  5. 【BZOJ】1611: [Usaco2008 Feb]Meteor Shower流星雨(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1611 一眼题,bfs. #include <cstdio> #include <c ...

  6. bzoj 1611: [Usaco2008 Feb]Meteor Shower流星雨【BFS】

    t记录每个格子最早被砸的时间,bfs(x,y,t)表示当前状态为(x,y)格子,时间为t.因为bfs,所以先搜到的t一定小于后搜到的,所以一个格子搜一次就行 #include<iostream& ...

  7. poj 3669 bfs(这道题隐藏着一个大坑)

    题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...

  8. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  9. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

随机推荐

  1. nodejs 实践:express 最佳实践(六) express 自省获得所有的路由

    nodejs 实践:express 最佳实践(六) express 自省获得所有的路由 某些情况下,你需要知道你的应用有多少路由,这在 express 中没有方法可以.因此我这边曲线了一下,做成了一个 ...

  2. python的subprocess模块(写的不错留作查询)

    python的subprocess模块 subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*. ...

  3. base-command

    命令分类 自带命令 工具型 文件系统 第三方命令 CLI GUI 操作文件系统 pwd(print working directory) ls(list files) ls 列出当前目录文件 ls 目 ...

  4. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  5. SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇)

    SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇) 最近真的没有什么精力写文章,天天加班,为了完成这个系列,硬着头皮上了 再看这篇文章之前请大家先看我之前写的第一篇和第二篇 第一篇: ...

  6. web端 repeat和简单控件

    <%@ %> - 这里面写一些声明和引用的<% %> - 编写C#代码的<%= %><%# %> Repeater - 重复器     相当于winfo ...

  7. 洛谷 P2733 家的范围 Home on the Range

    题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...

  8. UVA Planning mobile robot on Tree树上的机器人(状态压缩+bfs)

    用(x,s)表示一个状态,x表示机器人的位置,s表示其他位置有没有物体.用个fa数组和act数组记录和打印路径,转移的时候判断一下是不是机器人在动. #include<bits/stdc++.h ...

  9. Jquery库插件大全(工作中遇到总结)

    Jquery UI所有插件下载:http://jqueryui.com/download/all/ Jquery layer灯箱等演示与帮助:http://sentsin.com/jquery/lay ...

  10. 绘制方式和OpenGL枚举对应关系

    绘制方式和OpenGL枚举对应关系 图元类型 OpenGL枚举量 点 GL_POINTS 线 GL_LINES 条带线 GL_LINE_STRIP 循环线 GL_LINE_LOOP 独立三角形 GL_ ...