迷宫&&长草&&字典序迷宫——蓝桥杯例题(bfs由浅入深)

目录

1.bfs(迷宫/地图)广度优先搜索思维导图

2.bfs宽度搜索思想(队列实现)

3.主旨展现

4.例题(1)来喽——走迷宫 +(路径打印)—两种方法

5.例题(2)来喽——长草(20年蓝桥杯模拟)—两种思路

6.例题(3)来喽——字典序迷宫(19年蓝桥杯)——有坑,小心


1.bfs(迷宫/地图)广度优先搜索思维导图

此图来自AC中的Hasity作者,万分感谢;

2.bfs宽度搜索思想(队列实现)

  • bfs是一种"盲目的"搜索技术(俗称"无向图"),它在搜索中并不理会目标在哪里,只顾自己乱走,当然最后会走到终点然后结束,从而在所有路径中找到最短的一条路径;
  • 宽搜是谁先到达终点谁就是最短路径,和深搜(dfs)不一样,深搜可以找到最终答案但不一定是最短路径;
  • 因为宽搜的时候需要一层一层搜索,先进来的点先搜索,后进来的点后搜索,和队列的特征很符合,因此一般用队列进行解决;

3.主旨展现

  • 用g[n][m],f[n][m]记录地图,用d[n][m]存储起点到n,m的距离;

  • 从起点开始进行优先遍历地图;

  • 当地图遍历完,就求出了起点到各个点的距离,输出d[n][m]即可。

4.例题(1)来喽——走迷宫 +(路径打印)—两种方法

题目描述

给定一个 n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 0 或 1,其中 0 表示可以走的路,1 表示不可通过的墙壁。

最初,有一个人位于左上角 (1,1)(1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。

请问,该人从左上角移动至右下角 (n,m)(n,m) 处,至少需要移动多少次。

数据保证 (1,1)(1,1) 处和 (n,m)(n,m) 处的数字为 0,且一定至少存在一条通路。

输入格式

第一行包含两个整数 n 和 m。

接下来 n 行,每行包含 m 个整数(0 或 1),表示完整的二维数组迷宫。

输出格式

输出一个整数,表示从左上角移动至右下角的最少移动次数。

数据范围

1≤n,m≤100

输入样例

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出样例

8

方法一:用stl中的队列实现+路径输出

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;
typedef pair<int, int>PII;

const int N = 110;
int n, m, a[N][N], d[N][N];
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };

int bfs()
{
	queue<PII>q;
	PII pre[N][N];//记录路径
	q.push({ 0,0 });
	memset(d, -1, sizeof(d));
	d[0][0] = 0;
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = t.first + dx[i], y = t.second + dy[i];
			if (x >= 0 && y >= 0 && x < n && y < m && a[x][y] == 0 && d[x][y] == -1)
			{
				q.push({ x,y });
				pre[x][y] = t;
				d[x][y] = d[t.first][t.second] + 1;
			}
		}
	}
	/*int x = n - 1, y = m - 1,k=0,l[N],r[N];//按顺序打印输出路径
	while (x || y)
	{
		l[k]=x,r[k]=y;k++;
		auto t = pre[x][y];
		x = t.first, y = t.second;
	}
	while(k--)
	{
		cout<<l[k]<<" "<<r[k]<<endl;
	}*/
	return d[n - 1][m - 1];
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> a[i][j];
		}
	}
	cout << bfs() << endl;
	return 0;
}

方法二:手动模拟队列+路径输出

#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;
typedef pair<int, int> PII;

const int N = 110;

int n, m;
int g[N][N], d[N][N];
int c[N], f[N], l, k;//c[N]、d[N]目的是储存逆向路径进而输出正向路径
PII q[N * N], Prev[N][N];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };

int bfs()//手动模拟数组
{
    int tt = 0, hh = 0;
    q[0] = { 0,0 };

    memset(d, -1, sizeof d);
    d[0][0] = 0;

    while (tt >= hh)
    {
        PII t = q[hh++];
        for (int i = 0; i < 4; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];

            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
            {
                d[x][y] = d[t.first][t.second] + 1;
                Prev[x][y] = t;//记忆队列中这个数
                q[++tt] = { x,y };

            }
        }
    }
    int x = n - 1, y = m - 1;
    /*while (x || y)
    {
        //cout<<x<<" "<<y<<endl;逆向输出路径
        c[l++] = x; f[k++] = y;//储存逆向路径进而输出正向路径
        PII t = Prev[x][y];
        x = t.first, y = t.second;
    }
    while (k--)//而输出正向路径
    {
        cout << c[k] << " " << f[k] << endl;
    }*/
    return d[n - 1][m - 1];
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> g[i][j];

    cout << bfs() << endl;
    return 0;
}

5.例题(2)来喽——长草(20年蓝桥杯模拟)—两种思路

题目描述

小明有一块空地,他将这块空地划分为 nn 行 mm 列的小块,每行和每列的长度都为 1。

小明选了其中的一些小块空地,种上了草,其他小块仍然保持是空地。

这些草长得很快,每个月,草都会向外长出一些,如果一个小块种了草,则它将向自己的上、下、左、右四小块空地扩展,

这四小块空地都将变为有草的小块。请告诉小明,kk 个月后空地上哪些地方有草。

输入格式

输入的第一行包含两个整数 n, m。

接下来 n 行,每行包含 m 个字母,表示初始的空地状态,字母之间没有空格。

如果为小数点,表示为空地,如果字母为 g,表示种了草。

接下来包含一个整数 k。 其中2≤n,m≤1000,0≤k≤1000。

输出格式

输出 n 行,每行包含 m 个字母,表示 k 个月后空地的状态。如果为小数点,表示为空地,如果字母为 g,表示长了草。

输出样例

4 5
.g...
.....
..g..
.....
2

输出样例

gggg.
gggg.
ggggg
.ggg.

  方法一:用距离来表示天数

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;
typedef pair<int, int>PII;
queue<PII>q;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
const int N = 1e3 + 10;
int n, m, k, d[N][N];
char g[N][N];

void bfs()
{
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        if (d[t.first][t.second] == k) return;

        for (int i = 0; i < 4; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];
            if (x >= 0 && y >= 0 && x < n && y < m && g[x][y] == '.')
            {
                g[x][y] = 'g';
                q.push({ x,y });
                d[x][y] = d[t.first][t.second] + 1;
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> g[i][j];
            if (g[i][j] == 'g') q.push({ i,j });
        }
    }
    cin >> k;
    bfs();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << g[i][j];
        }
        cout << endl;
    }
    return 0;
}

  方法二:重新定义pair

#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

const int N = 1111;

int n, m, k;
char a[N][N];
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };

struct node//定义横纵坐标和天数
{
    int x, y, d;//都是队列中的元素
};

void bfs()
{
    queue<node>q;
    while (q.size())
    {
        node t = q.front();
        q.pop();

        if (t.d == k) return;//长到k天结束

        for (int i = 0; i < 4; i++)
        {
            int tx = t.x + dx[i]; int ty = t.y + dy[i];
            if (tx<1 || ty<1 || tx>n || ty>m || a[tx][ty] == 'g') continue;
            if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && a[tx][ty] == '.')
            {
                a[tx][ty] = 'g';
                q.push({ tx,ty,t.d + 1 });
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 'g') q.push({ i,j,0 });
        }
    }
    cin >> k;
    bfs();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << a[i][j];
        }
        printf("\n");
    }
    return 0;
}

6.例题(3)来喽——字典序迷宫(19年蓝桥杯)——有坑,小心

题目描述

下图给出了一个迷宫的平面图,其中标记为 1的为障碍,标记为 0 的为可以通行的地方。
010000
000100 
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。

对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。

其中D、U、L、R 分别表示向下、向上、向左、向右走。 

对于下面这个更复杂的迷宫(30行 50列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,

请找出字典序最小的一个作为答案。

请注意在字典序中 D<L<R<U。

输入样例

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

输出样例

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

const int N = 1e2 + 10;
int n = 30, m = 50, d[N][N];
char g[N][N];
int dx[4] = { 1,0,0,-1 }, dy[4] = { 0,-1,1,0 };
char dir[4] = { 'D','L','R','U' };

struct PII {
    int first, second;
    string s;
};

void bfs()
{
    queue<PII>q;
    q.push({ 0,0 });
    d[0][0] = 1;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        if (t.first == n - 1 && t.second == m - 1)
        {
            cout << t.s << endl;
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];
            if (x >= 0 && y >= 0 && x < n && y < m && d[x][y] == 0 && g[x][y] == '0')//喵的,就是这卡好半天——单引号,唉!!
            {
                d[x][y] = 1;
                q.push({ x,y,t.s + dir[i] });
            }
        }
    }
}

int main()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> g[i][j];
        }
    }
    bfs();
}

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大小胖虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值