leetcode + acwing 接雨水问题总结

二维接雨水

leetcode : 原题链接

  • 直接预处理每个点,找往左走 KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲ 往右走能被拦下的最大高度,然后再遍历一遍左右数组,找到最小值与h[i]的差即可

C++代码

class Solution {
public:
    int trap(vector<int>& height) {
        if (height.empty()) return 0;
        int n = height.size();
        vector<int> left(n), right(n);
        left[0] = height[0];
        for (int i = 1; i < n; i ++ )
            left[i] = max(left[i - 1], height[i]);
        right[n - 1] = height[n - 1];
        for (int i = n - 2; i >= 0; i -- )
            right[i] = max(right[i + 1], height[i]);
        int res = 0;
        for (int i = 0; i < n; i ++ )
            res += min(left[i], right[i]) - height[i];
        return res;
    }
};

三维接雨水

acwing : 原题链接

leetcode : 原题链接

做法:缩圈法 + 木桶效应

  • 类似于最短路问题,最短路问题是求解路径总和的最小值;这里的问题是求出所有路径中最大值的最小值。这一题可以采用最短路径的方式求解,下面使用类似于dijkstea算法求解该问题(其实就是简单的bfs)。

  • 本题的起点可以设为大海,相当于新建了一个虚拟源点s。从s向四周的每个格子都连接一条边权为0的边,所有起点不能到达的点标记为正无穷,然后从s求单源最短路径即可(从a走到b的边权定义为b所在位置的高度)。

  • 代码实现时不需要真实地将s建立出来,直接将s到达的所有点加入优先队列即可。

  • dist[i][j]表示从s到(i,j)所有路径中边权最大值的最小值,如果从(i,j)能到达(x,y),则根据定义一定有:dist[x][y]≤max(dist[i][j],h[x][y])。解释如下:

  • 下面还有一个问题,就是要证明一下可以使用dijkstea算法求解该问题。核心是证明当前从优先队列中出队的元素(假设是(x,y))就是最小值,不可能被其他点更新了。可以用反证法证明,假设当前队首元素会被更新成更小的数据,则一定是被队列中后面的点(假设是(i,j))更新,但是从源点到后面的点的值更大,根据dist[x][y]一定是被max(dist[i][j],h[x][y]),这里是取大,所以当前队首元素不可能被更新的更小。

C++ 代码

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int N = 60;

int g[N][N];
bool st[N][N];

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int ans = 0;
int n, m;

struct node
{
    int x, y, val;
    bool operator< (const node& b) const 
    {
        return val > b.val;
    }
};

priority_queue<node> q;

void bfs()
{
    while(q.size())
    {
        auto t = q.top();
        q.pop();
        st[t.x][t.y] = true;
        
        for(int i = 0; i < 4; i ++ )
        {
            int tx = t.x + dx[i], ty = t.y + dy[i];
            if(tx < n && tx >= 0 && ty < m && ty >= 0 && !st[tx][ty])
            {
                st[tx][ty] = true;
                ans += max(0, t.val - g[tx][ty]);
                q.push({tx, ty, max(t.val, g[tx][ty])});
            }
        }
    }
}

int main()
{
    int t; cin >> t;
    for(int c = 1; c <= t; c ++ )
    {
        cin >> n >> m;
        ans = 0;
        memset(st, false, sizeof st);
        
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++ )    
                cin >> g[i][j];
                
        for(int i = 1; i < m - 1; i ++ )
        {
            st[0][i] = st[n - 1][i] = true;
            q.push({0, i, g[0][i]});
            q.push({n - 1, i, g[n - 1][i]});
        }
        
        for(int i = 1;  i < n - 1; i ++ )
        {
            st[i][0] = st[i][m - 1] = true;
            q.push({i, 0, g[i][0]});
            q.push({i, n - 1, g[i][m - 1]});
        }
        
        st[0][0] = st[0][m - 1] = st[n - 1][0] = st[n - 1][m - 1] = true;
        
        bfs();
        
        printf("Case #%d: %d\n", c, ans);
    }
    
    return 0;
}


三维接雨水变种

所谓变种就是由求储水量变为求水坑数,转化为一个求连通块问题(经典染色法)

题目详情:
在X星的一篇建筑工地上堆放着很多防水板砖,它们每一块的规格都一模一样,但是每一叠都高矮不一,这些砖块堆放得非常整齐而且非常紧凑,紧凑到“滴水不漏”。俯视这些砖块可以得到一个n*m的矩阵,第n行m列上的数字表示对应位置的板砖数量,例如下面的矩阵所示:

捕获.PNG

第1行第1列的“2”表示这个位置对应的那一叠板砖的数量为2.某一天突然天降暴雨,暴雨过后,在板砖区形成了很多小水坑,如果某一叠板砖的数量比它周围前、后、左、右的板砖数量少,将形成一个小水坑。相邻的两叠或者多叠板砖可能会构成一个大一点的水坑。例如在上面的图中,两叠红色的板砖构成一个水坑,因为它们周围上、下、左、右的板砖(蓝色板砖)的数量比它们要多。

假如这场雨下得足够大,足以让每一水坑都装满水。现在请问,暴雨过后在板砖区一共留下了多少个水坑?

Input
第一行输入两个正整数分别表示n和m,n和m均不超过100,两个数字之间用空格隔开

接下来n行是一个n*m的矩阵,每一行m个正整数,表示某一叠板砖的数量,两个正整数之间用空格隔开

Output
输出一个整数,即留下的水坑数量

Sample Input 1

4 4
2 3 5 1
4 1 2 3
1 5 4 2
1 2 2 2

Sample Output 1

1

C++代码

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>

#define a first
#define b second
using namespace std;
const int N = 60;
typedef pair<int, int> PII;

int g[N][N];
bool st[N][N];

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int ans = 0;
int n, m;

struct node
{
    int x, y, val;
    bool operator< (const node& b) const 
    {
        return val > b.val;
    }
};

priority_queue<node> q;

void bfs()
{
    while(q.size())
    {
        auto t = q.top();
        q.pop();
        st[t.x][t.y] = true;
        
        for(int i = 0; i < 4; i ++ )
        {
            int tx = t.x + dx[i], ty = t.y + dy[i];
            if(tx < n && tx >= 0 && ty < m && ty >= 0 && !st[tx][ty])
            {
                st[tx][ty] = true;
                //打标记
                if(max(0, t.val - g[tx][ty])) g[tx][ty] = 0;
                q.push({tx, ty, max(t.val, g[tx][ty])});
            }
        }
    }
}

void bfs1(int a, int b)
{
    queue<PII> q;
    q.push({a, b});
    st[a][b] = true;

    int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i = 0;i < 4; i ++ )
        {
            int x = t.a + dx[i], y = t.b + dy[i];
            if(x >= 0 && x <= n && y >= 0 && y <= m && !st[x][y] && g[x][y] == 0)
            {
                g[x][y] = 1;
                q.push({x, y});
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    ans = 0;
    memset(st, false, sizeof st);
        
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++ )    
            cin >> g[i][j];
               
    for(int i = 1; i < m - 1; i ++ )
    {
        st[0][i] = st[n - 1][i] = true;
        q.push({0, i, g[0][i]});
        q.push({n - 1, i, g[n - 1][i]});
    }
        
    for(int i = 1;  i < n - 1; i ++ )
    {
        st[i][0] = st[i][m - 1] = true;
        q.push({i, 0, g[i][0]});
        q.push({i, n - 1, g[i][m - 1]});
    }
        
    st[0][0] = st[0][m - 1] = st[n - 1][0] = st[n - 1][m - 1] = true;
        
    bfs();
        
    memset(st, false, sizeof st);
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < m; j ++ )
            if(!st[i][j] && g[i][j] == 0)
            {
                bfs1(i, j);
                ans ++;
            }
    cout << ans << endl;   

    return 0;
}

总结:自己太菜了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1bu3dong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值