BFS应用:POJ3278 抓牛
作者:互联网
题目描述:
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?
输入:Line 1: Two space-separated integers: N and K
输出:Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
输入样例:
5 17
输出样例:
4
解题思路:
农民有3种移动方式,但如果想向后移动就只有一种移动方式,也就是说明如果农民的位置在牛的位置之前或相同(N>=K)农民就只能采用一步步回退的方式;
如果牛在农民前面则3种移动方式都能用的上,想求出移动的步数可以BFS的方式逐层扩散,直到遍历到牛的位置,遍历的层数就是最短的路径。
解题步骤:
1、若N>=K,直接输出N-K;
2、若N<K,用BFS,定义一个步数数组step,下标为结点的值,内容为该结点到N的步数;
3、设出发顶点为N,step[N]为0,将其入队后开始遍历;若队头元素为u,三个邻接点为u+1、u-1、2*u,且邻接点的步数比结点的步数+1。
4、有队头元素 = k时,输出此元素的步数。
代码实现:
#include <iostream>
using namespace std;
#include <queue>
#define maxn 100001
int n, k;
bool vis[maxn];
int step[maxn];
void BFS()
{
int u = 0, v = 0;
step[n] = 0;
queue<int> q;
q.push(n);
vis[n] = true;
while (!q.empty())
{
u = q.front();
q.pop();
if (u == k)
{
cout << step[u] << endl;
return;
}
v = u - 1;
if (v >= 0 && v <= 100000 && !vis[v])
{
step[v] = step[u] + 1;
vis[v] = true;
q.push(v);
}
v = u + 1;
if (v >= 0 && v <= 100000 && !vis[v])
{
step[v] = step[u] + 1;
vis[v] = true;
q.push(v);
}
v = 2 * u;
if(v >= 0 && v <= 100000 && !vis[v])
{
step[v] = step[u] + 1;
vis[v] = true;
q.push(v);
}
}
}
int main()
{
cin >> n >> k;
if (n >= k)
{
cout << n - k << endl;
}
else
{
BFS();
}
return 0;
}
标签:抓牛,cow,point,POJ3278,move,BFS,Farmer,John,步数 来源: https://blog.csdn.net/qq_48988844/article/details/120350458