Catch That Cow
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2717
解题思路:
简单的搜索题,直接搜就可以了。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100000;
int n,k;
int vis[N+10];
void bfs(){
queue<int> q;
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
q.push(n);
while(!q.empty()){
int cur = q.front();
q.pop();
if(cur == k)
return;
int next = cur-1;
if(next>=0 && !vis[next]){
vis[next] = vis[cur]+1;
q.push(next);
}
next = cur+1;
if(!vis[next]){
vis[next] = vis[cur]+1;
q.push(next);
}
next = 2*cur;
if(next<=N && (next-k < k-cur) && !vis[next]){
vis[next] = vis[cur]+1;
q.push(next);
}
}
}
int main(){
while(~scanf("%d%d",&n,&k)){
if(n >= k)
vis[k] = n-k;
else
bfs();
printf("%d\n",vis[k]);
}
return 0;
}