我正在学习python并遇到问题,我需要在10x10生成的网格中找到最大的矩形只包含1个。我只能找到宽度,不知道如何找到相应的高度。
import random
import sys
grid=[[0]*12 for _ in range(12)]
def display_grid():
for i in range(1,11):
for j in range(1,11):
print(grid[i][j], end=' ')
print()
def populate_grid():
for i in range(1,11):
for j in range(1,11):
grid[i][j]= int(random.randrange(2)==0)
random.seed(3)
populate_grid()
display_grid()
grid
width=1
list_width=[]
for i in range(1,11):
for j in range(1,11):
if grid[i][j]==grid[i][j+1]==1:
width+=1
list_width.append(width)
else:
width=1
print(list_width)
1 1 0 0 1 1 0 0 1 1
0 0 0 1 1 1 0 1 1 1
1 0 1 0 0 0 0 0 0 1
0 1 1 1 0 1 0 0 0 0
0 0 0 1 0 1 0 1 0 1
1 0 0 1 1 0 0 1 0 1
0 1 1 0 0 0 1 1 1 0
0 0 1 1 0 1 1 1 1 1
0 0 0 1 1 0 0 0 1 0
0 0 0 1 0 0 1 0 0 0
[2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 2, 3, 4, 5, 2]
非常感谢任何帮助。