标签:
5 17
4HintThe fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes. 睡前一水~ 好久没做过搜索了 注意剪枝&&&&&&&&#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<stack> #include<iostream> using namespace std; struct Node { int x,step; Node(){} Node(int x,int step):x(x),step(step){} }; int n,k; bool vis[199999]; int bfs() { queue<Node>Q; Node cur,next; Q.push(Node(n,0)); while(!Q.empty()) { cur=Q.front(); Q.pop(); for(int i=0;i<3;i++) { if(i==0) next.x=cur.x+1; if(i==1) next.x=cur.x-1; if(i==2) next.x=cur.x*2; next.step=cur.step+1; if(next.x==k) return next.step; if(next.x<0||next.x>100000) continue; if(!vis[next.x]) { vis[next.x]=true; Q.push(next); } } } } int main() { while(cin>>n>>k) { memset(vis,0,sizeof(vis)); if(n<k) cout<<bfs()<<endl; if(n==k) cout<<0<<endl; if(n>k) cout<<n-k<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lsgqjh/article/details/47116031