其他分享
首页 > 其他分享> > 5.26每日一题题解

5.26每日一题题解

作者:互联网

A - 用水填坑

涉及知识点:

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\n",res);
    return 0;
}

标签:typedef,int,题解,每日,long,st,5.26,res,first
来源: https://www.cnblogs.com/QFNU-ACM/p/12965619.html