poj3278,cow

题目链接

Catch That Cow

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 120702 Accepted: 37665
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?

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效率较高、
搜索入门题(
ac代码


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

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Queue<Integer> d = new LinkedList<Integer>();
        boolean bo[] = new boolean[100001];
        int n = sc.nextInt();
        int k = sc.nextInt();
        d.offer(n);
        int count = 0;
        int num = 1;
        if (k <= n) &#123;
            System.out.println(n - k);
            System.exit(0);
        &#125;
        while (!d.isEmpty()) &#123;
            int ad = 0;
            count++;
            for (int i = 0; i < num; i++) &#123;
                int stp = d.poll();
                int next = stp - 1;
                if (next >= 0 && !bo[next]) &#123;
                    bo[next] = true;
                    d.offer(next);
                    ad++;
                    if (next == k) &#123;
                        System.out.println(count);
                        System.exit(0);
                    &#125;
                &#125;

                next = stp + 1;
                if (next < 100001 && !bo[next]) &#123;
                    bo[next] = true;
                    d.offer(next);
                    ad++;
                    if (next == k) &#123;
                        System.out.println(count);
                        System.exit(0);
                    &#125;
                &#125;

                next = stp << 1;
                if (next < 100001 && !bo[next]) &#123;

                    bo[next] = true;
                    d.offer(next);
                    ad++;
                    if (next == k) &#123;
                        System.out.println(count);
                        System.exit(0);
                    &#125;
                &#125;
            &#125;

            num = ad;
        &#125;

    &#125;

&#125;


   转载规则


《poj3278,cow》 xfx 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
矩阵乘法最优序列问题 矩阵乘法最优序列问题
该问题是给定一系列矩阵求一个最少乘法次数。这是一个动态规划问题,状态转移方程 long thisCost = m[left][i] + m[i+1][right]+c[left-1]*c[i]*c[right]; 进行求解 /
2018-11-14
下一篇 
牛客网小白月赛6D 字符串丝带 牛客网小白月赛6D 字符串丝带
链接:https://www.nowcoder.com/acm/contest/136/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %l
2018-09-17
  目录