POJ--3669--DFS

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 ≤ 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: 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
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int MAX_M = 50000;
const int MAX_N = 400 + 1;
const int INF = 100000000;
int M;
int X[MAX_M], Y[MAX_M], T[MAX_M];
int maze[MAX_N][MAX_N];                        //保存地图
int d[MAX_N][MAX_N];                        //保存最短步数
//4个方向
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int bfs(){
    //一开始就被炸
    if(maze[0][0] == 0) return -1;
    queue<P> que;
    que.push(P(0, 0));
    d[0][0] = 0;
    while(!que.empty()){
        P p = que.front();
        que.pop();
        //已到达安全位置
        int x = p.first, y = p.second;
        if(maze[x][y] == INF) return d[x][y];
        //4个方向走
        for(int i = 0; i < 4; i ++){
            int nx = x + dx[i], ny = y + dy[i];
            //判断是否可移动,是否访问过,以及下一个时刻是否安全
            if(0 <= nx && 0 <= ny && d[nx][ny] == INF && d[x][y] + 1 < maze[nx][ny]){
                que.push(P(nx, ny));
                d[nx][ny] = d[x][y] + 1;
            }
        }
    }
    return -1;
}
void solve(){
    //初始化地图
    for(int i = 0; i < MAX_N; i ++)
        fill(maze[i], maze[i] + MAX_N, INF);
    //模拟轰炸场景
    for(int i = 0; i < M; i ++){
        maze[X[i]][Y[i]] = min(maze[X[i]][Y[i]], T[i]);
        for(int j = 0; j < 4; j ++){
            int nx = X[i] + dx[j], ny = Y[i] + dy[j];
            if(0 <= nx && 0 <= ny)
                maze[nx][ny] = min(maze[nx][ny], T[i]);
        }
    }
    //初始化地图最小步数
    for(int i = 0; i < MAX_N; i ++)
        fill(d[i], d[i] + MAX_N, INF);
    //宽度优先搜索
    int ans = bfs();
    printf("%d\n", ans);
}

int main(){
    scanf("%d", &M);
    for(int i = 0; i < M; i ++){
        scanf("%d %d %d", &X[i], &Y[i], &T[i]);
    }
    solve();
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值