[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述

给定一个二维矩阵,每个元素都有一个正整数值,表示高度。这样构成了一个二维的、有高度的物体。请问该矩阵可以盛放多少水?

相关题目:POJ The Wedding Juicer

Description

Farmer John’s cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows:
* A flat board of size W cm x H cm is procured (3 <= W <= 300, 3 <= H <= 300)
* On every 1 cm x 1 cm square of the board, a 1 cm x 1 cm block is placed. This block has some integer height B (1 <= B <= 1,000,000,000)

The blocks are all glued together carefully so that punch will not drain through them. They are glued so well, in fact, that the corner blocks really don’t matter!

FJ’s cows can never figure out, however, just how much punch their bowl designs will hold. Presuming the bowl is freestanding (i.e., no special walls around the bowl), calculate how much juice the bowl can hold. Some juice bowls, of course, leak out all the juice on the edges and will hold 0.
Input

  • Line 1: Two space-separated integers, W and H

  • Lines 2..H+1: Line i+1 contains row i of bowl heights: W space-separated integers each of which represents the height B of a square in the bowl. The first integer is the height of column 1, the second integers is the height of column 2, and so on.
    Output

  • Line 1: A single integer that is the number of cc’s the described bowl will hold.
    Sample Input

4 5
5 8 7 7
5 2 1 5
7 1 7 1
8 9 6 9
9 8 9 9
Sample Output

12

相关题目:lintcode Trapping Rain Water II

Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining.

《[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II》

Have you met this question in a real interview? Yes
Example
Given 5*4 matrix

[12,13,0,12]
[13,4,13,12]
[13,8,10,12]
[12,13,12,12]
[13,13,13,13]
return 14.

问题解答

  • 维护一个最小堆,保存外围的高度
  • 每次取最小高度 h,查看周围4个没有被访问过的元素:
  • 如果该元素的高度小于 h,则注入水到其中,并将其加入到最小堆中,设置该元素被访问过
  • 如果该元素的高度大于 h,则直接将其加入到最小堆中,设置改元素被访问过

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <queue>
#include <map>
#include <set>
#include <queue>

using namespace std;

struct Node {
    int x, y, h;
    Node (int xx, int yy, int hh): x(xx), y(yy), h(hh) {}

    friend bool operator > (const Node &n1, const Node &n2);
};

inline bool operator > (const Node &n1, const Node &n2) {
    return n1.h > n2.h;
}

class Solution {
public:
    void floodfill() {
        int W, H, ans = 0;
        cin >> W >> H;
        vector<vector<int> > board(H, vector<int>(W, 0));
        vector<vector<bool> > visited(H, vector<bool>(W, false));
        priority_queue<Node, vector<Node>, greater<Node> > pq;
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
            scanf("%d", &board[i][j]);
            if (i == 0 || i == H-1 || j == 0 || j == W-1) {
                Node n(i, j, board[i][j]);
                pq.push(n);
                visited[i][j] = true;
            }
        }
        int dir[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}};
        while (!pq.empty()) {
            Node top = pq.top(); pq.pop();
            for (int d = 0; d < 4; d++) {
                int next_x = top.x + dir[d][0], next_y = top.y + dir[d][1];
                if (next_x < 0 || next_y < 0 || next_x >= H || next_y >= W) continue;
                if (visited[next_x][next_y]) continue;
                visited[next_x][next_y] = true;
                Node tmp(next_x, next_y, board[next_x][next_y]);
                if (tmp.h < top.h) {
                    ans += top.h - tmp.h;
                    tmp.h = top.h;
                }
                pq.push(tmp);
            }
        }
        printf("%d\n", ans);

    }
};

int main() {
    Solution solution;
    solution.floodfill();
    return 0;
}
点赞