当前位置: 首页 > news >正文

【搜索】POJ-3669 BFS

一、题目

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 ≤ 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: 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

二、思路

  • 安全区可以到301
  • 同一个点可以炸两次
  • 必须在第一象限
  • t可以等于0
  • 可以一开始就死,也可以一开始就是安全区

二、思路&心得

  • 要注意数据的范围
  • 广搜常用来求解某个问题的最小值,求解过程中,利用dist数组更新最小值

三、代码


int dist[MAX_SIZE][MAX_SIZE];

int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0};

typedef pair<int, int> P;

struct Meteor {
    int X;
    int Y;
    int T;
} meteors[MAX_M];

bool cmp (Meteor a, Meteor b) {
    if (a.T < b.T) return true;
    else return false;
}

bool isLegal(int x, int y) {
    if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
    else return false;
}

int bfs() {
    queue<P> que;
    if (map[0][0] == 0) return -1;
    else if (map[0][0] == MAX_TIME) return 0;
    que.push(P(0, 0));
    while (que.size()) {
        P p = que.front();
        que.pop();
        int px = p.first, py = p.second;
        if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
        for(int i = 0; i < 4; i ++) {
            int tx = px + dirction[i][0], ty = py + dirction[i][1];
            if (isLegal(tx, ty)) {
                if (map[tx][ty]  == MAX_TIME) {
                    return ++ dist[px][py];
                }
                if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
                    que.push(P(tx, ty));
                    dist[tx][ty] = dist[px][py] + 1;
                }
            }
        }
    }
    return -1;
}

void solve() {
    for (int i = 0; i < MAX_SIZE; i ++) {
        for (int j = 0; j < MAX_SIZE; j ++) {
            map[i][j] = MAX_TIME;
        }
    } 
    for (int i = 0; i < M; i ++) {
        scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
    }
    sort(meteors, meteors + M, cmp);
    for (int i = 0; i < M; i ++) {
        for (int j = 0; j < 5; j ++) {
            int tx = meteors[i].X, ty = meteors[i].Y;
            tx += dirction[j][0];
            ty += dirction[j][1];
            if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
                map[tx][ty] = meteors[i].T;
            }
        }
    }
    printf("%d\n", bfs());
}

int main() {
    while (~scanf("%d", &M)) {
        solve();
    }
    return 0;
}

转载于:https://www.cnblogs.com/CSLaker/p/7281171.html

相关文章:

  • Django model字段类型参考列表
  • 拓扑排序的原理及其实现
  • oracle11g 在azure云中使用rman进行实例迁移
  • Python三种排序算法
  • 最纯粹的直播技术实战02-Camera的处理以及推流
  • 一、MyBatis基本用法——3-Mapper XML映射文件
  • @Autowired和@Resource装配
  • ES6--ArrayBuffer
  • HDU 6078 Wavel Sequence
  • java io
  • 1.spring、mybatis、mysql整合需要的包
  • html行内元素和块级元素及其居中问题
  • 贝叶斯来理解高斯混合模型GMM
  • 键盘输入字符、数字,并判断数是否是2的阶次方数
  • [noip2015 d1t2] 信息传递
  • 「译」Node.js Streams 基础
  • CSS中外联样式表代表的含义
  • Electron入门介绍
  • Java小白进阶笔记(3)-初级面向对象
  • Js基础知识(一) - 变量
  • LeetCode29.两数相除 JavaScript
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • tensorflow学习笔记3——MNIST应用篇
  • Vim Clutch | 面向脚踏板编程……
  • vuex 学习笔记 01
  • 从0实现一个tiny react(三)生命周期
  • 搞机器学习要哪些技能
  • 面试总结JavaScript篇
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 前端存储 - localStorage
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 移动端唤起键盘时取消position:fixed定位
  • 云大使推广中的常见热门问题
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #AngularJS#$sce.trustAsResourceUrl
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (一)基于IDEA的JAVA基础12
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)mysql使用Navicat 导出和导入数据库
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • @Builder用法
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [ solr入门 ] - 利用solrJ进行检索
  • [2021 蓝帽杯] One Pointer PHP