0
点赞
收藏
分享

微信扫一扫

最大矩形问题【python实现】

zibianqu 2022-04-23 阅读 33

实现方式

import numpy as np


def oneId(matrix): # 获取1的坐标
    oneid = []
    matrix = matrix
    print(matrix)
    height, width = len(matrix), len(matrix[0])
    for rowid, row in enumerate(matrix):
        for colid, num in enumerate(row):
            if num == 1:
                oneid.append([rowid, colid])
                # print([rowid,colid])
    return oneid


def recTangle(seed): # 以种子为左上角,搜索最大矩形
    h, w = 1, 1
    p = [seed[0], seed[1]]
    while p[1] + 1 <= len(matrix[0]) - 1 and matrix[p[0], p[1] + 1] == 1:
        w += 1
        p[1] += 1
    q = [seed[0], seed[1]]
    while q[0] + 1 <= len(matrix) - 1 and matrix[q[0] + 1, q[1]] == 1:
        h += 1
        q[0] += 1
    while ((matrix[seed[0]:seed[0] + h, seed[1]:seed[1] + w]) == 0).any():
        if h >= w:
            h -= 1
        if h < w:
            w -= 1
    square = matrix[seed[0]:seed[0] + h, seed[1]:seed[1] + w]
    # print(h, w, '\n', square)
    # print((square == 1).all())
    # print("--" * 30)
    return w * h


matrix = np.random.randint(0, 2, size=(10, 10))
# print([seed for seed in oneId(matrix)])
oneid = oneId(matrix)
maxarea = 0
for seed in oneid:
    area = recTangle(seed)
    if area > maxarea:
        maxarea = area

print(maxarea)

result

[[0 0 0 1 0 1 0 1 0 0]
 [1 1 0 1 0 1 0 1 1 0]
 [1 1 0 0 1 0 1 0 1 0]
 [1 1 0 0 0 0 0 0 0 0]
 [0 0 0 1 0 0 1 1 0 0]
 [1 0 1 1 1 1 0 1 0 0]
 [0 1 0 1 0 0 1 0 0 0]
 [0 0 1 0 0 1 1 0 0 0]
 [0 0 1 0 0 0 1 0 1 1]
 [0 1 1 1 0 0 0 1 0 1]]
6
举报

相关推荐

0 条评论