Catch That Cow POJ - 3278

本文探讨了在解决特定搜索问题时,使用结构体与数组实现广度优先搜索(BFS)算法的区别。作者最初尝试使用结构体进行数据存储,但遭遇运行时错误(RE),改用数组后成功通过评测(AC)。文章详细记录了两种实现方式的代码,为读者提供了实际案例,展示了不同数据结构在算法效率和性能上的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 
 * 
 *想用结构体做下,结果一直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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值