Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
3 steps solution in Clear category for Safe Coasts by DiZ
#3 steps:
#- find direct safe cases (8-neighbours of X-cases)
#- propagate D-cases (amoung 4-neighbours)
#- fill unmarked cases with S-cases
def finish_map(land_m):
N = [(1,0),(0,1),(-1,0),(0,-1), (1,1),(1,-1),(-1,1),(-1,-1)]
land, land_map = {'X':set(), 'D':set(), '.':set()}, list(map(list, land_m))
for x,line in enumerate(land_map):
for y,cell in enumerate(line):
land[cell].add((x,y))
def traverse(explore, neighbours, result='S'):
bag = land[explore]
while bag:
x,y = bag.pop()
if explore != 'X':
land_map[x][y] = result
for i,j in N[:neighbours]:
xn, yn = x+i, y+j
if (xn,yn) in land['.']:
land_map[xn][yn] = result
land['.'].remove((xn, yn))
if explore == result: bag.add((xn, yn))
for args in (('X',8), ('D',4,'D'),('.',0)): traverse(*args)
return [''.join(line) for line in land_map]
Aug. 29, 2014
Comments: