题目:
| Catch That Cow| Time Limit: 2000MS | | Memory Limit: 65536K |
| ---------------------------- | - | ------------------------ |
| Total Submissions: 80985 | | Accepted: 25495 |DescriptionFarmer 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?InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input5 17Sample Output```
4
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - | - |
\[[Submit](http://poj.org/submit?problem_id=3278)] \[Go Back] \[[Status](http://poj.org/problemstatus?problem_id=3278)] \[[Discuss](http://poj.org/bbs?problem_id=3278)]
代码:
```cpp
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int s[100002];
int vis[100002];
int bfs(int n,int k)
{
queue<int>q;
int now,to;//现在的和要走的
q.push(n);
s[n]=0;//把n的步数标记为0
vis[n]=1;//走过了标记为1
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0; i<3; i++)//枚举三种情况
{
if(i==0)to=now-1;
else if(i==1)to=now+1;
else to=now*2;
if(to<0||to>=100002)continue;//判断越界
if(vis[to]==0)
{
vis[to]=1;//走过了标记
q.push(to);//入队
s[to]=s[now]+1;//步数+1
}
if(to==k)return s[to];//当走到我们要找的数了返回
}
}
return 0;
}
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))//输入数据
{
mem(s,0);
mem(vis,0);
if(n>=k)
printf("%d\n",n-k);//如果农民在母牛后面那就只能每次-1
else
printf("%d\n",bfs(n,k));
}
return 0;
}
用了广搜,枚举三种情况,和走地图是一类题