文章

99

粉丝

120

获赞

8

访问

75.1k

头像
走路还是坐公交
备考心情
发布于2024年8月11日 10:44
阅读数 754

#include <iostream>
#include <algorithm>
#include <stack>
#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std;

int vis[200005];
struct node {
    int step;
    int x;
    node(int a, int b) {
        x = a;
        step = b;
    }
};
int bfs(int s, int e) {
    queue <node> q;
    int ans = 1e9;
    q.push(node{ s,0 });
    vis[s] = 1;
    while (!q.empty()) {
        node tt = q.front();
        q.pop();
        if (tt.x == e) {
            return tt.step;
        }
        else {
            int xx = tt.x + 1;
            if (xx < 2 * e && xx>0 && !vis[xx]) {
                q.push(node{ xx,tt.step + 1 });
                vis[xx] = 1;
            }
            xx = tt.x - 1;
            if (xx < 2 * e && xx>0 && !vis[xx]) {
                q.push(node{ xx,tt.step + 1 });
                vis[xx] = 1;
            }
            xx = t...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发