POJ3669-宽度优先搜索

133 阅读3分钟
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 32098 Accepted: 8247

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 (Xi, Yi) (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: 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

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

Sample Output

5

题意:

陨石降临,为了避难,罗西要去找一个陨石砸不到的地方。

  • 陨石砸一个点,这个点的四周也会被破坏
  • 罗西无法走陨石砸过的路

注意:

  • 陨石落下的t可以为0,也就是说,如果起点有一个0时刻的陨石,就直接没了。
  • 边界虽然是300,但是由于陨石是波及的,所以301也可能被砸到
  • 数据输入的顺序不是按先坠落的先输入

解法:

  • 先计算出每个陨石坠落的时间,然后跑bfs就可以了

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;

int M,tx,ty,tt;
int time[310][310];//记录陨石坠落时间
int dis[310][310];//记录到这里的时间
int dire[5][2] = {0,0,0,1,0,-1,1,0,-1,0};//方向数组
pair<int , int> tem;
queue<pair <int, int> > q;


int  bfs(){
    tem.first = 0;
    tem.second = 0;
    q.push(tem);
    dis[0][0] = 0;

    //起点有0时刻的陨石
    if(time[0][0] <= dis[0][0]){
        return -1;
    }

    while(!q.empty()){
        tem = q.front();
        q.pop();

        for(int i = 1; i < 5; i++){
            int xx = tem.first + dire[i][0];
            int yy = tem.second + dire[i][1];

            if(xx >= 0 && yy >= 0 && dis[tem.first][tem.second] + 1 < time[xx][yy] && dis[xx][yy] == 0x3f3f3f3f){
                if(time[xx][yy] == 0x3f3f3f3f){//找到一个陨石不会落下的地方,结束
                    return dis[tem.first][tem.second] + 1;
                }else{
                    dis[xx][yy] = dis[tem.first][tem.second] + 1;
                    q.push(pair<int,int>(xx,yy));
                }
            }
        }
    }

    return -1;
}

int main(void){

    scanf("%d",&M);
    memset(time,0x3f,sizeof(time));
    memset(dis, 0x3f3f, sizeof(dis));
    while(M--){
        scanf("%d %d %d",&tx,&ty,&tt);
        for(int i = 0; i < 5; i++){
            int xx = tx + dire[i][0];
            int yy = ty + dire[i][1];

            if(xx >= 0 && yy >= 0){
                if(tt < time[xx][yy]){//比之前的陨石先落下,修改数据
                    time[xx][yy] = tt;
                }
            }
        }
    }

    cout << bfs() << endl;

    return 0;
}