zoukankan      html  css  js  c++  java
  • POJ 3669 Meteor Shower (BFS+预处理)

    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

    Sample Input
    4
    0 0 2
    2 1 2
    1 1 2
    0 3 5
    
    Sample Output
    5

    题意:

      巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方。已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间,否则输出-1。

    思路:

      首先根据题意自己构建迷宫,将map数组初始化为MAX,表示这个格子被袭击的时间为INF(即永远不会被袭击)。对于每一个流星,将其影响反映到map上,如果破坏范围由重叠,那么格子显示的是较早的破坏时间,下一步用BFS搜索。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #define MAX 0x3f3f3f3f
    using namespace std;
    int map[305][305];//存节点信息
    int vis[305][305];//标记数组
    int dir[4][2]= {-1,0, 1,0, 0,1, 0,-1};//上下左右四个方向
    int end;
    struct node
    {
        int x,y;//两点表示节点位置
        int time;
    } start;//入队列使用
    queue<node> q;//队列,自己维护用来存储节点信息
    int bfs(int x,int y)
    {
        memset(vis,0,sizeof(vis));
        start.x=x,start.y=y,start.time=0;//将传递过来的0.0节点放入结构体
        vis[x][y]=1;//标记为已搜过
        q.push(start);//入队列
        while(!q.empty())
        {
            node now=q.front();//取队头元素
            q.pop();
            if(map[now.x][now.y]==MAX)
            {
                return now.time;//如果符合条件,返回;根据题意自己写符合的条件。
            }
            for(int i=0; i<4; i++)//四个方向入队列
            {
                start.x=now.x+dir[i][0],start.y=now.y+dir[i][1];//将第一个方向的入队列
                start.time=now.time+1;
                if(start.x>=0&&start.y>=0&&vis[start.x][start.y]==0&&start.time<map[start.x][start.y])//判断是否越界
                {
                    vis[start.x][start.y]=1;
                    q.push(start);
                }
            }
        }
        return -1;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            memset(map,MAX,sizeof(map));
            for(int j=0; j<n; j++)
            {
                int x,y,time;
                scanf("%d%d%d",&x,&y,&time);
                if(map[x][y]>time)
                    map[x][y]=time;
                for(int i=0; i<4; i++)//自己建图过程,一般不需要自己建图
                {
                    int cx,cy;
                    cx=x+dir[i][0],cy=y+dir[i][1];
                    if(cx>=0&&cy>=0)
                        if(map[cx][cy]>time)
                            map[cx][cy]=time;
                }
            }
            int ans=bfs(0,0);//从00点开始广搜,根据题目要求具体定
            cout<<ans<<endl;
        }
    
    }
  • 相关阅读:
    用XPath定位Web页面元素时,如何快速验证XPath语句是否正确?
    遇到Web页面禁用鼠标右键操作时,该如何解禁?
    Python之win32模块
    Python之smtplib模块
    Python之telnetlib模块
    使用HttpRunner3+Allure+Jenkins实现Web接口自动化测试
    Python实现Thrift Server
    Python之requests模块-hook
    php新手常用的函数(随时更新)
    手机测试pc端网页
  • 原文地址:https://www.cnblogs.com/aiguona/p/7269012.html
Copyright © 2011-2022 走看看