-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectSolidSquare.py
27 lines (24 loc) · 1.07 KB
/
detectSolidSquare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
matrix = [[1, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0, 1],
[0, 0, 1, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 1]]
def max_size(mat, ZERO=0):
"""Find the largest square of ZERO's in the matrix `mat`."""
nrows, ncols = len(mat), (len(mat[0]) if mat else 0)
if not (nrows and ncols): return 0 # empty matrix or rows
counts = [[0]*ncols for _ in xrange(nrows)]
for i in reversed(xrange(nrows)): # for each row
assert len(mat[i]) == ncols # matrix must be square
for j in reversed(xrange(ncols)): # for each element in the row
if mat[i][j] != ZERO:
counts[i][j] = (1 + min(
counts[i][j+1], # east
counts[i+1][j], # south
counts[i+1][j+1] # south-east
)) if i < (nrows - 1) and j < (ncols - 1) else 1 # edges
print max(c for rows in counts for c in rows)
return max(c for rows in counts for c in rows)
max_size(matrix,0)