7/16 每日刷题记录 一道特别坑的题

大家小心hdu2717,本身这道题就是一个裸的bfs,而且是状态转移特别简单的题。

但是题目说明的不清楚,这道题没有说是多组输入输出,然后样例也只给出了一组输入输出。

导致我死活改不出来,(昨天直接心态炸裂)

 受害者视角。大家一定要注意多组输入输出的问题,一定要认真审题啊。

ac代码:

#include <bits/stdc++.h>
using namespace std;
int a, b;
struct node
{
    int x;
    int time;
};
const int N = 2e6 + 5;
int vis[N];
int check(int x)
{
    if (x < 0 || x >= N || vis[x])
        return 0;
    return 1;
}
void bfs()
{
    struct node cur, nex;
    cur.x = a;
    vis[cur.x] = 1;
    cur.time = 0;
    queue<node> qu;
    qu.push(cur);
    while (!qu.empty())
    {
        cur = qu.front();
        qu.pop();
        if (cur.x == b)
        {
            printf("%d\n", cur.time);
            return;
        }
        else
        {
            nex.x = cur.x + 1;
            if (check(nex.x))
            {
                vis[nex.x] = 1;
                nex.time = cur.time + 1;
                qu.push(nex);
            }
            nex.x = cur.x - 1;
            if (check(nex.x))
            {
                vis[nex.x] = 1;
                nex.time = cur.time + 1;
                qu.push(nex);
            }
            nex.x = cur.x * 2;
            if (check(nex.x))
            {
                vis[nex.x] = 1;
                nex.time = cur.time + 1;
                qu.push(nex);
            }
        }
    }
}
int main()
{
    
    while (cin >> a >> b)
    {
    	memset(vis, 0, sizeof(vis));
        bfs();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值