Catch That Cow(HDU 2717)

本文介绍了如何使用广搜算法解决CatchThatCow问题,提供了两种实现方式:一种是利用循环队列,另一种是使用队列模板。通过具体代码示例展示了如何有效地避免栈溢出,并对算法的关键步骤进行了详细解释。

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

Catch That Cow

题目就不摘了-。-

题目链接

这道题一般用广搜写,可以用循环队列队列模板来解决数组开得不够大的问题。

测试时试试 0 10000这个数据,其结果为22,一定要保证 0 10000 不会栈溢出,否则出现running time error

本题关键是在入队之前就进行判断,这样就会减少入队的次数。

1,循环队列(c,c++)

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<stdlib.h>
using namespace std;

int main()
{
    int n, k;
    struct node
    {
        int s;
        int num;
    }que[200010];//队列

    int book[200010];//用来标记

    while (scanf("%d%d", &n, &k) == 2)
    {
        if (n >= k)
            printf("%d\n", n - k);//负的直接输出
        else
        {
            int min = k - n;

            memset(book, 0, sizeof(book));
            memset(que, 0, sizeof(que));

            int head, tail;
            head = 1;
            tail = 1;

            que[tail].num = n;
            que[tail].s = 0;
            tail++;

            while (head < tail)
            {
                book[que[head].num] = 1;

                que[tail].num = que[head].num + 1;
                if (que[tail].num == k)
                    break;
                if (book[que[tail].num] == 0 
                      && que[tail].num >= 0 
                      && que[tail].num <= 100000)//入队判断条件有三个
                {
                    book[que[tail].num] = 1;
                    que[tail++].s = que[head].s + 1;
                }
                if (tail >= 200000)
                    tail = 1;

                //接下来两个方向相当于复制-粘贴
                que[tail].num = que[head].num * 2;
                if (que[tail].num == k)
                    break;
                if (book[que[tail].num] == 0 
                      && que[tail].num >= 0 
                      && que[tail].num <= 100000)
                {
                    book[que[tail].num] = 1;
                    que[tail++].s = que[head].s + 1;
                }
                if (tail >= 200000)
                    tail = 1;

                que[tail].s = que[head].s + 1;
                que[tail].num = que[head].num - 1;
                if (que[tail].num == k)
                    break;
                if (book[que[tail].num] == 0 
                      && que[tail].num >= 0 
                      && que[tail].num <= 100000)
                {
                    book[que[tail].num] = 1;
                    que[tail++].s = que[head].s + 1;
                }
                if (tail >= 200000)
                    tail = 1;

                head++; 
                if (head >= 200000)
                    head = 1;
            }
            printf("%d\n", que[head].s+1);
        }
    }
    return 0;
}

2,队列模板(c++)

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;

int cow[100010];
int n, k;
void bfs()
{
    cow[n] = 0;
    queue<int> Q;
    int x, y;
    Q.push(n);//入队
    while (!Q.empty())//head == tail ??
    {
        x = Q.front();//返回队首
        Q.pop();//出队
        if (x == k)
            return;
        y = x + 1;
        if (y >= 0 && y <= 100000 && cow[y] == 0)
        {
            cow[y] = cow[x] + 1;
            Q.push(y);
        }
        y = x - 1;
        if (y >= 0 && y <= 100000 && cow[y] == 0)
        {
            cow[y] = cow[x] + 1;
            Q.push(y);
        }
        y = x * 2;
        if (y >= 0 && y <= 100000 && cow[y] == 0)
        {
            cow[y] = cow[x] + 1;
            Q.push(y);
        }
    }
}
int main()
{
    while (cin >> n >> k)
    {
        memset(cow, 0, sizeof(cow));
        bfs();
        cout << cow[k] << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值