Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25693 Accepted Submission(s): 7183
Problem 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?
* 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. 题解:
抓住那头牛,农夫和牛在一个数轴上,但是有三种移动方式,就可以把这三种移动方式,当作方向。
就相当于是一维的bfs,拯救天使是二维的,下个题是三维的。
emmmm
画个图,理解一下bfs.,小菜刚开始在csdn上搜题解,一直纳闷为啥不用比较就是最小的😓😓
如果不是真菜,谁愿意混吃混喝等死呢
如图:绿色表示搜索到每一步的步数,
圈圈里是搜索到的数字,也就是农夫的当前位置,
然后灰色的圈表示它已经被标记过,不再继续搜索,
同一颜色的圈圈的数字的步数是一样的
也就是说,将位置5 用三个方式搜索一下,从位置5走到位置4,6,10的步数为1,然后再走位置10相邻的三个位置,就是1+1=2.
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊,菜是原罪。
代码:
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=100001;
bool vis[maxn]; int step[maxn]; queue <int> q;
int bfs(int n,int k)
{
int first,next;
q.push(n);
step[n]=0;
vis[n]=1;
while(!q.empty())
{
first=q.front();
q.pop();
for(int i=0;i<3;i++)
{
if(i==0) next=first-1;
else if(i==1) next=first+1;
else next=first*2;
if(next<0||next>=maxn) continue;
if(vis[next]==0)
{
q.push(next);
step[next]=step[first]+1;
vis[next]=1;
}
if(next==k) return step[next];
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
if(n>=k) cout<<n-k<<endl;
else cout<<bfs(n,k)<<endl;
}
return 0;
#include <algorithm>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=100001;
bool vis[maxn]; int step[maxn]; queue <int> q;
int bfs(int n,int k)
{
int first,next;
q.push(n);
step[n]=0;
vis[n]=1;
while(!q.empty())
{
first=q.front();
q.pop();
for(int i=0;i<3;i++)
{
if(i==0) next=first-1;
else if(i==1) next=first+1;
else next=first*2;
if(next<0||next>=maxn) continue;
if(vis[next]==0)
{
q.push(next);
step[next]=step[first]+1;
vis[next]=1;
}
if(next==k) return step[next];
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
if(n>=k) cout<<n-k<<endl;
else cout<<bfs(n,k)<<endl;
}
return 0;
}