最大1的矩形在二进制矩阵中的位置

时间:2018-11-12 18:36:04

标签: histogram binary-matrix

我一直在尝试解决一个问题,以找到负整数中最大负矩形的位置(ytopxtopybotxbot)整数矩阵。我将其视为在直方图中找到最大区域。问题是这样的:对于某些输入,它返回正确的结果,但是对于某些(较大的)输入,结果将向左移动一列-两个x坐标都比应有的少一。如果有人能在我的代码甚至注释中发现错误,我将感到非常高兴。

这是纽约代码:

from sys import argv
#text = argv[1]
text = "text2.txt"
# Finds the largest submatrix with only negative integers in a given matrix, 
returns coordinates of its top left and bottom right corner.

def max_area(histogram):
# Finds the largest area in a histogram.
    area = 0
    stack = [-1]
    for i in range(len(histogram)):
        # Stores indeces of an ascending set of bars.
        while stack[-1] != -1 and histogram[stack[-1]] >= histogram[i]:
           # Finds a bar that is lower than the previous one, gets all 
           # possible areas from the bars before this (excluding).
            last_element_index = stack.pop()
            if area < histogram[last_element_index] * (i - stack[-1] - 1):
                # The current area.
                area = histogram[last_element_index] * (i - stack[-1] - 1)
                # Index of the rightmost bar (from current area) = x of 
                bottom right corner.
                x_bot = last_element_index
                # Value (height) of the rightmost bar = height of the area.
                height = histogram[last_element_index]
                # Index of current bar - 1 = the last ascending bar, - 
                # stack[-1] = amount of bars between <-- and the bar that's
                # being looked at.
                width = i - stack[-1] - 1
        stack.append(i)
    while stack[-1] != -1:
        last_element_index = stack.pop()
        if area < histogram[last_element_index] * (i - stack[-1] - 1):
            area = histogram[last_element_index] * (i - stack[-1] - 1)
            x_bot = last_element_index + 1
            width = i - stack[-1] - 1
    return area, x_bot, height, width


large_location  = [0, 0, 0, 0]  # ytop, xtop, ybot, xbot
large_size = 0
with open(text) as file:
    row = file.readline().split()
    histogram = []
    row_number = 0
    # Creates the 1st histogram, to know its lenght. Stores positive and 
    # negative integers as 0's and 1's.
    for number in row:
        if number[0] == "-":
            histogram.append(1)
        else:
            histogram.append(0)
    large_size, large_location[3], height, width = max_area(histogram) 
    # size, xbot, height, width
    large_location[2] = row_number  # ybot
    large_location[0] = large_location[2] - height + 1  # ytop
    large_location[1] = large_location[3] - width + 1  # xtop

    for row in file:
    # Stores the index of the current row, used for the bottom y coordinate.
        row_number += 1
        aux = row.split()
    # For every row in the matrix, if a number is positive sets the 
    # corresponding value inthe histogram to 0,
    # else adds 1. The histogram always represents all the uninterupted 
    # negative integers in rows 0 to current.
        for i in range(len(aux)):
            if aux[i][0] == "-":
                histogram[i] += 1
            else:
                histogram[i] = 0
    # Gets the max area of the current histogram, compares it to the 
    # previous best result, stores it when neccessary.
        current_size = max_area(histogram)
        if current_size[0] > large_size:
            large_size, large_location[3], height, width = current_size
            large_location[2] = row_number
            large_location[0] = large_location[2] - height + 1
            large_location[1] = large_location[3] - width + 1

print(large_location[0], large_location[1])
print(large_location[2], large_location[3])
#print("size:", large_size, "height:", height, "width:", width)

这将获得正确的结果[1, 2, 3, 3]

 1  -9   -2    8    6   1
 8  -1  -11   -7    6   4
 10 12   -1   -9  -12  14
 8  10   -3   -5   17   8
 6   4   10  -13  -16  19

我不想发布一个不好的例子,因为它有50多个列,但是结果看起来像这样[11, 33, 23, 51]而不是[11, 34, 23, 52]

0 个答案:

没有答案