//只有三种搜素情况 #include<iostream> #include<queue> #include<cstring> using namespace std; int map[100010]; int k,n; struct node { int x; int time; }; int check(int x) { if(x<0||x>100000||map[x]) return 0; return 1; } int bfs(int x) { queue<node>q; node a,b; a.x=x; a.time=0; map[x]=1; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.x==k) return a.time; b=a; b.x=a.x+1; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } b.x=a.x-1; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } b.x=2*a.x; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } } return -1; } int main() { int ans; while(cin>>n>>k) { memset(map,0,sizeof(map)); ans=bfs(n); cout<<ans<<endl; } return 0; }
HDU-2717-Catch That Cow
最新推荐文章于 2020-01-27 10:07:07 发布