O - Meteor Shower ~~~ [kuangbin带你飞]专题一 简单搜索

Description - 题目描述

Bessie听说有场史无前例的流星雨即将来临;有谶言:陨星将落,徒留灰烬。为保生机,她誓将找寻安全之所(永避星坠之地)。目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点。

此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti  ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300)。每颗流星都将摧毁落点及其相邻四点的区域。

Bessie在0时刻时处于原点,且只能行于第一象限,以平行与坐标轴每秒一个单位长度的速度奔走于未被毁坏的相邻(通常为4)点上。在某点被摧毁的刹那及其往后的时刻,她都无法进入该点。

寻找Bessie到达安全地点所需的最短时间。

Input - 输入

* 第1行: 一个整数: M
* 第2..M+1行: 第i+1行包含由空格分隔的三个整数: Xi, Yi, and Ti

Output - 输出

* 仅一行: Bessie寻得安全点所花费的最短时间,无解则为-1。

Sample Input - 输入样例

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

Sample Output - 输出样例

5

思路 : 找出陨星砸到某个点的最开始时间,特判下开始会不会被砸死;

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#define ll long long
using namespace std;
int a[305][305];
int book[305][305];
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
const int MAXN = 1005 ;
struct Node
{
    int x,y,s;
    Node () {} 
    Node(int xx,int yy,int ss) : x(xx) , y(yy) , s(ss){}
};
int bfs(int x,int y ,int s)
{

    queue<Node> Q;
    Q.push(Node(x,y,s));
    book[x][y] = 1 ;
    while(!Q.empty())
    {
        Node u = Q.front() ;
        Q.pop() ;
        
        if(u.x>300  || u.y>300 ||  a[u.x][u.y] == MAXN)
        {
            
            return u.s;
        }
        for(int i=0 ; i<4 ; i++)
        {
            int xx = u.x + dx[i] ;
            int yy = u.y + dy[i] ;
            int ss = u.s + 1 ;
            if(xx>=0 && yy>=0  &&  ss < a[xx][yy] && book[xx][yy] == 0)
            {
                book[xx][yy] = 1;
                Q.push(Node(xx,yy,ss));
                
            } 
        }
    }
    return -1;
}
int main()
{
    int M;
    while(~scanf("%d",&M))
    {
        memset(book,0,sizeof(book)); 
        for(int i=0 ; i<305 ;i++)
        {
            for(int j=0 ; j<305 ; j++)
            { 
                a[i][j] = MAXN ;
            }
        }
    
        while(M--)
        {
            int xi,yi,ti;
            scanf("%d%d%d",&xi,&yi,&ti);
            a[xi][yi] = min(a[xi][yi],ti) ;
            for(int i=0 ; i<4 ;i++)
            {
                int xx = xi + dx[i] ;
                int yy = yi + dy[i] ;
                if(xx>=0 && yy>=0)
                {
                    a[xx][yy] =  min(a[xx][yy],ti) ;
                } 
            }
    
        }
        if(a[0][0] == 0)
        {
            printf("-1\n");
            continue;
        }
        int ans = bfs(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43364008/article/details/84855115
今日推荐