很短的 BFS 队列
HDU_2717_Catch That Cow
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 8 int book[100005<<1]={0}; 9 int que[100005<<1]; 10 int n,k; 11 int main() 12 { 13 cin>>n>>k; 14 queue<int>q; 15 que[n]=0; 16 book[n]=1; 17 q.push(n); 18 while(!q.empty()) 19 { 20 int a=q.front(); 21 q.pop(); 22 if(a-1>=0 && !book[a-1]) 23 { 24 book[a-1]=1; 25 que[a-1]=que[a]+1; 26 q.push(a-1); 27 } 28 if(a+1<=2*k && !book[a+1]) 29 { 30 book[a+1]=1; 31 que[a+1]=que[a]+1; 32 q.push(a+1); 33 } 34 if(a*2<=2*k && !book[a*2]) 35 { 36 book[a*2]=1; 37 que[a*2]=que[a]+1; 38 q.push(a*2); 39 } 40 } 41 cout<<que[k]<<endl; 42 }