题目:C - Catch That Cow
题意分析:
思路:
BFS + 单调队列
注意农夫瞬移时超过最大也应该剪掉
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = (int)1e5 + 10;
int n, k;
int vis[maxn];
struct node{
int x, t;
friend bool operator < (node a, node b){
return a.t > b.t;
}
};
void BFS(int x1, int x2){
memset(vis, 0, sizeof(vis));
priority_queue <node> que;
node e1, e2;
e1.x = x1; e1.t = 0;
vis[e1.x] = 1;
que.push(e1);
while (!que.empty()){
e1 = que.top();
que.pop();
if (e1.x == x2) break;
else
{
if(e1.x-1>0&&vis[e1.x-1]==0)
{
e2.x=e1.x-1;
e2.t=e1.t+1;
vis[e2.x]=1;
que.push(e2);
}
if(e1.x+1<=100000&&vis[e1.x+1]==0)
{
e2.x=e1.x+1;
e2.t=e1.t+1;
vis[e2.x]=1;
que.push(e2);
}
if(e1.x*2<=100000&&vis[e1.x*2]==0)
{
e2.x=e1.x*2;
e2.t=e1.t+1;
vis[e2.x]=1;
que.push(e2);
}
}
}
cout<<e1.t<<endl;
}
int main()
{
while(cin>>n>>k){
if (n > k) cout<<n-k<<endl;
else
BFS(n, k);
}
return 0;
}