题目描述
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?
算法思路
- 本题是求最小化问题,一般使用BFS搜索的方式。
- 首先,我们发现,向数轴的右边移动可以采用walking和teleporting两种方式。而向数轴的左边一动则一次只能一动1个单位长度。也就是说如果N>=K,那么最小时间就是N-K
- 我们只考虑N< K的情况。
- 如果农民移动到K右边的地方,则又回到第二点中我们提到的情况,此时加上判断即可。如果一直保持在K的左边,则到达终点的最长时间为K-locate(cur)+step(cur)也可以更新最优解,进行启发式剪枝。再加上一个数组记录搜索过的位置,本题即可得到解。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
int N,K;
bool visited[200005];
struct status{
int curx,step;
status(int a,int b){
curx = a;
step = b;
}
status(){}
};
void Solve()
{
if(N>=K){
//only one way to solve
printf("%d\n",N-K);
return;
}
memset(visited,false,sizeof(visited));
status st = status(N,0);
visited[N] = true;
status cur,next;
queue<status>Q;
Q.push(st);
while(!Q.empty()){
cur = Q.front();
Q.pop();
//printf("%d %d\n",cur.curx,cur.step);
if(cur.curx == K){
printf("%d\n",cur.step);
return;
}
next = status(cur.curx+1,cur.step+1);
if(!visited[cur.curx+1]){
Q.push(next);
visited[cur.curx+1] = true;
}
next = status(cur.curx-1,cur.step+1);
if(next.curx>=0 && !visited[cur.curx-1]){
Q.push(next);
visited[cur.curx-1]=true;
}
next = status(cur.curx*2,cur.step+1);
if((next.curx<=K || next.curx+next.step-K<=K-N) && !visited[next.curx]){
Q.push(next);
visited[next.curx] = true;
}
}
return;
}
int main()
{
//freopen("input","r",stdin);
while(scanf("%d%d",&N,&K)!=EOF){
Solve();
}
return 0;
}