首页 > 其他分享 >POJ--3669 Meteor Shower(bfs/稍微变通)

POJ--3669 Meteor Shower(bfs/稍微变通)

时间:2023-02-09 21:55:19浏览次数:62  
标签:-- MAX 3669 bfs int meteor time include

记录
21:37 2023-2-9

http://poj.org/problem?id=

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

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

bfs。注意:

  1. M是50000,要使用uint32_t,否则MLE。
  2. BFS注意不要走回头路,否则TLE。(哈哈,我是笨蛋。
  3. 题目问题,看discuss板块说数据会超过300。

是一个变通一下的bfs,经典的bfs都是面对墙壁等不通的地方,这里也是,只不过多了时间。
先将被破坏的地点及破坏时间都记录下来。在dfs的时候,需要判断在当前时间路径是否无法通行,无法通行的就不进行bfs。(注意别走回头路

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX_N 303

const int INF = 0x3f3f3f3f;
unsigned int M;
int result = -1;

typedef struct foo {
    int x, y, t;
    foo() {
        foo(0, 0, 0);
    }
    foo(int x, int y, int t):x(x), y(y), t(t) {
    }
} C;

C c;
int destory[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N] = {false};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};  //右 上 左 下

void bfs() {
    std::queue<C> q;
    
    C c(0, 0, 0);
    visited[0][0] = true;
    q.push(c);

    while (!q.empty()) {
        C c = q.front(); q.pop();
        if(destory[c.x][c.y] == INF) {
            result = c.t;
            break;
        }
        for(int i = 0; i < 4; i++) {
            int new_x = c.x + dx[i];
            int new_y = c.y + dy[i];
            if (0 <= new_x && 0 <= new_y) {
                if(c.t + 1 < destory[new_x][new_y] && visited[new_x][new_y] == false)
                {
                    q.push(C(new_x, new_y, c.t + 1));
                    visited[new_x][new_y] = true;
                }
            }
        }
    }
    
}

void init() {
    if(c.t < destory[c.x][c.y]) destory[c.x][c.y] = c.t;
    for(int k = 0; k < 4; k++) {
        int new_x = c.x + dx[k];
        int new_y = c.y + dy[k];
        if (0 <= new_x && 0 <= new_y) {
            if(c.t < destory[new_x][new_y])
                destory[new_x][new_y] = c.t;
        }
    }
}

void solve() {
    bfs();
    printf("%d\n", result);
}

int main() {
    std::fill(destory[0], destory[0] + MAX_N * MAX_N, INF);
    scanf("%d", &M);
    for(int i = 0; i < M; i++) {
        scanf("%d %d %d", &c.x, &c.y, &c.t);
        init();
    }
    solve();
}

标签:--,MAX,3669,bfs,int,meteor,time,include
From: https://www.cnblogs.com/57one/p/17107210.html

相关文章

  • 面向对象知识点汇总(小白必会)
    目录Python基础之面向对象一、编程思想1、面向过程2、面向对象二、类与对象的创建1、类的语法结构2、定义与调用3、给对象添加独有属性4、对象独有属性修改三、动态、静态......
  • kubernetes(k8s)基础学习-kubernetes是什么?有什么用?
    kubernetes(k8s)基础学习-kubernetes是什么?一、认识DockerDocker是什么先来看看Docker的图标:一条鲸鱼背上驮着四方形块的物品,就像一条海运船上装满集装箱,集装箱里......
  • 专辑口是心非小记
    专辑《口是心非》小记Datetime:2023-02-08T20:47:00+08:00Categories:MusicTags:Diary最近机缘巧合,因为《河》这首歌再次接触到了张雨生。至于机缘是啥,如果你(我)想不......
  • 算法刷题 Day 34 | ● 1005.K次取反后最大化的数组和 ● 134. 加油站 ● 135. 分发糖
    1005.K次取反后最大化的数组和本题简单一些,估计大家不用想着贪心,用自己直觉也会有思路。https://programmercarl.com/1005.K%E6%AC%A1%E5%8F%96%E5%8F%8D%E5%90%8E%......
  • 为什么在容器中 1 号进程挂不上 arthas?
    作者:卜比本文是《容器中的Java》系列文章之4/n,欢迎关注后续连载......
  • Kaguya数
    首先在我和\(Kaguya\)关于第三次数学危机和逻辑学的激烈讨论下做出了以下决议:首先达成了以下共识:1.一个逻辑构成的集合内,逻辑会自然增殖,直到填补所有空缺或产生矛盾......
  • Educational Codeforces Round 118 (Rated for Div. 2)(D,E)
    EducationalCodeforcesRound118(RatedforDiv.2)(D,E)DD这个题大意给我们一个长度为\(n\)的序列,然后我们需要知道有多少个满足一下条件的子序列对于这一个序列,长度......
  • 决策单调性与四边形不等式
    参考自:《决策单调性与四边形不等式》,彭思进,感谢Itst的耐心答疑/bx《决策单调性与四边形不等式-学习笔记》,p_b_p_b的博客一、决策单调性与四边形不等式定义1.......
  • m基于FPGA的数字下变频verilog设计
    1.算法描述整个数字下变频的基本结构如下所示  NCO使用CORDIC算法,CIC采用h结构的CIC滤波器,HBF采用复用结构的半带滤波器,而FIR则采用DA算法结构。     这里,我......
  • 3.5正则表达式和EXCESS系统
       尾数部分使用正则表达式,可以将表现形式多样的浮点数统一为一种表现型时。例如,十进制数0.75就有很多中表现形式,如图3-5所示。     单精度浮点数的正则......