Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 43517 | Accepted: 13549 |
Description
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
Hint
The 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.
/*****************************************************/
/**** POJ 3278 Catch That Cow ******/
/**** Wangguoliang @Greenday ******/
/**** 2014-5-12 ******/
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int visit[200001];//1为遍历过,0为未遍历
int n,m,flag;
struct node //定义结点
{
int pi;
int step;
};
queue<node>Q;//定义对列 Q
void BFS() //广度优先搜索
{
node p,q;
int i,j;
p.pi=n,p.step=0;
Q.push(p); //入队
while(!Q.empty())//判断是否队列已空
{
p=Q.front(); //返回队列第一个元素
Q.pop(); //函数删除队列的一个元素
if(p.pi==m) //找到终点,结束
{
flag=p.step;
return ;
}
p.step++; //未找到步加1
if(p.pi<m&&!visit[p.pi]) //直接乘以2
{
q=p;
q.pi*=2;
Q.push(q);
}
if(p.pi>0&&!visit[p.pi])//判断是否大于0,然后--
{
q=p;
q.pi--;
Q.push(q);
}
if(!visit[p.pi])//直接++,然后把这个点标记为遍历过
{
q=p;
q.pi++;
Q.push(q);
visit[p.pi]=1;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(visit,0,sizeof(visit));//去除标记清0
flag=0;
BFS();
printf("%d\n",flag);
//while(!Q.empty())//清空队列
Q.pop();
}
return 0;
}
/*
#include<iostream>
#include<queue>
using namespace std;
int a[100001],b[100001];
int main()
{
int m,n,y;
queue<int>x;//建立名为x的队列
cin>>m>>n;
x.push(m);//在队列后加入m
a[m]=0;
while(x.size()!=0)//队列大小不为0
{
y=x.front();//y取队列首位
x.pop();//去掉队列首位
b[y]=1;
if(y==n)
break;
if(y-1>=0&&b[y-1]==0)
{
x.push(y-1);
a[y-1]=a[y]+1;
b[y-1]=1;
}
if(y+1<=100000&&b[y+1]==0)
{
x.push(y+1);
a[y+1]=a[y]+1;
b[y+1]=1;
}
if(2*y<=100000&&b[2*y]==0)
{
x.push(2*y);
a[2*y]=a[y]+1;
b[2*y]=1;
}
}
cout<<a[y]<<endl;
return 0;
}
/*
//搜索的百度的不用stl 的算法
memory 980K time 16Ms
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstdio>
#include <climits>
using namespace std;
bool data[100005] = {0};
int que[100005] = {0};
int tnum[100005] = {0};
void bfs(int n, int k)
{
int front = 0, rear = 0;
que[0] = n;
data[n] = true;
tnum[0] = 0;
rear++;
while (front != rear)
{
int t = que[front];
int tn = tnum[front];
if (t == k)
{
printf("%d\n", tn);
return;
}
if (t > 0 && !data[t-1])
{
data[t-1] = true;
que[rear] = t-1;
tnum[rear] = tn+1;
rear++;
}
if (t < 100000 && !data[t+1])
{
data[t+1] = true;
que[rear] = t+1;
tnum[rear] = tn+1;
rear++;
}
if (t <= 50000 && !data[t*2])
{
data[t*2] = true;
que[rear] = t*2;
tnum[rear] = tn+1;
rear++;
}
front++;
}
printf("0\n");
}
int main()
{
int n, k;
scanf("%d%d", &n, &k);
memset(data, 0, sizeof(data));
bfs(n, k);
return 0;
}