1 条题解

  • 0

    题意分析

    迷宫由 N×NN \times N 的矩阵表示,其中 # 表示墙壁,. 表示空地。墙壁的每个方块占据 3×33 \times 3 平方米的面积,高度为 33 米。需要计算所有可见墙面的总面积(包括外围墙和内部 # 块的墙面,但相邻的墙面重叠部分不计)。

    解题思路

    1. 外围墙计算

      • 迷宫四周(除入口)有围墙,总长度为 (4N4)×3(4N - 4) \times 3 米(高度 33 米),面积为 (4N4)×9(4N - 4) \times 9
      • 左上角和右下角是入口,需减去 2×3×3=182 \times 3 \times 3 = 18 平方米。
      • 因此,外围墙面积为 (4N6)×9(4N - 6) \times 9
    2. 内部 # 块墙面计算

      • 每个 # 块贡献 4×3×3=364 \times 3 \times 3 = 36 平方米(四个面)。
      • 每有一个相邻 # 块(上下左右)就减少 99 平方米(重叠面)。
      • 每个 # 的实际贡献:369×36 - 9 \times 相邻 # 数。
    3. 总表面积

      • 外围墙面积 ++ 所有内部 # 块的贡献。

    实现步骤

    1. 输入处理:读取 NN 和迷宫矩阵。
    2. 计算外围墙面积(4N6)×9(4N - 6) \times 9
    3. 遍历内部 #
      • 对每个 #,检查四个方向(上下左右)是否有其他 #
      • 统计相邻数 kk,该块贡献为 369k36 - 9k
    4. 累加总面积:外围面积 ++ 所有内部 # 贡献。
    5. 输出结果

    代码实现

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    const int WALL_HEIGHT = 3;
    const int BLOCK_SIZE = 3;
    
    int calculateWallArea(const vector<string>& labyrinth, int N) {
        int totalArea = 0;
        
        // Calculate perimeter walls (excluding entrances)
        // Top and bottom walls
        totalArea += (N * 2 - 2) * BLOCK_SIZE * WALL_HEIGHT;
        
        // Left and right walls
        totalArea += (N * 2 - 2) * BLOCK_SIZE * WALL_HEIGHT;
        
        // Calculate internal walls
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                if (labyrinth[i][j] == '#') {
                    // Each block contributes 4 sides (up, down, left, right)
                    int visibleSides = 4;
                    
                    // Check adjacent cells to subtract hidden sides
                    if (i > 0 && labyrinth[i-1][j] == '#') visibleSides--;
                    if (i < N-1 && labyrinth[i+1][j] == '#') visibleSides--;
                    if (j > 0 && labyrinth[i][j-1] == '#') visibleSides--;
                    if (j < N-1 && labyrinth[i][j+1] == '#') visibleSides--;
                    
                    totalArea += visibleSides * BLOCK_SIZE * WALL_HEIGHT;
                }
            }
        }
        
        return totalArea;
    }
    
    int main() {
        int N;
        cin >> N;
        
        vector<string> labyrinth(N);
        for (int i = 0; i < N; ++i) {
            cin >> labyrinth[i];
        }
        
        cout << calculateWallArea(labyrinth, N) << endl;
        
        return 0;
    }
    
    • 1

    信息

    ID
    1358
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者