Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive solution in Clear category for Color Map by altarfinch
# migrated from python 2.7
DIRS = {(-1,0),(1,0),(0,-1),(0,1)}
COLORS = {1, 2, 3, 4}
def color_it(neighs,colors,current=0):
if current == len(colors):return colors
for c in COLORS - {colors[n] for n in neighs[current]}:
colors[current] = c
res = color_it(neighs,colors,current+1)
if res:return res
return []
def color_map(region):
countries = max(list(map(max,region)))+1
neighs = [set() for i in range(countries)]
for i, row in enumerate(region):
for j, cell in enumerate(row):
for di, dj in DIRS:
if len(row)>j+dj>=0<=i+di
Feb. 23, 2015
Comments: