Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Path finding solution in Uncategorized category for Haunted House by Amachua
# migrated from python 2.7
from math import sqrt
DIRECTIONS={"N":-4, "W":-1, "E":1, "S":4}
GOAL=0
# Get the ghost next position.
# It doesn't give the right result all the time because in case of equal distance the next position is chosen randomly.
def getGhostNextPosition(h, s, g):
vmin, minDist = 0, float("Inf")
for d, v in DIRECTIONS.items():
if d not in h[g]:
tmp = sqrt((s%4-(g+v)%4)**2+(s//4-(g+v)//4)**2)
if tmp < minDist:
vmin, minDist = v, tmp
return vmin
def checkio(h, s, g):
s, g = s-1, g-1
# If the position is already the goal, leave the house.
if s == GOAL: return "N"
pos, visited = [[s, g, ""]], []
# Add the closed door in the map.
for i in range(4):
h[i]+="N"
h[12+i]+="S"
h[i*4]+="W"
h[i*4+3]+="E"
# Search the best path.
while True:
# Pop the new position.
s, g, path = current = pos.pop(0)
# If the new position has already been visited => Continue
if current in visited: continue
visited.append(current)
# Check all the doors of the current room.
for d, v in DIRECTIONS.items():
if not d in h[s]:
# If the door is unlock we change compute the new position of Stephens and of the Ghost.
# The ghost and Stephens position must be different to be added to the possible position.
# If the goal is reached, return the first path position.
s1 = s+DIRECTIONS[d]
g1 = g+getGhostNextPosition(h, s1, g)
if s1 == g1: continue
if s1 == GOAL: return (path+d)[0]
pos.append([s1,g1,path+d])
Nov. 5, 2013
Comments: