Official

A - 最小の額縁 / Smallest Frame Editorial by admin Editorial for printing

DeepSeek V3

Overview

This problem asks you to find the perimeter of the smallest rectangle that contains all painting tiles (#) placed on a grid.

Key Observation

The important observation is that the smallest rectangle containing all painting tiles is determined by the minimum and maximum values of the rows and columns where painting tiles exist. Specifically: - Minimum row index (min_row) - Maximum row index (max_row) - Minimum column index (min_col) - Maximum column index (max_col)

From these values, we calculate the height and width of the rectangle, then determine the perimeter. A naive approach that tries all possible rectangles would have a time complexity of \(O(H^2W^2)\), which would be far too slow given the constraints (\(H, W \leq 1000\)). However, with the above observation, we can solve this efficiently.

Algorithm

  1. Read the grid.
  2. Scan all cells and check whether each is a painting tile (#).
  3. If a painting tile is found, update the minimum and maximum values using its row and column indices.
  4. If no painting tiles exist, output 0.
  5. Calculate the height from the minimum and maximum rows (height = max_row - min_row + 1) and the width from the minimum and maximum columns (width = max_col - min_col + 1).
  6. Compute the perimeter as \(2 \times (height + width)\) and output it.

Complexity

  • Time complexity: \(O(H \times W)\)
    • Since we scan the grid once
  • Space complexity: \(O(H \times W)\)
    • For storing the grid

Implementation Notes

  • Initial value setup: Set the minimum values to the largest possible values (H or W), and the maximum values to -1.

  • Handling the case where no painting tiles exist: Use a counter to check for the existence of painting tiles, and output 0 if there are none.

  • Index handling: Since grid indices start from 0, you need to add +1 when calculating the range of rows and columns.

    Source Code

def main():
    import sys
    data = sys.stdin.read().splitlines()
    if not data:
        print(0)
        return
        
    H, W = map(int, data[0].split())
    grid = []
    for i in range(1, H+1):
        grid.append(data[i].strip())
    
    min_row = H
    max_row = -1
    min_col = W
    max_col = -1
    count = 0
    
    for i in range(H):
        for j in range(W):
            if grid[i][j] == '#':
                count += 1
                if i < min_row:
                    min_row = i
                if i > max_row:
                    max_row = i
                if j < min_col:
                    min_col = j
                if j > max_col:
                    max_col = j
                    
    if count == 0:
        print(0)
        return
        
    height = max_row - min_row + 1
    width = max_col - min_col + 1
    perimeter = 2 * (height + width)
    print(perimeter)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

posted:
last update: