Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Radiation Search by saklar13
def checkio(matrix):
groups = []
for y, line in enumerate(matrix):
for x, value in enumerate(line):
for group in groups:
if (y, x) in group: break
else:
groups.append(find_group(x, y, value, matrix))
groups.sort(key=len)
y, x = groups[-1][0]
value = matrix[y][x]
size = len(groups[-1])
return [size, value]
def find_group(x, y, value, matrix):
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
group = []
to_visit = [(y, x)]
while to_visit:
y, x = to_visit.pop()
if (y, x) in group: continue
group.append((y, x))
for i, j in directions:
xi, yj = x + i, y + j
if (yj, xi) in group: continue
if 0 <= xi < len(matrix[0]) and 0 <= yj< len(matrix):
if matrix[yj][xi] == value:
to_visit.append((yj, xi))
return group
June 10, 2015