zoukankan      html  css  js  c++  java
  • PAT 天梯赛 L3-008. 喊山 【BFS】

    题目链接

    https://www.patest.cn/contests/gplt/L3-008

    思路
    因为 每个山头 最多有两个 能听到它的 临近山头
    那么 我们就可以 给每个 山头 都 分配 最多两个子儿子
    然后 再从儿子 往下找
    一层一层往下找 如果找的元素 之前已经出现过 这个 就不压入 队列
    直到最后 队列为空 输出当层 编号最小的那个

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-3;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e4 + 5;
    const int MOD = 1e9 + 7;
    
    int ans;
    
    queue <int> opt;
    
    map <int, int> q, vis;
    
    struct Node
    {
        int l, r;
    }w[maxn];
    
    void bfs(int cur)
    {
        int len = opt.size();
        int Min = INF;
        while (len--)
        {
            int num = opt.front();
            opt.pop();
            if (num < Min)
                Min = num;
            if (vis[w[num].l] == 0)
            {
                opt.push(w[num].l);
                vis[w[num].l] = 1;
            }
            if (vis[w[num].r] == 0)
            {
                opt.push(w[num].r);
                vis[w[num].r] = 1;
            }
        }
        if (opt.empty())
        {
            ans = Min;
            return;
        }
        else
            bfs(cur + 1);
    }
    
    int main()
    {
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        int a, b;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &a, &b);
            q[a] = 1;
            q[b] = 1;
            if (w[a].l)
                w[a].r = b;
            else
                w[a].l = b;
            if (w[b].l)
                w[b].r = a;
            else
                w[b].l = a;
        }
        for (int i = 0; i < k; i++)
        {
            scanf("%d", &a);
            if (q[a] == 0)
                printf("0
    ");
            else
            {
                vis.clear();
                vis[0] = 1;
                while (!opt.empty())
                    opt.pop();
                if (w[a].l != 0)
                    opt.push(w[a].l);
                if (w[a].r != 0)
                    opt.push(w[a].r);
                vis[w[a].l] = 1;
                vis[w[a].r] = 1;
                vis[a] = 1;
                bfs(1);
                printf("%d
    ", ans);
            }
        }
    }
  • 相关阅读:
    随手乱记
    对拍程序
    生命游戏
    Command Operating System by cdsidi(ComSys) 0.2.x版本陆续更新
    C语言<stdio.h>的rename函数——重命名文件、更改文件路径或更改目录名
    C++ 类中的static 成员函数
    Command Operating System by cdsidi(ComSys) 0.1.x版本陆续更新
    Command Operating System by cdsidi (ComSys)首次发布(版本0.1.2)
    区间dp之 "石子合并"系列(未完结)
    C/C++快读(快速读入)有多——安全AC
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433222.html
Copyright © 2011-2022 走看看