• [BFS] [洛谷] P1135 奇怪的电梯


    一看求最短步数

    嗯 简单BFS

    主题构建非常容易

    但是有段时间没写了 

    MLE 一发

    马上反应过来没判重 出现来回走两个点的情况

    可以证明:

    走过的点不用管以后会不会走到

    因为以后再走肯定没有现在走优

    所以走过的点直接标记

    以后就不走了

    #include <iostream>
    #include <queue>
    using namespace std;
    
    const int MAXN = 2e2 + 10;
    
    bool isfind = false;
    
    typedef long long ll;
    
    ll arr[MAXN] = {0};
    
    ll brr[MAXN] = {0};
    
    ll N, A, B;
    
    struct floor
    {
        ll now;
        ll step;
    }beg;
    
    queue <floor> q;
    
    void bfs(floor beg)
    {
        floor ret;
    
        ret = beg;
    
        q.push(ret);
    
        while(!q.empty())
        {
            ret = q.front();
    
            q.pop();
    
            if(ret.now == B)
            {
                cout<<ret.step<<endl;
    
                isfind = true;
    
                return ;
            }
    
            else
            {
                floor tmp;
    
                tmp = ret;
                if(brr[tmp.now] == 0)
                {
                    brr[tmp.now] = 1;
    
                    if(tmp.now + arr[tmp.now] <= N)
                    {
                        tmp.now += arr[tmp.now];
                        tmp.step ++;
    
                        q.push(tmp);
                    }
    
                    tmp = ret;
    
                    if(tmp.now - arr[tmp.now] >= 1)
                    {
                        tmp.now -= arr[tmp.now];
                        tmp.step++;
    
                        q.push(tmp);
                    }    
                }
                
            }
    
        }
    }
    
    int main()
    {
        cin>>N>>A>>B;
    
        for(int i = 1; i <= N; i++)
        {
            cin>>arr[i];
        }
    
        beg.now = A;
    
        beg.step = 0;
    
        bfs(beg);
    
        if(!isfind)
        {
            cout<<-1<<endl;
        }
    
        return 0;
    }
  • 相关阅读:
    数据库创建索引后如何查看是否生效
    两种动态代理的区别
    zuul请求过滤
    intellij idea 中 Job Debug特别缓慢问题
    vue路由注册及创建
    vue.config.js配置文件
    TypeScript配置文件
    如何发布自己的npm安装包
    npm常见命令
    通过foreach的方式批量插入
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270532.html
Copyright © 2020-2023  润新知