农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点 N(0≤N≤105),牛位于点 K(0≤K≤105)。农夫有两种移动方式:
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?
#include <iostream>
#include <queue>
using namespace std;
int n, k, dis[100010];
bool vis[100010];
int bfs()
{
queue<int> q;
q.push(n);
vis[n] = true;
while (!q.empty())
{
int x = q.front();
if (x == k) return dis[x];
q.pop();
int y = x - 1;
if (!vis[y])
{
q.push(y);
vis[y] = true;
dis[y] = dis[x] + 1;
}
y = x + 1;
if (!vis[y])
{
q.push(y);
vis[y] = true;
dis[y] = dis[x] + 1;
}
y = 2 * x;
if (!vis[y])
{
q.push(y);
vis[y] = true;
dis[y] = dis[x] + 1;
}
}
}
int main()
{
int n, k;
cin >> n >> k;
cout << bfs() << endl;
return 0;
}
请问我哪里错了???