【Luogu1588】丢失的牛
【摘要】
problem
solution
codes
//BFS+记忆化搜索
#include<iostream>
#include<queue>
#include<cstri...
problem
solution
codes
//BFS+记忆化搜索
#include<iostream>
#include<queue>
#include<cstring>
#define maxn 100000
using namespace std;
int f[maxn],ans;
int a, b;
void bfs(){
memset(f,-1,sizeof f);
queue<int>q; q.push(a); f[a]=0;
while(q.size()){
int x = q.front(),y; q.pop();
if(x==b){ ans = f[b]; break; }
y = 2*x;
if(y>=0&&y<maxn&&(f[y]==-1||f[y]>f[x]+1)){ f[y]=f[x]+1; q.push(y); }
y = x-1;
if(y>=0&&y<maxn&&(f[y]==-1||f[y]>f[x]+1)){ f[y]=f[x]+1; q.push(y); }
y = x+1;
if(y>=0&&y<maxn&&(f[y]==-1||f[y]>f[x]+1)){ f[y]=f[x]+1; q.push(y); }
}
}
int main(){
int t; cin>>t;
while(t--){
cin>>a>>b;
bfs();
cout<<ans<<"\n";
}
return 0;
}
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/80601066
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)