Home HDU-5538 House Building
Post
Cancel

HDU-5538 House Building

Description

给定一个图\(grid[x][y] = h\),其中\(h\)为对应坐标\((x, y)\)的高度,求表面积。

Constraints

  • \(1 \le T \le 50\)
  • \(1 \le x, y \le 50\)
  • \(0 \le grid[x][y] \le 10^3\)

Solution

这是某场区域赛的签到题,我多少年前做过这道题呢,真是遥远的过去啊。

上题类似,只是二维变成三维,枚举四边即可。

由于不知道现在HDU支持哪个版本的g++,先使用std=gnu++11

Code

#include <bits/stdc++.h>
using namespace std;

int grid[53][53];
int n, m;

template <typename F>
void foreach(F f) {
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            f(i, j);
        }
    }
}

int read() {
    int x;
    cin >> x;
    return x;
}

void run() {
    memset(grid, 0, sizeof grid);
    n = read();
    m = read();
    int ans = 0;
    foreach([&](int x, int y) {
        grid[x][y] = read();
        // NOTE: 顶部也算入答案
        if(grid[x][y]) ans++;
    });
    foreach([&](int x, int y) {
        auto h = grid[x][y];
        using Dir = int[][2];
        for(auto &d : Dir {0, 1, 1, 0, -1, 0, 0, -1}) {
            auto nx = x + d[0];
            auto ny = y + d[1];
            if(nx < 0 || nx >= n) ans += h;
            else if(ny < 0 || ny >= m) ans += h;
            else ans += max(grid[nx][ny] - h, 0);
        }
    });
    cout << ans << endl;
}

int main() {
    int T = read();
    while(T--) run();
    return 0;
}

HDU-5538 House Building

This post is licensed under CC BY 4.0 by the author.
Contents