抓住那头牛

P1048 抓住那头牛
描述
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000)。农夫有两种移动方式:

1、从X移动到X-1或X+1,每次移动花费一分钟

2、从X移动到2*X,每次移动花费一分钟

假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?

格式
输入格式
两个整数,N和K。
输出格式
一个整数,农夫抓到牛所要花费的最小分钟数。
样例
输入样例
5 17
输出样例
4
一开始想用数学方法,没想到bfs
错误代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int minn(int a, int b, int c)
{
    a = a < b ? a : b;
    a = a < c ? a : c;
    return a;
}
int main()
{
    int n, k;
    cin >> n >> k;
    int ans;
    ans = 0;
    //int d;
    if (n <= k)
    {

        int n1, n2, n3, n4;
        int t1, t2, t, t3, t4;
        t = 0;
        t1 = t2 = 1;
        t3 = t4 = 2;
        n1 = n - 1;
        n2 = n + 1;
        n3 = n - 2;
        n4 = n + 2;
        while (abs(k - n) > abs(k - 2 * n))
        {
            n = n * 2;
            t++;
        }
        t = t + (abs(k - n));
        while (abs(k - n1) > abs(k - 2 * n1))
        {
            n1 = n1 * 2;
            t1++;
        }
        t1 = t1 + (abs(k - n1));
        while (abs(k - n2) > abs(k - 2 * n2))
        {
            n2 = n2 * 2;
            t2++;
        }
        t2 = t2 + (abs(k - n2));
        while (abs(k - n3) > abs(k - 2 * n3))
        {
            n3 = n3 * 2;
            t3++;
        }
        t3 = t3 + (abs(k - n3));
        while (abs(k - n4) > abs(k - 2 * n4))
        {
            n4 = n4 * 2;
            t4++;
        }
        t4 = t4 + (abs(k - n4));
        ans = minn(t, t1, t2);
        ans = minn(ans, t3, t4);
        printf("%d\n", ans);
    }
    else
    {
        printf("%d\n", n - k);
    }

    return 0;
}

一个简单的bfs题目,就是二维数组换成了一维,我新手小白按照魔板照写了一个
正确代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <list>
using namespace std;

struct node
{
    int x;
    int setp;
};
int dis[3] = {-1, 1, 2};//定义走法
int n, k;
int vis[100010];      //*不要忘记vis数组,不然很容易就超时了,比如反复横跳
void bfs()
{
    memset(vis,0,sizeof(vis));     
    node st, en;
    queue<node> s;
    st.x = n;
    st.setp = 0;
    vis[st.x]=1;
    s.push(st);
    while (!s.empty())
    {
        st = s.front();
        s.pop();
        if (st.x == k)
        {
            cout << st.setp << endl;
            return;
        }
        for (int i = 0; i < 3; i++)
        {
            if (i == 2)
            {
                en.x = st.x * 2;
                en.setp = st.setp + 1;   //*这里的en与st不要混淆
            }
            else
            {
                en.x = st.x + dis[i];
                en.setp = st.setp + 1;
            }
            if (en.x <= 100000 && en.x >= 0&&vis[en.x]==0)
            {   
                vis[en.x]=1;
                s.push(en);
            }
        }
    }
}

int main()
{

    cin >> n >> k;
    if (n <= k)
    {
        bfs();
    }
    else
    {
        printf("%d\n", n - k);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值