zoukankan      html  css  js  c++  java
  • poj 3278 Catch That Cow

    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.
     
     
    不解释 超简单BFS 思想,值得注意的是有可能输入的N和K相等,此时应该输出0
    其他没什么问题了
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 using namespace std;
     6 struct node
     7 {
     8     int x,time;
     9 };
    10 int N,K;
    11 int flag[100005];
    12 bool judge(node p)
    13 {
    14     if(p.x<0 || p.x>100000 || p.time>flag[p.x])
    15     {
    16         return false;
    17     }
    18     return true;
    19 }
    20 void bfs()
    21 {
    22     node p,temp;
    23     int i;
    24     flag[N]=0;
    25     p.x=N;
    26     p.time=0;
    27     queue<node>q;
    28     while(!q.empty())
    29     {
    30         q.pop();
    31     }
    32     q.push(p);
    33     while(!q.empty())
    34     {
    35         p=q.front();
    36         q.pop();
    37         for(i=0;i<3;i++)
    38         {
    39             temp=p;
    40             if(i==0)
    41             {
    42                 temp.x+=1;
    43             }
    44             if(i==1)
    45             {
    46                 temp.x-=1;
    47             }
    48             if(i==2)
    49             {
    50                 temp.x*=2;
    51             }
    52             temp.time=p.time+1;
    53             if(!judge(temp))
    54             {
    55                 continue;
    56             }
    57             if(temp.x==K)
    58             {
    59                 printf("%d\n",temp.time);
    60                 return;
    61             }
    62             flag[temp.x]=temp.time;
    63             q.push(temp);
    64         }
    65     }
    66 }
    67 int main()
    68 {
    69     while(~scanf("%d%d",&N,&K))
    70     {
    71         if(N==K)
    72         {
    73             printf("0\n");
    74             continue;
    75         }
    76         memset(flag,0x7F,sizeof(flag));
    77         bfs();
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    Cookie、Session详解
    阿里云高速maven库
    java23种设计模式详解
    分布式和集群的区别
    2016 年 Java 优秀文章
    java任务调度
    解酒
    中医教你如何调理女性内分泌失调
    Oracle RedoLog-二进制格式分析,文件头,DML,DDL
    Oracle RedoLog-基本概念和组成
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3109392.html
Copyright © 2011-2022 走看看