zoukankan      html  css  js  c++  java
  • poj--3669

    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 Input

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

    Sample Output

    5

    题目大意:陨石从天而降,将地面砸出一个大坑,坑的上下左右都不是安全区,Bessie需要到达安全区域。

    题解:需要对能被砸中的区域进行预处理,然后因为要找到一个最短的路径所以这题就可以采用BFS了。
      
    代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    using namespace std;
    const int Max_num=305;
    int map[Max_num][Max_num];
    bool visited[Max_num][Max_num];
    int M;
    int dx[5]={0,1,0,-1,0};
    int dy[5]={1,0,-1,0,0};
    struct Metor{
        int x,y,t;
    };
    int BFS()
    {
        Metor m;
        m.x=m.y=m.t=0;
        queue<Metor> que;
        que.push(m);
        memset(visited,0,sizeof(visited));
        visited[m.x][m.y]=true;
        while(!que.empty())
        {
            Metor p=que.front();
            que.pop();
            
            if(map[p.x][p.y]==-1)
                return p.t;
            
            for(int i=0;i<4;i++)
            {
                Metor q;
                q.x=p.x+dx[i];
                q.y=p.y+dy[i];
                q.t=p.t+1;
                if(q.x>=0 && q.y>=0 && !visited[q.x][q.y])
                {
                    if(map[q.x][q.y]==-1)    //˵Ã÷ÒѾ­Åܳö°²È«Çø
                        return q.t; 
                    if(map[q.x][q.y]>q.t)
                    {
                        visited[q.x][q.y]=true;
                        que.push(q);
                    }
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(cin >> M)
        {
            memset(map,-1,sizeof(map));
            int x,y,t;
            for(int i = 0;i < M;i++)
            {
                cin >> x >> y >> t;
                for(int j = 0;j < 5;j++)
                {
                    int nx = x + dx[j],ny = y + dy[j];
                    if(nx >= 0 && ny >= 0)
                    {
                        if(map[nx][ny] == -1)
                            map[nx][ny] = t;
                        else
                            map[nx][ny] = min(map[nx][ny],t);
                    }
                    //cout << maps[nx][ny] << endl;
                }
            }
                cout << BFS() << endl;
        }
        return 0;
    }
     
  • 相关阅读:
    新建安卓项目后,manifest.xml中会出现大段的黄色警告
    TextView设置setCompoundDrawables不生效解决办法
    数据结构->队列->顺序循环队列ADT代码
    数据结构->栈->顺序栈ADT代码
    JAVA环境变量的配置
    计算机科学的范围-----18.12.08
    字符串复制
    扔鸡蛋问题和找零钱问题
    动态规划
    最小二乘法
  • 原文地址:https://www.cnblogs.com/acmblog/p/9568212.html
Copyright © 2011-2022 走看看