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

    A - 用水填坑

    涉及知识点:

    • 优先队列/BFS

    solution:

    • 首先将这块地的四边放入优先队列,以它为这个水坑的边界
    • 每次取出边界上最小的数,然后搜索它上下左右四个点
    • 如果这些点里有比它小的,就说明将这个坑的水填到和取出的数一样高水不会溢出
    • 为什么要取最小的数可以想一下木桶效应
    • 然后把遍历的上下左右四个点作为新的边界继续进行上述步骤,直到遍历完所有的数

    std:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef unsigned long long ull;
    typedef pair<int,pair<int,int>> PII; 
    
    const int N = 1010;
    
    int a[N][N];
    bool st[N][N];
    int dx[]={-1,1,0,0},dy[] = {0,0,1,-1};
    int n,m;
    priority_queue<PII , vector<PII>, greater<PII> >heap;
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for (int i = 1 ; i <= n ; i ++ )
        {
            for (int j = 1 ; j <= m ; j ++ )
            {
                scanf("%d",&a[i][j]);
                if(i == 1 || j == 1 || i == n || j == m)
                {
                    st[i][j] = true;
                    heap.push({a[i][j],{i,j}});
                }
            }
        }
        
        ull res = 0;
        while(!heap.empty())
        {
            auto t = heap.top();
            heap.pop();
            for (int i = 0 ; i < 4 ; i ++ )
            {
                int x = t.second.first + dx[i];
                int y = t.second.second + dy[i];
                if(x < 1 || y < 1 || x > n || y > m || st[x][y]) continue;
                else
                {
                    if(a[x][y] < t.first)
                    {
                        res += t.first - a[x][y];
                        a[x][y] = t.first;
                    }
                    heap.push({a[x][y],{x,y}});
                    st[x][y] = true;
                }
            }
        }
        printf("%lld
    ",res);
        return 0;
    }
    
    #include <iostream>
    #include <vector>
    #include <queue>
    
    using namespace std;
    int n,m;
    const int N = 1010;
    
    int g[N][N];
    typedef pair<int,pair<int,int>> PIP;
    priority_queue<PIP,vector<PIP>,greater<PIP>>q;
    
    int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
    bool st[N][N];
    
    int main()
    {
        cin >> n >> m;
        for(int i = 0;i < n;i ++)
            for(int j = 0;j < m;j ++)
            {
                cin >> g[i][j];
                if(i == 0 || j == 0 || i == n - 1 || j == m - 1){
                    q.push({g[i][j],{i,j}});
                    st[i][j] = true;
                }
            }
        
        long long res = 0;
        
        while(q.size())
        {
            auto t = q.top();q.pop();
            
            for(int i = 0;i < 4;i ++)
            {
                int x = t.second.first + dx[i],y = t.second.second + dy[i];
                
                if(x < 0 || x >= n || y < 0 || y >= m || st[x][y])continue;
                
                if(g[x][y] < t.first){
                    res += t.first - g[x][y];
                    g[x][y] = t.first;
                }
                
                q.push({g[x][y],{x,y}});
                st[x][y] = true;
                
            }
            
            
        }
        
        cout << res << endl;
        
        return 0;
    }
    
  • 相关阅读:
    1mustache基本使用
    better-scroll
    PTA 题解:jmu-Java&Python-统计文字中的单词数量并按出现次数排序
    Java : Comparable 和 Comparator 接口
    java校验特殊符号
    vue项目左右布局的菜单效果,树形菜单
    响应式树形菜单导航,jq+ajax
    2020书单
    vite 为什么快
    vite 尝鲜
  • 原文地址:https://www.cnblogs.com/QFNU-ACM/p/12965619.html
Copyright © 2011-2022 走看看