Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Color Map solution in Clear category for Color Map by przemyslaw.daniel
def get_neighbors(region):
h, w = len(region), len(region[0])
out = [set() for _ in set([x for y in region for x in y])]
for i, j in __import__('itertools').product(range(h), range(w)):
for x, y in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
if not (0 <= x < h and 0 <= y < w):
continue
a, b = region[i][j], region[x][y]
out[a] |= {b} if a != b else set()
out[b] |= {a} if a != b else set()
return out
def color_map(region):
neigh = get_neighbors(region)
stack = [(1, [1]+[-1]*(len(neigh)-1))]
while stack:
cnt, cols = stack.pop()
if any([cols[x] == cols[cnt-1] for x in neigh[cnt-1]]):
continue
if cnt == len(neigh): return cols
for i in [4, 3, 2, 1]:
cols[cnt] = i
stack += [(cnt+1, cols[:])]
Jan. 25, 2017
Comments: