POJ3669(Meteor Shower)(bfs求最短路)

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12642   Accepted: 3414

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

Source

USACO 2008 February Silver
 
题意:用最短的时间找到最安全的位置。
注意坑:1.当坐标为(0,0)时,上下左右扩展时下标会越界。
         2. 每个地点的时间可能重复,取最小的。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 310

struct Node
{
    int x,y,t;
};

int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
int vis[maxn][maxn];
int pic[maxn][maxn];
int n;
void bfs()
{
    Node a,b;
    a.x = 0; a.y = 0; a.t = 0;
    queue<Node> sun;
    sun.push(a);
    while(!sun.empty())
    {
        Node temp = sun.front();
        sun.pop();
        for(int i = 0; i < 4; i++)
        {
             b.x = temp.x + dx[i];
             b.y = temp.y + dy[i];
             b.t = temp.t + 1;

             if(b.t < pic[b.x][b.y] && !vis[b.x][b.y]&&b.x>=0&&b.y>=0)
            {
                vis[b.x][b.y] = 1;
                //puts("QAQ");
                if(pic[b.x][b.y] == INF )
                {
                    printf("%d\n", b.t);
                    return;
                }
                //printf("%d %d\n", b.x, b.y);
                sun.push(b);
            }
        }
    }
    printf("-1\n");
}
int main()
{
    while(~scanf("%d", &n))
    {
        memset(pic, INF, sizeof pic);
        memset(vis, 0, sizeof vis);
        int x,y,t;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d", &x,&y,&t);
            pic[x][y] = min(pic[x][y],t);
            for(int j = 0; j < 4; j++)
            {
                int nx = x + dx[j];
                int ny = y + dy[j];
                if(nx >= 0 && ny >= 0)
                pic[nx][ny] = min(pic[nx][ny],t);
            }
        }
        vis[0][0] = 1;
        bfs();
    }
    return 0;
}

 

你可能感兴趣的:(POJ3669(Meteor Shower)(bfs求最短路))