• 3060 抓住那头奶牛 USACO


    3060 抓住那头奶牛

    USACO

    时间限制: 1 s
    空间限制: 16000 KB
    题目等级 : 黄金 Gold
    题目描述 Description

    农夫约翰被告知一头逃跑奶牛的位置,想要立即抓住它,他开始在数轴的N 点(0≤N≤100000),奶牛在同一个数轴的K 点(0≤K≤100000)。约翰有两种移动方式:1 分钟内从x 点移动到x+1 或x-1;1 分钟内从x 点移动到2x。假设奶牛不会移动,约翰抓住它需要多少时间?

    输入描述 Input Description

    一行两个整数N 和K,用空格隔开。

    输出描述 Output Description

    约翰抓住它需要的最少时间。

    样例输入 Sample Input

    5 17

    样例输出 Sample Output

    4

    数据范围及提示 Data Size & Hint

    见题目

    思路:广搜。

     1 #include<iostream>
     2 using namespace std;
     3 #include<queue>
     4 struct node{
     5     int x,step;
     6 }cur,net;
     7 queue<node>s;
     8 bool num[100000];
     9 int n,m;
    10 void bfs()
    11 {
    12     cur.x=n;
    13     cur.step=0;
    14     s.push(cur);
    15     num[n]=1;
    16     while(!s.empty() )
    17     {
    18         cur=s.front() ;
    19         s.pop() ;
    20         int a=cur.x;
    21         if(a*2>0&&a*2<=100000&&!num[a*2]&&a<=m)
    22         {
    23             if(a*2==m)
    24             {
    25                 cout<<cur.step+1;
    26                 return ;                
    27             }
    28             net.x =a*2;
    29             net.step=cur.step +1;
    30             s.push(net);;
    31             num[a*2]=1; 
    32         }
    33         if(a+1>0&&a+1<=100000&&!num[a+1])
    34         {
    35             if(a+1==m)
    36             {
    37                 cout<<cur.step +1;
    38                 return ;                
    39             }
    40             net.x =a+2;
    41             net.step=cur.step +1;
    42             s.push(net);
    43             num[a+2]=1; 
    44         }
    45         if(a-1>0&&a-1<=100000&&!num[a-1])
    46         {
    47             if(a-1==m)
    48             {
    49                 cout<<cur.step +1;
    50                 return ;                
    51             }
    52             net.x =a-2;
    53             net.step=cur.step +1;
    54             s.push(net);;
    55             num[a-2]=1; 
    56         }
    57     }
    58 }
    59 int main()
    60 {
    61     cin>>n>>m;
    62     if(n>=m)
    63     {
    64         cout<<n-m;
    65         return 0;
    66     }
    67     bfs();
    68     return 0;
    69 }
  • 相关阅读:
    Feli的生日礼物
    session cookie
    ArcGIS FLEXnet Licensing error:96,491错误解决
    CSS块元素与内联元素(转)
    铝伯世
    netsh修改IP及DNS

    JAVA代码查错(转)
    windows7查看占用端口的进程
    php 中文字符串截取子串
  • 原文地址:https://www.cnblogs.com/mjtcn/p/6758561.html
Copyright © 2020-2023  润新知