You need to use a monotonous class queue topic

Histogram of the largest rectangular area

n * m matrix perimeter space

The maximum storage capacity of puddles

One-dimensional

  1. To h
  2. To high and high ceilings of the ground
  3. The ground is a triangular ramp

Two-dimensional

  1. Only to high ground
    NYOJ 547
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
ll a[310][310];
bool vis[310][310];
int n, m;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
struct node{
    int x, y, v;
    bool operator < (const node & oth)const
    {
        return v > oth.v;   // 每一层都按从小到大的值排
    }
};
priority_queue<node>pq;
void bfs()
{
    ll ans = 0;
    while (!pq.empty())
    {
        int x = pq.top().x, y = pq.top().y, v = pq.top().v;
        pq.pop();
        if (vis[x][y])
            continue;
        vis[x][y] = 1;
        for (int i = 0; i < 4; i++)
        {
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            int vv = a[xx][yy];
            if (xx < n && xx > 1 && yy < m && yy > 1 && !vis[xx][yy])
            {
                if (vv < v) //若该点小于当前点,则可升高
                {
                    ans += v - vv;
                    a[xx][yy] = v; //更新信息
                }
                pq.push({ xx, yy, a[xx][yy] });
            }       
        }
    }
    cout << ans << endl;
}
int main(){
    while (cin >> m >> n)
    {
        memset(vis, 0, sizeof vis);
        while (!pq.empty())
            pq.pop();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                cin >> a[i][j];
                if (i == 1 || j == 1 || i == n || j == m)
                    pq.push({ i, j, a[i][j] }); //先压入边界的点
            }
        bfs();
        /*for (int i = 1; i <= n; i++, cout << endl)
            for (int j = 1; j <= m; j++)
                cout << a[i][j] << " ";*/
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/babydragon/p/11478812.html
Recommended