zoukankan      html  css  js  c++  java
  • HDU 1548 A strange lift(BFS)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548

    玩了两天,差点写不出了。。。。。。。

    这题是由某个状态遍历其他状态的BFS

    对于某一状态下有---->所在层:now_floor,从开始到该层按了几次按钮:step。。。。。

    #include <queue>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int INF = 1 << 30;
    
    int a[222];
    bool vis[222];
    int n;
    
    struct point
    {
        int now_floor, step;
    };
    
    
    int Bfs(int star, int end)
    {
        memset(vis, 0, sizeof(vis));
        point q1, q2;
        queue <point> q;
        q1.now_floor = star - 1;
        q1.step = 0;
        q.push(q1);
        vis[q1.now_floor] = 1;
    
        while (!q.empty())
        {
            q2 = q.front();
            q.pop();
    
            if (q2.now_floor == end - 1)
            {
                return q2.step;
            }
    
            q1.now_floor = q2.now_floor + a[q2.now_floor];
            q1.step = q2.step + 1;
            if (q1.now_floor < n && q1.now_floor >= 0 && !vis[q1.now_floor])
            {
    
                q.push(q1);
                vis[q1.now_floor] = 1;
            }
    
            q1.now_floor = q2.now_floor - a[q2.now_floor];
    
            if (q1.now_floor >= 0 && q1.now_floor < n && !vis[q1.now_floor])
            {
                q.push(q1);
                vis[q1.now_floor] = 1;
            }
        }
        return -1;
    
    }
    
    int main()
    {
    
        int i, j;
        int ans;
        int star, end;
    
        while (~scanf("%d", &n), n)
        {
            scanf("%d%d", &star, &end);
            for (i = 0; i < n; i++)
            {
                scanf("%d", a + i);
            }
    
            
            printf("%d\n", Bfs(star, end));
    
        }
    
        return 0;
    }



  • 相关阅读:
    Linux下MySQL主从同步配置
    Tortoisegit图文使用教程
    C语言I博客作业06
    第十周助教总结
    C语言I博客作业04
    C语言I博客作业02
    第十一周助教总结
    第十二周助教总结
    第九周助教总结
    C语言I博客作业02
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2439385.html
Copyright © 2011-2022 走看看