zoukankan      html  css  js  c++  java
  • bfs(火星撞地球)

    Meteor Shower

    链接:https://ac.nowcoder.com/acm/contest/997/I
    来源:牛客网

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    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.

    输入描述:

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

    输出描述:

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

    输入

    复制
    4
    0 0 2
    2 1 2
    1 1 2
    0 3 5
    

    输出

    复制
    5

    说明

    Examining the plot above at t=5, the closest safe point is (3, 0) -- but Bessie's path to that point is too quickly blocked off by the second meteor. The next closest point is (4,0) -- also blocked too soon.  Next closest after that are lattice points on the (0,5)-(5,0) diagonal. Of those, any one of (0,5), (1,4), and (2,3) is reachable in 5 timeunits.
    5|. . . . . . .
    4|. . . . . . .
    3|3 4 5 . . . . Bessie's locations over time
    2|2 . . . . . . for one solution
    1|1 . . . . . .
    0|0 . . . . . .
    --------------
    0 1 2 3 4 5 6

    一直在想动态标记,结果。。。巧妙的map让人折服
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <set>
    #include <stack>
    #include <string.h>
    #include <vector>
    #include <queue>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    using namespace std;
    const int N = 509;
    int  c[N] , d[N];
    int vis[309][309] , map[309][309];
    int dir[4][2] = {{1 , 0}, {-1 , 0} , {0 , 1} , {0 , -1}};
    
    struct node{
        int x , y , t;
        node(int x = 0 , int y = 0 , int t = 0)
        {
            this->x = x ;
            this->y = y ;
            this->t = t ;
        }
    };
    
    int bfs(int x , int y)
    {
        queue<node>s;
        struct node next;
        s.push(node(x , y , 0));
        vis[x][y] = 1 ;
        while(!s.empty())
        {
            next = s.front();
            s.pop();
            if(map[next.x][next.y] == 1009)
            {
                return next.t ;
            }
            for(int i = 0 ; i < 4 ; i++)
            {
                int xx = next.x + dir[i][0];
                int yy = next.y + dir[i][1];
                int tt = next.t + 1 ;
                if(!vis[xx][yy] && xx >= 0 && yy >= 0 && tt < map[xx][yy])
                {
                    vis[xx][yy] = 1 ;
                    s.push(node(xx , yy , tt));
                }
            }
        }
        return -1 ;
    
    }
    
    
    int main()
    {
    
    
        int n ;
        while(scanf("%d" , &n) != EOF)
        {
            for(int i = 0 ; i < 309 ; i++)
            {
                for(int j = 0 ; j < 309 ; j++)
                {
                    map[i][j] = 1009 ;
                }
            }
            for(int i = 0 ; i < n ; i++)
            {
                int x , y , t ;
                scanf("%d%d%d" , &x , &y , &t);
                map[x][y] = min(map[x][y] , t);
                for(int j = 0 ; j < 4 ; j++)
                {
                    int xx = x + dir[j][0];
                    int yy = y + dir[j][1];
                    if(xx >= 0 && yy >= 0)
                    map[xx][yy] = min(map[xx][yy] , t);
                }
            }
             cout << bfs(0 , 0) << endl ;
        }
    
    
        return 0 ;
    }
  • 相关阅读:
    cogs luogu 1901. [国家集训队2011]数颜色 待修改莫队
    luogu cogs 421. HH的项链
    luogu P2709 小B的询问
    排序
    算法基本概念
    金融的简单介绍
    Admin组件-----Django自带
    day02-菜单处理
    day01
    selenium常用方法
  • 原文地址:https://www.cnblogs.com/nonames/p/11317879.html
Copyright © 2011-2022 走看看