POJ 3278Catch That Cow BFS

本文介绍了一种算法,用于解决农民John如何在最短时间内找到并捕捉到逃逸的奶牛的问题。奶牛位于同一数轴上的另一点,John有两种移动方式:步行和瞬移。通过详细的步骤解析和示例,展示了如何使用广度优先搜索算法(BFS)来找到从起点到目标点的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 - 1 or + 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.

Source

USACO 2007 Open Silver

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di



import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

class point {
	int x;
	int step;

	public point(int x, int step) {
		super();
		this.x = x;
		this.step = step;
	}

	public point() {
		super();
		step = 0;
	}
}

public class Main{
	static int n, k;
	static int re = 0;
	static int dir[] = { -1, 1, 0, 0 };
	static int vis[] = new int[9000000];

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		n = cin.nextInt();// 人只往右2*x
		k = cin.nextInt();// 牛
		for (int i = 0; i <= n; i++)
			vis[i] = 0;
		if (k <= n) {
			re = n - k;
		} else {
			point p = new point(n, 0);
			bfs(p);
		}
		System.out.println(re);
	}

	private static void bfs(point x) {
		// TODO Auto-generated method stub
		Queue<point> queue = new LinkedList<point>();
		point x1 = new point(), x2 = new point();
		queue.add(x);
		while (!queue.isEmpty()) {
			//
			x1 = queue.peek();
			//System.out.println(x1.x);
			//System.out.println(x1.x+"ddddddd");
			queue.poll();
			for (int i = 0; i < 3; i++) {

				x2.x = x1.x + dir[i];
				//System.out.println(dir[i]+"rrrrrrrrrrrr");
				//System.out.println(x2.x+"ddddddd");
				if(i==2)
				{
					x2.x=x1.x*2;
				}
			
				if (x2.x >= 0 && x2.x <= 1000000 && vis[x2.x] == 0) {
					vis[x2.x] = 1;
					x2.step = x1.step + 1;
					if (x2.x == k) {
						re = x2.step;
						//System.out.println("cccc");
						return;
					} else {
						//System.out.println(x2.x+"rrrrrrrrrr");
						point kk=new point(x2.x,x2.step);
						queue.add(kk);
						//System.out.println(x2.x+"rrrrrrrrrr1111");
					
					}
				}
			}

		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值