HOJ 12808 杀死提莫(BFS)

本文介绍了一个简单的BFS(广度优先搜索)算法实现案例。通过一个迷宫寻路问题,详细解释了如何使用队列存储状态,并逐步扩展节点来寻找从起点到终点的最短路径。文中还提供了一段AC代码作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BFS入门题,还是我第一次做比赛的题目,不过直到龙酱教了我,我才知道怎么写。


思路很简单,一个一维que数组存下所有走过格子(此处“走”的含义是,每次按步数扩展后“走”过的,也可以说是可以走的,真正路线只是其中的某一些,如果这里看不懂,没关系,看完后面的再看这里就懂了)。


用一个now记录当前位置,next扩展now的四个方向,即now的下一步。如果遇到限制条件不能走,跳过;如果能走,再看有没有走过。


如果走过,说明这个位置可以用更少的步数走到(因为每次扩展都是按步数扩展的),如果没走过,放入que中。这样一来,que数组中,同一步(最少步数)可到达的格子是在连一起的。


但now改怎么取呢?从que数组中取。用一个head(初始化为-1)和tail(初始化为0)做指针,每次now取que[head],检查是否为终点。


如果不是,记下step[head],向四个方向扩展,把每个合法的下一步(next_1,next_2,next_3,next_4(假如都存在))存入que,每存入一个,就把tail推后一位,然后step[next_i] = step[head] + 1,扩展完当前的head后,把head推后一位,重复之前的步骤,直到找到终点或head > tail。


不难理解,每次扩展都是按步数扩展,扩展出来的新位置丢到que的末尾;步数越小,在que中的位置越前面,这就是BFS的含义——广度优先搜索。


如果head大于tail是个什么情况呢?就是合法的扩展没有了(tail不能往后移),而head在一步步往后移,却又一直找不到终点,最后,搜完了que中的所有步骤也没能找到终点,说明所有的走法都无法走到终点。


AC代码:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; //四个方向

struct node
{
        int x, y;
} A, B, que[1001*1001]; //x代表行,y代表列

int n, m;
char map[1001][1002]; //存地图
bool flag[1001][1001]; //记录map[i][j]是否“走”过
int step[1001][1001]; //记录走到map[i][j]的最小步数
int i, j, k;

int bfs()
{
        int head = 0;
        int tail = 1;
        que[1] = A;
        flag[A.x][A.y] = true;

        while (head < tail)
        {
                node now = que[++head];

                if (now.x == B.x && now.y == B.y)
                        return step[now.x][now.y];
                node next;
                for (i = 0; i < 4; i++)
                {
                        next.x = now.x + dir[i][0];
                        next.y = now.y + dir[i][1];
                        if (next.x < 1 || next.y < 1 || next.x > n || next.y > m)
                        {
//                                printf("\n1: %c %d %d\n", map[next.x][next.y], next.x, next.y);
                                continue;
                        }
                        if (map[next.x][next.y] == 'X')
                        {
//                                printf("\n2: %c %d %d\n", map[next.x][next.y], next.x, next.y);
                                continue;
                        }
                        if (flag[next.x][next.y])
                        {
//                                printf("\n3: %c %d %d\n", map[next.x][next.y], next.x, next.y);
                                continue;
                        }
                        step[next.x][next.y] = step[now.x][now.y] + 1;
                        flag[next.x][next.y] = true;
                        que[++tail] = next;
//                        printf("\nstep %d: %c %d %d\n",  step[next.x][next.y], map[next.x][next.y], next.x, next.y);
                }
        }
//        printf("\nhead: %d tail: %d\n\n", head, tail);
        k = tail;
        return -1;
}

int main()
{
        #ifdef BellWind
                freopen("12808.in", "r", stdin);
        #endif // BellWind

        while (~scanf("%d%d", &n, &m))
        {
                memset(flag, 0, sizeof(flag));
                for (i = 1; i <= n; i++)
                {
                        map[i][0] = 'R';
                        scanf("%s", &map[i][1]);
                }

//                for (i = 1; i <= n; i++)
//                {
//                        for (j = 1; j <= m; j++)
//                                printf("%c", map[i][j]);
//                        printf("\n");
//                }

//                printf("\nn: %d m: %d\n\n", n, m);

                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= m; j++)
                        {
                                flag[i][j] = false;
                                step[i][j] = 0;
                                if (map[i][j] == 'A')
                                {
                                        A.x = i;
                                        A.y = j;
                                }
                                if (map[i][j] == 'B')
                                {
                                        B.x = i;
                                        B.y = j;
                                }
                        }
                }

                int ans = bfs();

//                for (i = 1; i <= k; i++) printf("*%d %d\n", que[i].x, que[i].y);
                printf("%d\n", ans);
        }

        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值