zoukankan      html  css  js  c++  java
  • 5.11 每日一题题解

    P1332 血色先锋队

    涉及知识点:

    • bfs

    solution:

    • (这个题直接用bfs即可,前几天出过bfs的题了,巩固一下)
    • (在一开始将a个传染源读入队列,同时记录感染时间为0)
    • (每次访问上下左右四个节点,如果已经感染则不用再次入队)
    • (这样记录每个点对应的感染时间才是首次被感染的时间)

    std:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int N = 510;
    
    int n,m,a,b;
    
    typedef pair<int,int> PII;
    
    int ans[N][N];
    int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
    
    queue<PII> q;
    vector<PII> v;
    
    void bfs()
    {
        while(!q.empty())
        {
            auto t = q.front();
            q.pop();
            for (int i = 0 ; i < 4 ; i ++ )
            {
                int x = t.first+dx[i],y = t.second+dy[i];
                if(x >= 1 && x <= n && y >= 1 && y <= m && ans[x][y] == -1)
                {
                    ans[x][y] = ans[t.first][t.second]+1;
                    q.push({x,y});
                }
            }
        }
    }
    
    int main()
    {
        memset(ans,-1,sizeof ans);
        scanf("%d%d%d%d",&n,&m,&a,&b);
        while (a -- )
        {
            int x,y;
            scanf("%d%d",&x,&y);
            q.push({x,y});
            ans[x][y] = 0;
        }
        while (b -- )
        {
            int x,y;
            scanf("%d%d",&x,&y);
            v.push_back({x,y});
        }
        bfs();
        for (auto t : v)
        {
            printf("%d
    ",ans[t.first][t.second]);
        }
        return 0;
    }
    
  • 相关阅读:
    IndexDB
    实现es6中的set和map
    视口viewport
    nginx入门
    http协议
    图像格式
    promise
    js中this指向
    CSS 7阶层叠水平
    C# 一个方法如何返回多个值
  • 原文地址:https://www.cnblogs.com/QFNU-ACM/p/12868327.html
Copyright © 2011-2022 走看看