BFS+预处理 POJ - 3669 



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


#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
using namespace std;
int n,x,y,time;
int maps[305][305];
int visit[305][305];
int dir[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}};  //包含本身的位置
struct point
{
    int x;
    int y;
    int time;
};
int BFS()
{
    if(maps[0][0]==0) return -1;  //maps[0][0]==0表示零时刻有炸弹爆炸 那么这个人就挂了
    if(maps[0][0]==-1) return 0;  //maps里面是-1代表这个点永远是安全的  不用移动了
    memset(visit,0,sizeof(visit));
    visit[0][0]=1;
    point p;
    p.x=0; p.y=0;p.time=0;
    queue<point> que;
    que.push(p);
    while(!que.empty())
    {
        point before=que.front();
        que.pop();
        if(maps[before.x][before.y]==-1) return before.time;
        else{
            for(int k=0;k<4;k++)
            {
                point next;
                next.x=before.x+dir[k][0];
                next.y=before.y+dir[k][1];
                next.time=before.time+1;
                if( next.x>=0 &&next.y>=0 && !visit[next.x][next.y])
                {
                   if(maps[next.x][next.y]==-1) return next.time;
                   else
                   {
                       if(maps[next.x][next.y]>next.time)
                       {
                           visit[next.x][next.y]=1;
                           que.push(next);
                       }
                   }
                }
            }
        }
    }
    return -1;
}
int main()
{
   scanf("%d",&n);
   memset(maps,-1,sizeof(maps));
   for(int i=1;i<=n;i++)
   {
       scanf("%d%d%d",&x,&y,&time);
         for(int k=0;k<5;k++)
         {
             int xx=x+dir[k][0];
             int yy=y+dir[k][1];
             if(xx>=0 && yy>=0)
             {
                 if(maps[xx][yy]==-1)
                    maps[xx][yy]=time;
                 else
                    maps[xx][yy]=min(maps[xx][yy],time);
             }
         }

   }
    printf("%d\n",BFS());
    return 0;
}


/*解题思路(参考大神的):

首先对矩阵进行预处理,先将矩阵置为-1表示从没有流星雨来临,
然后将流星雨来临的点及其周围四格置为流星雨降临的时间,
同一格有两次以上的流星雨时取时间最早的,因为求最短路不会走走过的点。
然后从(0,0)开始BFS。原点处的值为-1则说明已经是安全的地方不需要移动,值为0说明开始就挂了。
用maps[i][j] > next.t 就可以判断这个点是否可以在当前时间暂时访问那*/



/*题意:

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于C++&OPENCV 的全景图像拼接 C++是一种广泛使用的编程语言,它是由Bjarne Stroustrup于1979年在新泽西州美利山贝尔实验室开始设计开发的。C++是C语言的扩展,旨在提供更强大的编程能力,包括面向对象编程和泛型编程的支持。C++支持数据封装、继承和多态等面向对象编程的特性和泛型编程的模板,以及丰富的标准库,提供了大量的数据结构和算法,极大地提高了开发效率。12 C++是一种静态类型的、编译式的、通用的、大小写敏感的编程语言,它综合了高级语言和低级语言的特点。C++的语法与C语言非常相似,但增加了许多面向对象编程的特性,如类、对象、封装、继承和多态等。这使得C++既保持了C语言的低级特性,如直接访问硬件的能力,又提供了高级语言的特性,如数据封装和代码重用。13 C++的应用领域非常广泛,包括但不限于教育、系统开发、游戏开发、嵌入式系统、工业和商业应用、科研和高性能计算等领域。在教育领域,C++因其结构化和面向对象的特性,常被选为计算机科学和工程专业的入门编程语言。在系统开发领域,C++因其高效性和灵活性,经常被作为开发语言。游戏开发领域中,C++由于其高效性和广泛应用,在开发高性能游戏和游戏引擎中扮演着重要角色。在嵌入式系统领域,C++的高效和灵活性使其成为理想选择。此外,C++还广泛应用于桌面应用、Web浏览器、操作系统、编译器、媒体应用程序、数据库引擎、医疗工程和机器人等领域。16 学习C++的关键是理解其核心概念和编程风格,而不是过于深入技术细节。C++支持多种编程风格,每种风格都能有效地保证运行时间效率和空间效率。因此,无论是初学者还是经验丰富的程序员,都可以通过C++来设计和实现新系统或维护旧系统。3

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值