zoukankan      html  css  js  c++  java
  • POJ-3278(BFS)

    题目:

                                                                                                                                                             Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
         

    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


    分析:题目大意是在数轴上给定任意起点n、终点k,对任意的点坐标x每次有3种走法:x-1x+12*x。每走一次花费时间为1minute,问从nk最少需要花费多少时间?

          该题是最短路径问题,于是可以用BFS搜索,每次往三个方向BFS,直到到达k,每走一步记录当前时间。

    //simonPR
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int maxn=100005;
    int n,k,vis[maxn];
    struct catche{
        int site,times;      //记录点和所花的时间。
    };
    queue<catche> q;
    void init();
    void init(){
        while(!q.empty())
            q.pop();
        memset(vis,0,sizeof(vis));
    }
    int bfs(int n,int k);
    int bfs(int n,int k){
        if(n==k) return 0;
        catche now,news,start;
        start.site=n;
        start.times=0;
        q.push(start);         //将起点入队列。
        vis[n]=1;
        while(!q.empty()){
            int ts;
            now=q.front();
            q.pop();
            for(int i=0;i<3;i++){      //分三个方向BFS。
                if(i==0) ts=now.site-1;
                else if(i==1) ts=now.site+1;
                else ts=2*now.site;
                if(ts>maxn||vis[ts]==1) continue;       //如果已经访问过了或超出数据范围则跳过。
                if(ts==k) return now.times+1;           //到达终点K,返回时间。
                if(vis[ts]==0)                          //更新点信息,并将新点入队列。
                {
                    vis[ts]=1;
                    news.site=ts;
                    news.times=now.times+1;
                    q.push(news);
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d",&n,&k);
        init();                    //初始化。
        int ans=bfs(n,k);
        printf("%d
    ",ans);
        return 0;
    }
    
    
  • 相关阅读:
    使用echarts插件做图表常见的几个问题(五)——图形的两种渲染方式
    数组对象如何根据对象中某个字段分组
    JS监听浏览器后退事件
    使用echarts插件做图表常见的几个问题(四)—— 柱状图中以虚线展示重合的柱子
    使用echarts插件做图表常见的几个问题(三)—— 图表标线的使用
    使用echarts插件做图表常见的几个问题(二)—— 实现多Y轴
    使用echarts插件做图表常见的几个问题(一)—— 折线图局部虚线
    如何判断touch事件滑动的方向?
    解决session共享方案
    设计模式总结
  • 原文地址:https://www.cnblogs.com/RRirring/p/4540657.html
Copyright © 2011-2022 走看看