Meteor Shower (bfs)

来源:互联网 发布:软件接口安全设计 编辑:程序博客网 时间:2024/05/22 17:25

Meteor Shower

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
Problem 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: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
40 0 22 1 21 1 20 3 5
 

Sample Output
5
 

Source
PKU

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


先建图 把被摧毁的坐标用时间记录一下,没有摧毁的用INF标记,优先队列bfs先找到的INF点就是答案

#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <queue>using namespace std;int n;const int maxn = 310;const int INF = 0x3fffffff;int map[maxn][maxn];int visited[maxn][maxn];int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};struct node {    int x, y, step;    void set(int x, int y, int s) {        this->x = x;        this->y = y;        step = s;    }    friend bool operator < (const node& n1,const node& n2) {        return n1.step > n2.step;    }};int bfs() {    memset(visited, 0, sizeof(visited));    priority_queue<node> que;    node cur, nex;    cur.set(0, 0, 0);    que.push(cur);    visited[0][0] = 1;    while (!que.empty()) {        cur = que.top();        que.pop();        if (map[cur.x][cur.y] == INF) {                //cout << cur.x << cur.y << endl;            return cur.step;        }        for (int i = 0; i < 4; ++i) {            nex.set(cur.x + dir[i][0], cur.y + dir[i][1], cur.step + 1);            if (nex.x >= 0 && nex.y >= 0 && map[nex.x][nex.y] > nex.step && !visited[nex.x][nex.y]) {                que.push(nex);                visited[nex.x][nex.y] = 1;            }        }    }    return -1;}int main() {        //freopen("in.txt", "r", stdin);    while (scanf("%d", &n) != EOF){        int x, y, t;        for (int i = 0; i < maxn; ++i) {            for (int j = 0; j < maxn; ++j) {                map[i][j] = INF;            }        }        for (int i = 0; i < n; ++i) {            scanf("%d%d%d", &x, &y, &t);            map[x][y] = min(map[x][y], t);            for (int j = 0; j < 4; ++j) {                int nx = x + dir[j][0];                int ny = y + dir[j][1];                               if (nx >= 0 && ny >= 0) {                    map[nx][ny] = min(map[nx][ny], t);                }            }        }        printf("%d\n", bfs());    }}


 
原创粉丝点击