Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Radiation Search by Sim0000
from collections import deque
def area(x, y, matrix):
if matrix[y][x] == 0: return [0, 0]
mx, my = len(matrix[0]), len(matrix)
n = matrix[y][x]
q = deque([(x, y)])
count = 0
while len(q) != 0:
x, y = q.popleft()
if matrix[y][x] != n: continue
matrix[y][x] = 0
count += 1
for dx, dy in ((-1,0), (1,0), (0,-1), (0,1)):
x1, y1 = x + dx, y + dy
if 0 <= x1 < mx and 0 <= y1 < my: q.append((x1, y1))
return [count, n]
def checkio(matrix):
return max(area(x, y, matrix) for y in range(len(matrix)) for x in range(len(matrix[0])))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
[1, 2, 3, 4, 5],
[1, 1, 1, 2, 3],
[1, 1, 1, 2, 2],
[1, 2, 2, 2, 1],
[1, 1, 1, 1, 1]
]) == [14, 1], "14 of 1"
assert checkio([
[2, 1, 2, 2, 2, 4],
[2, 5, 2, 2, 2, 2],
[2, 5, 4, 2, 2, 2],
[2, 5, 2, 2, 4, 2],
[2, 4, 2, 2, 2, 2],
[2, 2, 4, 4, 2, 2]
]) == [19, 2], '19 of 2'
March 1, 2014
Comments: