BFS搜索

  BFS搜索是一种基础的图遍历算法,在很多算法中都有体现。

  BFS搜索的核心思想:首先我们引入一个层的思想,来帮助大家理解。首先初始一个节点为起点,其即为0层。凡是由0层节点能一步到达的节点都为一层,凡是由一层节点能一步到达的节点都为二层,以此类推直至标记完所有的层。(以上为一种思想策略,在算法中并不需要具体实现)。  然后由0层开始遍历,若未发现目标,再去遍历一层,若未发现目标,在去遍历二层,以此类推,直至发现目标或者遍历完所有的层(节点)为止。

  话不多说 上题: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

    该题目理解起来很简单,但是做起来需要一点灵感,就是要先对地图做初始化处理。对于从来都不会被破坏的区域我们标记为-1;对于会被破坏的区域,我们用最早的破坏时间来标记它,这样就能很方便的判断,在当前时间下,该点到底是否可以通过。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
#define Max_n 305
int map1[Max_n][Max_n];
int move1[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int M;
int x, y, T;
int mintime ;
bool flag ;
 struct Point{
    int sx, sy, t;
    Point(int sx = 0, int sy = 0, int t = 0):sx(sx), sy(sy), t(t) {}
};  

void init ()  ///该函数即为初始化函数。
{
    memset(map1, -1, sizeof(map1));
    for (int i = 0; i < M; i ++) {
        scanf("%d%d%d", &x, &y, &T);
        if (map1[x][y] == -1) {
            map1[x][y] = T;
        }
        else map1[x][y] = min(map1[x][y], T);
        for (int i = 0; i < 4; i ++) {
            int nx = x + move1[i][0];
            int ny = y + move1[i][1];
            if (0 <= nx && ny >= 0)
            if (map1[nx][ny] == -1)
                map1[nx][ny] = T;
            else map1[nx][ny] = min(map1[nx][ny], T);
        }

    }
  /* for(int i = 0; i < 4; i ++) {
            printf("\n");
        for (int j = 0; j < 4; j ++)
            printf(" %d", map1[i][j]);
    }*/
}     
int bfs()                                    //BFS搜索一般来说需要用队列来实现。
{
    queue<Point> que;
    Point s,p;
    if (map1[0][0] == 0) return -1;//如果初始节点 刚开始就被毁灭就失败了。
    if (map1[0][0] == -1) return 0;//如果初始节点 刚开始就是永远安全直接成功。
    s.sx = s.sy = s.t = 0;
    que.push(s);        //首先将初始节点推入队列中。
    while (!que.empty()) {
        p = que.front();
        que.pop();     
        for (int i = 0; i < 4; i ++) {   // 用此循环来判断当前节点能到达的所有节点的情况。
            s.sx = p.sx + move1[i][0];
            s.sy = p.sy + move1[i][1];
            s.t  = p.t + 1;
            if (s.sx < 0 || s.sy < 0) continue;
            if (map1[s.sx][s.sy] == -1) return s.t;//如果直接就是目标节点,则退出。
            if (map1[s.sx][s.sy] > s.t) {           //否则就推入队列中。
                map1[s.sx][s.sy] = s.t;
                que.push(s);     
            }
        }
    }
    return -1;
}
int main()
{
   while (~scanf("%d", &M)) {
        init();
        printf("%d\n",bfs());

   }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值