Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Color Map by Pactp
def color_neighbours(nbs, colors):
try:
i = colors.index(0)
res = []
for c in (1,2,3,4):
if (any(colors[y] == c for (x,y) in nbs if x == i) or
any(colors[y] == c for (y,x) in nbs if x == i)):
pass
else:
res = color_neighbours(nbs, colors[:i]+[c]+colors[i+1:])
if res:
break
return res
except ValueError:
return colors
def color_map(region) -> list[int]:
n, m = len(region), len(region[0])
max_cell = max( map(max,region) )
nbs = {(region[r][c],region[r][c+1])
for r in range(n) for c in range(m - 1)}
nbs|= {(region[r][c],region[r+1][c])
for r in range(n - 1) for c in range(m)}
nbs = {(min(x,y),max(x,y)) for (x,y) in nbs if x != y}
return color_neighbours(nbs, [0]*(max_cell + 1))
May 4, 2023