Meteor Shower

链接:https://ac.nowcoder.com/acm/contest/997/I
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入描述:

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

输出描述:

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
示例1

输入

复制

4
0 0 2
2 1 2
1 1 2
0 3 5

输出

复制

5

说明

Examining the plot above at t=5, the closest safe point is (3, 0) -- but Bessie's path to that point is too quickly blocked off by the second meteor. The next closest point is (4,0) -- also blocked too soon.  Next closest after that are lattice points on the (0,5)-(5,0) diagonal. Of those, any one of (0,5), (1,4), and (2,3) is reachable in 5 timeunits.
5|. . . . . . .
4|. . . . . . .
3|3 4 5 . . . . Bessie's locations over time
2|2 . . . . . . for one solution
1|1 . . . . . .
0|0 . . . . . .
--------------
0 1 2 3 4 5 6 一直在想动态标记,结果。。。巧妙的map让人折服
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#include <queue>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
using namespace std;
const int N = ;
int c[N] , d[N];
int vis[][] , map[][];
int dir[][] = {{ , }, {- , } , { , } , { , -}}; struct node{
int x , y , t;
node(int x = , int y = , int t = )
{
this->x = x ;
this->y = y ;
this->t = t ;
}
}; int bfs(int x , int y)
{
queue<node>s;
struct node next;
s.push(node(x , y , ));
vis[x][y] = ;
while(!s.empty())
{
next = s.front();
s.pop();
if(map[next.x][next.y] == )
{
return next.t ;
}
for(int i = ; i < ; i++)
{
int xx = next.x + dir[i][];
int yy = next.y + dir[i][];
int tt = next.t + ;
if(!vis[xx][yy] && xx >= && yy >= && tt < map[xx][yy])
{
vis[xx][yy] = ;
s.push(node(xx , yy , tt));
}
}
}
return - ; } int main()
{ int n ;
while(scanf("%d" , &n) != EOF)
{
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
map[i][j] = ;
}
}
for(int i = ; i < n ; i++)
{
int x , y , t ;
scanf("%d%d%d" , &x , &y , &t);
map[x][y] = min(map[x][y] , t);
for(int j = ; j < ; j++)
{
int xx = x + dir[j][];
int yy = y + dir[j][];
if(xx >= && yy >= )
map[xx][yy] = min(map[xx][yy] , t);
}
}
cout << bfs( , ) << endl ;
} return ;
}

bfs(火星撞地球)的更多相关文章

  1. poj 3669 火星撞地球问题 bfs算法

    题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间 思路:bfs+预处理 这题的数据量比较大,所以需要进行预处理 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1 for (int j = ...

  2. 彗星撞地球 | 近25万倍压缩的精品3D动画

    文章目录 写在前面 Prophecy<彗星撞地球> 下载地址 简概 注意 3D射击小游戏 下载地址 简概 写在前面 WareZ是个无形的组织,号称"不以赢利为目的纯技术团体&qu ...

  3. DDD 主题交流会总结及计划

    思维碰撞才能更加进步 2015年5月16日(上周六),我们举行了一次 DDD 主题的交流会,参会者主要是 ENode 群友.以及部分园友,为什么要搞这次交流会?原因很简单,就是希望可以提供对 DDD ...

  4. P1044 火星数字

    P1044 火星数字 转跳点:

  5. Wakez计算与压缩的思考

    大部分人认为,比尔盖茨在1981年说过这样一句话:640K ought to be enough for anybody.(640个kB对任何人来说都应该足够了).不过后来比尔盖茨否认自己说过这句话. ...

  6. hdu----(1075)What Are You Talking About(trie之查找)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. 005.数组、for、foreach

    1.方法的传输传递 值参数:传递的是副本 引用参数:自身 保留自定义的方法中对值的改变 形参影响实参ref:对应的形参和实参都用ref修饰 输出参数:实参不用赋值,但是自定义方法内必须对此参数赋值!! ...

  8. 《Google软件测试之道》测试工程师

    愿和我一样读过这本书的人有所共鸣或者启发,愿没读过这本书的人,能获得一点点收获... 说到软件测试工程师,首先我们需要明白一个问题,软件测试工程师的职责是什么? 关于这个话题,不同的人有不同的定义:抛 ...

  9. JavaOne 2016主旨演讲畅谈Java近期及远期规划

    在 JavaOne 2016 主题演讲开场,来自 Oracle 的 Java 产品管理负责人 Sharat Chander 指出 Java 盛行于个人和工作的日常生活各个领域,无论是大数据.物联网甚至 ...

随机推荐

  1. ES6——面向对象-基础

    面向对象原来写法 类和构造函数一样 属性和方法分开写的 // 老版本 function User(name, pass) { this.name = name this.pass = pass } U ...

  2. 用jquery制作的简单轮播图

    我也是进入H5前端的小菜鸟一枚,最近才进入jquery的学习,所以打算对自己的学习进行记录. 今天分享的是一个简单的轮播图,这个轮播图的特效很简单,能够进行图片的轮播以及点击相应图片,图片能够跳转到相 ...

  3. 【记录】spring boot 全局捕获异常@ExceptionHandler与@Validated @RequestBody 配合使用

    @ExceptionHandler与@Validated @RequestBody 三者配合使用可以很好的做到入参校验,具体demo如下: 接口 import org.springframework. ...

  4. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  5. spring requestbody json

    1  @requestbody string param 前台将jsonobject序列化成字符串 后台解析成JsonObject 2 @requestbody map<string,objec ...

  6. 二、冯式结构与哈佛结构及ARM处理器状态和处理器模式

    2.1 冯式结构与哈佛结构 2.1.1 两者的区别 如果是独立的存储架构和信号通道那就是哈佛结构,否则就是冯式结构 结构与是否统一编址没有关系,也与 CPU 没有关系,与计算机的整体设计有关 CACH ...

  7. java并发学习--第三章 线程安全问题

    线程的安全问题一直是我们在开发过程中重要关注的地方,出现线程安全问题的必须满足两个条件:存在着两个或者两个以上的线程:多个线程共享着共同的一个资源, 而且操作资源的代码有多句.接下来我们来根据JDK自 ...

  8. 如何对Web服务器进行飓风级防御

    万一你的网站被DDOS飓风式攻击,你会怎么办?笔者以经验告诉你,这里的每一层都起着保护网站的作用.如果您宁愿拥有如何在单台服务器上维持大量流量的版本,则可以: 专用硬件服务器,无共享主机 千兆上行链路 ...

  9. springboot 整合Druid

    Druid连接池监控配置 1) 引入依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <depende ...

  10. 【leetcode】1029. Two City Scheduling

    题目如下: There are 2N people a company is planning to interview. The cost of flying the i-th person to ...