您的位置:首页 > 其它

POJ 3278 Catch That Cow

2014-08-29 21:07 295 查看

Catch That Cow

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3278
64-bit integer IO format: %lld Java class name: Main

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Source

USACO 2007 Open Silver

解题:搜。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
struct node{
int p,step;
node(int x = 0,int y = 0):p(x),step(y){}
};
int n,k;
bool vis[100100];
queue<node>q;
int bfs(){
while(!q.empty()) q.pop();
memset(vis,false,sizeof(vis));
vis
= true;
q.push(node(n,0));
int tmp;
while(!q.empty()){
node now = q.front();
q.pop();
if(now.p == k) return now.step;
tmp = now.p+1;
if(tmp <= 100000 && !vis[tmp]){
vis[tmp] = true;
q.push(node(tmp,now.step+1));
}
tmp = now.p-1;
if(tmp >= 0 && !vis[tmp]){
vis[tmp] = true;
q.push(node(tmp,now.step+1));
}
tmp = now.p<<1;
if(tmp <= 100000 && !vis[tmp]){
vis[tmp] = true;
q.push(node(tmp,now.step+1));
}
}
return -1;
}
int main() {
while(~scanf("%d %d",&n,&k))
printf("%d\n",bfs());
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: