BFS入门刷题

目录

[P1746 离开中山路](#P1746 离开中山路)

[P1443 马的遍历](#P1443 马的遍历)

[P1747 好奇怪的游戏](#P1747 好奇怪的游戏)

[P2385 [USACO07FEB] Bronze Lilypad Pond B](#P2385 [USACO07FEB] Bronze Lilypad Pond B)


P1746 离开中山路

cpp 复制代码
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n;
int startx, starty;
int endx, endy;
char a[1010][1010];
int dis[1010][1010];
queue<pair<int, int>> q;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int bfs(int x, int y)
{
    memset(dis, -1, sizeof dis);
    dis[x][y] = 0;
    q.push({x, y});
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= n && ny <= n && a[nx][ny] != '1' && dis[nx][ny] <= 0)
            {
                q.push({nx, ny});
                dis[nx][ny] = dis[t.first][t.second] + 1;
            }
        }
    }
    return dis[endx][endy];
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
        }
    }
    cin >> startx >> starty >> endx >> endy;
    cout << bfs(startx, starty);
    return 0;
}

P1443 马的遍历

2个样例TLE:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
queue<pair<int, int>> q;
int startx, starty;
int bfs(int x, int y)
{
    if (x == startx && y == starty)
    {
        return 0;
    }
    memset(dis, -1, sizeof dis);
    q.push({startx, starty});
    dis[startx][starty] = 0;
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 8; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] <= 0)
            {
                q.push({nx, ny});
                dis[nx][ny] = dis[t.first][t.second] + 1;
            }
        }
    }
    return dis[x][y];
}
int main()
{
    cin >> n >> m >> startx >> starty;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << bfs(i, j) << " ";
        }
        cout << endl;
    }
    return 0;
}

作者一开始想的是每个点都要计数,所以每个点都要搜一次,然后返回一个值

其实只要搜一次

用void类型,不用返回,从开始搜到结尾,然后每个点都会一层一层地搜到,最后dis数组里存的就是每个点的计数

优化:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
queue<pair<int, int>> q;
int startx, starty;
void bfs(int x, int y)
{
    memset(dis, -1, sizeof dis);
    q.push({startx, starty});
    dis[startx][starty] = 0;
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 8; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] <= 0)
            {
                q.push({nx, ny});
                dis[nx][ny] = dis[t.first][t.second] + 1;
            }
        }
    }
}
int main()
{
    cin >> n >> m >> startx >> starty;
    bfs(startx, starty);
    dis[startx][starty] = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << dis[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

调用STL中的queue调用坐标需要花费比较长的时间,我们可以用数组来模拟queue节省时间

再优化:

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
pair<int, int> q[410 * 410];
int startx, starty;
void bfs(int x, int y)
{
    memset(dis, -1, sizeof dis);
    int head = 0;
    int tail = 0;
    q[0] = {x, y};
    dis[x][y] = 0;
    while (head <= tail)
    {
        auto t = q[head++];
        for (int i = 0; i < 8; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] <= 0)
            {
                q[++tail] = {nx, ny};
                dis[nx][ny] = dis[t.first][t.second] + 1;
            }
        }
    }
}
int main()
{
    cin >> n >> m >> startx >> starty;
    bfs(startx, starty);
    dis[startx][starty] = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << dis[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

好像看不出什么区别

P1747 好奇怪的游戏

cpp 复制代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int x1, y1;
int x2, y2;
int dis[30][30];
queue<pair<int, int>> q;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1, 2, 2, -2, -2};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2, 2, -2, -2, 2};
int bfs(int x, int y)
{
    memset(dis, -1, sizeof dis);
    q.push({x, y});
    dis[x][y] = 0;
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 12; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= 20 && ny <= 20 && dis[nx][ny] <= 0)
            {
                q.push({nx, ny});
                dis[nx][ny] = dis[t.first][t.second] + 1;
            }
        }
    }
    return dis[1][1];
}
int main()
{
    cin >> x1 >> y1 >> x2 >> y2;
    cout << bfs(x1, y1) << endl;
    cout << bfs(x2, y2) << endl;
    return 0;
}

P2385 [USACO07FEB] Bronze Lilypad Pond B

cpp 复制代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
queue<pair<int, int>> q;
int m, n;
int m1, n1;
int a[35][35];
int dis[35][35];
int startx, starty, endx, endy;
int bfs(int x, int y)
{
    memset(dis, -1, sizeof dis);
    q.push({x, y});
    dis[x][y] = 0;
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        int dx[] = {m1, m1, -m1, -m1, n1, n1, -n1, -n1};
        int dy[] = {n1, -n1, n1, -n1, m1, -m1, m1, -m1};
        for (int i = 0; i < 8; i++)
        {
            int nx = t.first + dx[i];
            int ny = t.second + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= m && ny <= n && a[nx][ny] != 0 && a[nx][ny] != 2 && dis[nx][ny] == -1)
            {
                dis[nx][ny] = dis[t.first][t.second] + 1;
                q.push({nx, ny});
            }
        }
    }
    return dis[endx][endy];
}
int main()
{
    cin >> m >> n >> m1 >> n1;
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 3)
            {
                startx = i;
                starty = j;
            }
            if (a[i][j] == 4)
            {
                endx = i;
                endy = j;
            }
        }
    }
    cout << bfs(startx, starty);
    return 0;
}
相关推荐
闪电麦坤9521 分钟前
数据结构:递归:自然数之和
数据结构·算法
t1987512830 分钟前
matlab实现求解兰伯特问题
开发语言·算法·matlab
red润41 分钟前
奇怪?为什么 floor((n + t - 1) / t) 比 ceil(n / t) 更高效?(因为没有浮点转换带来的性能损耗)
前端·后端·算法
纪元A梦1 小时前
分布式拜占庭容错算法——权益证明(PoS)算法详解
java·分布式·算法
GalaxyPokemon1 小时前
LeetCode - 144. 二叉树的前序遍历
算法·leetcode·职场和发展
帮关下月亮2 小时前
Python 训练营打卡 Day 33-神经网络
python·神经网络·算法
合合技术团队2 小时前
TextIn OCR Frontend前端开源组件库发布!
大数据·人工智能·算法
zstar-_2 小时前
【Ragflow】25.Ragflow-plus开发日志:excel文件解析新思路/公式解析适配
人工智能·算法·excel
Jasmine_llq3 小时前
《CF912E Prime Gift》
算法·优先队列(最小堆)·集合去重
Dovis(誓平步青云)6 小时前
C++ Vector算法精讲与底层探秘:从经典例题到性能优化全解析
开发语言·c++·经验分享·笔记·算法