Catch That Cow POJ - 3278

9 篇文章 0 订阅
/*
 * 
 * 
 *想用结构体做下,结果一直RE,1 40000,不会有结果。用数组做了就AC了....待解决 
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>

using namespace std;

const int Max = 100000+5;

int N, K;

bool pos[Max];

//int dir[3] = {-1, 1};

struct Node
{
    int p; //position
    int step;
    Node(){
        p = 0;
        step = 0;
    }
    Node(int q, int b){
      p = q, step = b;
    }
};
Node tmp1;

void bfs()
{

    queue<Node> q;
    while(!q.empty()){
        q.pop();
    }
    Node tmp;

    tmp.p = N;
    tmp.step = 0;
    //   cout << tmp.p << endl;
    q.push(tmp);
    pos[tmp.p] = true;

    while(!q.empty()){
        tmp1 = q.front();

        if(tmp1.p == K){
            return;
        }
        q.pop();

        for(int i=0; i<3; i++){

            Node tmp2;

            if(i == 1){
                tmp2.p = tmp1.p*2;
            }
            else
                tmp2.p = tmp1.p+i-1;

            cout << tmp2.p << endl;
            //judge
            if((!pos[tmp2.p]) && tmp2.p >=0 &&tmp2.p <= Max){
                tmp2.step = tmp1.step+1;
//                cout << tmp2.t << " ";
                q.push(tmp2);

                pos[tmp2.p] = true;

            }
        }
//        cout << endl;

    }

}

int main()
{
    while(cin >> N >> K){
        memset(pos, false, sizeof(pos));
        bfs();
        cout << tmp1.step << endl;
    }

    return 0;
}*/


#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>

using namespace std;

const int Max = 100000+5;

int N, K;

bool pos[Max];
int step[Max];
//int dir[3] = {-1, 1};

struct Node
{
    int p; //position
    int step;
    Node(){
        p = 0;
        step = 0;
    }
    Node(int q, int b){
      p = q, step = b;
    }
};
int tmp1;

void bfs()
{

    queue<int> q;
    while(!q.empty()){
        q.pop();
    }
    int tmp;

    tmp = N;
    step[tmp] = 0;
    //   cout << tmp.p << endl;
    q.push(tmp);
    pos[tmp] = true;

    while(!q.empty()){
        tmp1 = q.front();

        if(tmp1 == K){
            return;
        }
        q.pop();

        for(int i=0; i<3; i++){

            int tmp2;

            if(i == 1){
                tmp2 = tmp1*2;
            }
            else
                tmp2 = tmp1+i-1;

//            cout << tmp2 << endl;
            //judge
            if((!pos[tmp2]) && tmp2 >=0 &&tmp2 <= Max){
                step[tmp2] = step[tmp1]+1;
//                cout << tmp2.t << " ";
                q.push(tmp2);

                pos[tmp2] = true;

            }
        }
//        cout << endl;

    }

}

int main()
{
    while(cin >> N >> K){
        memset(pos, false, sizeof(pos));
        memset(step, 0, sizeof(step));

        bfs();
        cout << step[K] << endl;
    }

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值