我是靠谱客的博主 香蕉雪糕,最近开发中收集的这篇文章主要介绍C - Catch That Cow,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本题是bfs的应用,将所有的满足情况置入队列之中,然后再判断是否到达终点(满足条件)就行,注意本题不能使用stack来储存,只能使用queue

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
int n,k,cnt;
const int maxn=200000+100;
int vis[maxn];
struct node{
int x,cnt;
};
void bfs()
{
queue<node> s;
while(!s.empty())
s.pop();
node r;
r.x=n,r.cnt=0;
vis[n]=1;
s.push(r);
while(!s.empty())
{
node rt=s.front();s.pop();
cnt=rt.cnt;
//cout<<cnt<<endl;
int xx=rt.x;
if(xx==k)
{
printf("%dn",cnt);continue;
}
if(xx<=k&&!vis[xx+1])
{
vis[xx+1]=1;
node rhs;
rhs.x=xx+1,rhs.cnt=cnt+1;
s.push(rhs);
}
if(xx>=1&&!vis[xx-1])
{
vis[xx-1]=1;
node rhs;
rhs.x=xx-1,rhs.cnt=cnt+1;
s.push(rhs);
}
if(xx<=k&&!vis[2*xx])
{
vis[2*xx]=1;
node rhs;
rhs.x=2*xx,rhs.cnt=cnt+1;
s.push(rhs);
}
}
}
int main()
{
//freopen("C.txt","r",stdin);
scanf("%d %d",&n,&k);
//cout<<n<<k<<endl;
memset(vis,0,sizeof(vis));
bfs();
return 0;
}


最后

以上就是香蕉雪糕为你收集整理的C - Catch That Cow的全部内容,希望文章能够帮你解决C - Catch That Cow所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部