【PowerOJ】1209: Catch That Cow

题目链接: 1209: Catch That Cow
题目描述:

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

样例解释:

INPUT DETAILS:
Farmer John starts at point 5 and the fugitive cow is at point 17.
OUTPUT DETAILS:
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.

题意:给两个数n和k,再给三种操作,每种操作代价一样,求将n变为k的最少操作数。
思路:bfs题,利用队列来记录中间操作之后的结果,如果某个操作使得队列中的结果和k相等就直接退出,用一个temp中间变量来记录下标,这个下标就是变到k的中间值,最后我们使用ans数组记录操作的答案即可(每进行一次操作答案加一,直到到达目标值)。

Java代码:

class Main{
    private static final int maxn=100005;
    public void bfs(int n,int k,int ans[]){
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.offer(n);
        Integer temp;
        while(!queue.isEmpty()){
            Integer cur=queue.poll();
            if(cur.equals(k)) break;  //别用==比较时候注意Integer的缓存问题
            temp=cur+1;
            if(temp<maxn&&temp>=0&&ans[temp]==0){
                ans[temp]=ans[cur]+1;
                queue.offer(temp);
            }
            temp=cur-1;
            if(temp<maxn&&temp>=0&&ans[temp]==0){
                ans[temp]=ans[cur]+1;
                queue.offer(temp);
            }
            temp=cur*2;
            if(temp<maxn&&temp>=0&&ans[temp]==0){
                ans[temp]=ans[cur]+1;
                queue.offer(temp);
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            int n=cin.nextInt();
            int k=cin.nextInt();
            int ans[]=new int[maxn];
            Main main=new Main();
            main.bfs(n,k,ans);
            System.out.println(ans[k]);
        }
    }
}

C++代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5 ;
int ans[maxn];
int n,k;
void bfs()
{
    queue<int>q;
    q.push(n);
    int tem,cur;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur==k) break;
        tem=cur+1;
        if(tem>=0&&tem<maxn&&ans[tem]==0)
        {
            ans[tem]=ans[cur]+1;
            q.push(tem);
        }
        tem=cur-1;
        if(tem>=0&&tem<maxn&&ans[tem]==0)
        {
            ans[tem]=ans[cur]+1;
            q.push(tem);
        }
        tem=cur*2;
        if(tem>=0&&tem<maxn&&ans[tem]==0)
        {
            ans[tem]=ans[cur]+1;
            q.push(tem);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(ans,0,sizeof(ans));
        bfs();
        printf("%d\n",ans[k]);
    }
}

猜你喜欢

转载自blog.csdn.net/dl962454/article/details/115080874