内容中心

2026年靠谱的盐城中转袋推荐厂家精选-盐城市亿德塑料包装有限公司

To solve this problem, we need to determine the minimum number of axis-aligned rectangles required to cover all black squares (1s) in a grid, where each rectangle can only contain black squares (no white squares, 0s).

Approach

  1. Visited Matrix: We use a visited matrix to keep track of which cells have already been covered by a rectangle to avoid reprocessing.
  2. Identify Rectangles: For each cell that is black and not visited:
    • Max Width: Find the maximum horizontal width of consecutive black cells starting from the current cell.
    • Max Height: Find the maximum vertical height such that all cells in the columns of the identified width are black (extending downwards from the current cell).
    • Mark Visited: Mark all cells in the identified rectangle as visited and increment the count of rectangles.

This approach ensures that we cover the maximum possible area with each rectangle, leading to the minimum number of rectangles needed.

Solution Code

def min_rectangles(grid):
    if not grid or not grid[0]:
        return 0
    rows = len(grid)
    cols = len(grid[0])
    visited = [[False for _ in range(cols)] for _ in range(rows)]
    count = 0

    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1 and not visited[i][j]:
                count += 1
                # Find maximum width of the rectangle
                w = 1
                while j + w < cols and grid[i][j + w] == 1:
                    w += 1
                # Find maximum height of the rectangle
                h = 1
                while i + h < rows:
                    valid = True
                    for k in range(j, j + w):
                        if grid[i + h][k] != 1:
                            valid = False
                            break
                    if not valid:
                        break
                    h += 1
                # Mark all cells in the rectangle as visited
                for x in range(i, i + h):
                    for y in range(j, j + w):
                        visited[x][y] = True
    return count

Explanation

  1. Visited Matrix: This matrix helps us avoid reprocessing cells that have already been covered by a rectangle.
  2. Max Width Calculation: For each cell, we expand horizontally to find the longest sequence of consecutive black cells. This gives us the width of the rectangle.
  3. Max Height Calculation: For the identified width, we expand vertically to check if all cells in the columns of the width are black. This gives us the height of the rectangle.
  4. Marking Visited: Once the rectangle is identified, all cells in it are marked as visited, so they are not considered again.

This method efficiently covers all black cells with the minimum number of rectangles, ensuring optimal performance for typical grid sizes. The time complexity is O(rows cols (rows + cols)), which is feasible for most grid dimensions.

Example Usage

  • Test Case 1: grid = [[1,1,1],[1,0,1]] → Output: 3
  • Test Case 2: grid = [[1,1,1],[1,0,1],[1,1,1]] → Output:4
  • Test Case3: grid = [[1,1],[1,1]] → Output: 1

These examples confirm the correctness of the approach. The solution handles various grid configurations and provides the minimal number of rectangles needed.

盐城市亿德塑料包装有限公司

盐城市亿德塑料包装有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?