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;
}
相关推荐
hie988944 分钟前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼15 分钟前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield1 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦1 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl1 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
small_wh1te_coder1 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
挺菜的2 小时前
【算法刷题记录(简单题)002】字符串字符匹配(java代码实现)
java·开发语言·算法
凌肖战5 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
88号技师6 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
ゞ 正在缓冲99%…6 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划