首页 > 代码库 > bfs Catch That Cow
bfs Catch That Cow
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace std; #define maxn 100001 int steps=0; int book[maxn]={0}; queue<int>q; void bfs(int a,int b) { memset(book,0,sizeof(book)); book[a]=1; while(!q.empty()) q.pop(); q.push(a); while(!q.empty()) { int q_size=q.size(); while(q_size--) { int head=q.front(); q.pop(); if(head==b) return; int t=0; for(int i=1;i<=3;i++) { if(i==1) t=head-1; else if(i==2) t=head+1; else if(i==3) t=2*head; if(t<0||t>maxn) continue; if(!book[t]) { book[t]=1; q.push(t); } } } steps++; } } int main() { int n,k; scanf("%d%d",&n,&k); if(n>=k) printf("%d\n",n-k); else { bfs(n,k); printf("%d\n",steps); } return 0; }
bfs Catch That Cow
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。