Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive (and ugly) solution in Uncategorized category for Haunted House by Juge_Ti
visited = []
def distance(a, b):
return (((b-1)//4 - (a-1)//4)**2 + ((b-1)%4 - (a-1)%4)**2) ** 0.5
def escape(house, stephen, ghost, done):
if stephen == ghost:
return 'Aaaaaaarrrrgghhhhh!'
d = {'N': -4, 'W': -1, 'E': 1, 'S': 4}
for stephens_move in {'N', 'W', 'E', 'S'} - set(house[stephen-1]):
newstephen = stephen + d[stephens_move]
if newstephen == 1:
return stephens_move
dmin = 10
for move in {'N', 'W', 'E', 'S'} - set(house[ghost-1]):
dist = distance(ghost + d[move], newstephen)
if dist < dmin:
ghosts_move = move
dmin = dist
newghost = ghost + d[ghosts_move]
if (newstephen, newghost) not in done:
newdone = done + [(newstephen, newghost)]
x = escape(house, newstephen, newghost, newdone)
if x != 'Aaaaaaarrrrgghhhhh!':
return x + stephens_move
return 'Aaaaaaarrrrgghhhhh!'
def checkio(house, stephen, ghost):
global visited
if stephen == 1:
return 'N'
visited.append((stephen, ghost))
for i in range(4):
house[i] += 'N'
house[12+i] += 'S'
house[4*i] += 'W'
house[4*i+3] += 'E'
return escape(house, stephen, ghost, visited)[-1]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
checkio(
["", "S", "S", "",
"E", "NW", "NS", "",
"E", "WS", "NS", "",
"", "N", "N", ""],
16, 1)
checkio(
["", "", "", "",
"E", "ESW", "ESW", "W",
"E", "ENW", "ENW", "W",
"", "", "", ""],
11, 6)
Nov. 6, 2013
Comments: