poj 3669 Meteor Shower

                                                                                                  Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16339   Accepted: 4293

Descriptionios

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.spa

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).code

Determine the minimum time it takes Bessie to get to a safe place.blog

Inputthree

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

Outputget

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

Sample Inputio

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

Sample Output

5
題意: 牛Bessie想要躲避流星雨,因而準備乘飛機逃跑。首先已知有M顆流星,每顆流星的位置(x,y)以及下落的時間t都已知,牛的出發點位於座標軸原點,如今牛想要跑到一個流星砸不到的點,問至少要花多少時間(牛在出發點的時間記爲0,每走一步花1單位的時間)。
思路:首先用一張圖來記錄每個點會被流星砸到的最先時間,若某點不會被砸到則可記爲無窮大,以後廣度優先搜索,對於每個點,若在圖的範圍而且當前時間還沒被流星砸到且沒被訪問過,那麼訪問,以後看一下該點是否安全,安全就退出搜索,不然將該點入隊。
AC代碼:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N_MAX = 500;
int field[N_MAX][N_MAX];//標記好每一個位置被流星砸的時間
bool visited[N_MAX][N_MAX];
int direction[5][2] = { {1,0},{-1,0},{0,1},{0,-1},{0,0} };
int last;
struct MM {
 int X, Y, T;
 bool operator<(const MM&b)const{
     return T < b.T;
 }
};
MM m[50003];
int bfs(const int &x1,const int &y1,const int&cur_T) {
    memset(visited, 0, sizeof(visited));
    visited[x1][y1] = true;
    queue<MM>que;MM cur;
    cur.X = x1;cur.Y = y1, cur.T = cur_T;
    que.push(cur);
    while (!que.empty()) {
        MM p = que.front();que.pop();
        for (int i = 0;i < 4;i++) {
            cur = p;
            cur.X = direction[i][0] + p.X; cur.Y = direction[i][1] + p.Y;
            cur.T++;
            if (cur.X>= 0 && cur.Y>= 0 && field[cur.X][cur.Y]>cur.T&&!visited[cur.X][cur.Y]) {
                visited[cur.X][cur.Y] = true;
                if (field[cur.X][cur.Y]>last)//說明這一點不會被炸到
                    return cur.T;
                que.push(cur);
            }
        }
    }
    return -1;
}
int main() {
    int M;
    scanf("%d",&M);
    for (int i = 0;i < M;i++) 
        scanf("%d%d%d",&m[i].X,&m[i].Y,&m[i].T);

    sort(m,m+M);//按流星砸的時間的前後順序排
    last = m[M - 1].T;

    for (int i = 0;i < N_MAX;i++) {
        for (int j = 0;j < N_MAX;j++)
            field[i][j] = INT_MAX;
    }

    for (int i = 0;i < M;i++) {
        for (int j = 0;j < 5;j++) {
            int x = m[i].X + direction[j][0],y=m[i].Y+direction[j][1];
            if (x >= 0 && y >= 0 && field[x][y]>m[i].T)  field[x][y] = m[i].T; 
        }
    }

    if (field[0][0] == 0)
        cout << -1 << endl;
    else {
        cout << bfs(0,0,0) << endl;
    }

    return 0;
}
相關文章
相關標籤/搜索