题目描述

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 will strike, with meteor i will striking point at time . 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: and 

输出描述:

* 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

解答

题意:

这个同学发现流星雨要来了,会对地球造成伤害于是决定逃跑。N颗流星会不定时降落在坐标轴第一象限内的点上。给出每颗流星降落的坐标和时刻,求这个同学能否成功逃跑,能的话用时多少。

思路:

略有一点tricky,无法变通了(@﹏@)~。看了码农场的代码

这道题没有给出地图,所以需要自己生成地图。

关键的点在于:在生成地图的时候,将每个点的初始值设为无穷大(表示不会有流星),将要被摧毁的点设置为流星降落的时刻。如果原点从一开始就被炸毁,那么直接死亡,否则开始BFS。

从原点开始广度优先地游荡,判断要不要到一个点的依据是该点在地图内,并且该点此前未被访问过,也未被炸毁(可能在将来被炸毁)。每到一个点,当前时间+1.

在游荡过程中一旦发现一个点永远不会被炸(>last变量,或者等于INF?),说明找到了活命点,返回当前所用时间。

如果一直到遍历完毕都找不到这样的点,说明无法逃跑,返回-1。

代码:

刚开始提交的时候总是Runtime Error,de了半天发现input数组本应该是50000,设置成了5000……这种粗心的错误要少犯啊

#include <iostream>
#include <algorithm>
#include <stdio.h>   
#include <memory.h>  
#include <queue>

using namespace std;

const int MAX_INDEX = 512;
int eMap[MAX_INDEX][MAX_INDEX];
bool visited[MAX_INDEX][MAX_INDEX];

struct Meteor {
    int x;
    int y;
    int t;
};

typedef Meteor P;

Meteor input[50000];
int n;
int last = 0;

int dx[5] = {-1, 0, 1, 0, 0};
int dy[5] = {0, -1, 0, 1, 0};

int bfs() {
    memset(visited, 0, sizeof(visited));
    queue<P> que;
    P cur;
    cur.x = 0;
    cur.y = 0;
    cur.t = 0;
    que.push(cur);
    
    while (que.size()) {        
        for (int i = 0; i < 4; i++) {
            cur = que.front();
            cur.x += dx[i];
            cur.y += dy[i];
            cur.t++;
            if (cur.x >= 0 && cur.y >= 0 && cur.t < eMap[cur.x][cur.y] && !visited[cur.x][cur.y]) {
                visited[cur.x][cur.y] = true;
                if (eMap[cur.x][cur.y] > last) {
                    return cur.t;
                }                
                que.push(cur);
            }
        }        
        que.pop();
    }
    return -1;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> input[i].x >> input[i].y >> input[i].t;
    }
    
    memset(eMap, 0x7f, sizeof(eMap));   //将地图上所有点的值设为极大值(按题意只需大于1000就可) 
    for (int i = 0; i < n; i++) {       //创建地图 
        last = max(last, input[i].t);   //last变量记录流星下落的最晚时间 
        for (int j = 0; j < 5; j++) {
            int nx = input[i].x + dx[j];
            int ny = input[i].y + dy[j];
            if (nx >= 0 && ny >= 0 && eMap[nx][ny] > input[i].t) {
                eMap[nx][ny] = input[i].t;
            }
        }     
    }
    
    if (eMap[0][0] == 0) {
        return -1;
    } 
    else { 
        cout << bfs() << endl;
    }
    
    return 0;
}


来源:小林鼠草草的窝