zoukankan      html  css  js  c++  java
  • POJ.3278 Catch That Cow (BFS)

    POJ.3278 Catch That Cow (BFS)

    题意分析

    给出给出初始坐标N,你可以执行的操作有N-1,N+1,N*2,求出最少需要几次操作,使得N=K。

    BFS时每次有3种操作,按照操作来即可。特别需要注意越界的问题,坐标不能小于0,也不能大于题目给的最大值100000.然后就没太大问题。

    一开始因为忘记数组越界的问题,RE了几次。

    代码总览

    #include <queue>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define nmax 100005
    using namespace std;
    int sta ,end;
    typedef struct{
        int pos;
        int times;
    }mes;
    bool visit[nmax];
    queue<mes> q;
    void bfs()
    {
        while(!q.empty()) q.pop();
        mes temp, head = {sta,0};
        q.push(head);
        while(!q.empty()){
            head = q.front();q.pop();
            if(head.pos == end){
                printf("%d
    ",head.times);
                return;
            }
            temp.times = head.times + 1;
            temp.pos = head.pos-1;
            if(temp.pos >=0 && !visit[temp.pos]){
                q.push(temp);
                visit[temp.pos] = true;
            }
            temp.pos = head.pos+1;
            if(temp.pos < nmax && !visit[temp.pos]){
                q.push(temp);
                visit[temp.pos] = true;
            }
            temp.pos = head.pos * 2 ;
            if(temp.pos < nmax && !visit[temp.pos]){
                q.push(temp);
                visit[temp.pos] = true;
            }
        }
    }
    int main()
    {
        while(scanf("%d %d",&sta,&end) != EOF){
            memset(visit,0,sizeof(visit));
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    C# 设计模式
    FutureTask、Fork/Join、 BlockingQueue
    线程的几种创建方式
    行锁、表锁、乐观锁、悲观锁
    J.U.C之AQS
    同步容器并发容器
    线程不安全类
    线程封闭
    不可变对象
    安全发布对象—单例模式
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367058.html
Copyright © 2011-2022 走看看