文章
99
粉丝
120
获赞
8
访问
97.5k
#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...
登录后发布评论
暂无评论,来抢沙发