Python HW – 3D Maze
แปะไว้กันลืม เครดิตน้องแฟร์
204111 Computer and Programming
HW – Follow Path 3D
def getMaze( ):
return eval( input( “Enter Maze: ” ) )def getEntrance( ):
return eval( input( “Enter Entrance: ” ) )def isPath( path, maze ):
z = path[0]
y = path[1]
x = path[2]
if z=0and y>=0 and x>=0:
if maze[z][y][x]==’p’ or maze[z][y][x]==’d’ or maze[z][y][x]==’u’ or maze[z][y][x]==’exit’:
return True
return Falsedef nextMove( path, maze ):
z = path[0]
y = path[1]
x = path[2]
if maze[z][y][x]==’p':
if isPath([z,y,x-1],maze):
markPath([z,y,x],maze)
return [z,y,x-1]
elif isPath([z,y,x+1],maze):
markPath([z,y,x],maze)
return [z,y,x+1]
elif isPath([z,y+1,x],maze):
markPath([z,y,x],maze)
return [z,y+1,x]
elif isPath([z,y-1,x],maze):
markPath([z,y,x],maze)
return [z,y-1,x]
elif maze[z][y][x]==’d':
if isPath([z+1,y,x],maze):
markPath([z,y,x],maze)
return [z+1,y,x]
elif maze[z][y][x]==’u':
if isPath([z-1,y,x],maze):
markPath([z,y,x],maze)
return [z-1,y,x]
return 0def isExit( path, maze ):
return maze[path[0]][path[1]][path[2]] == ‘exit’def markPath( path, maze ):
maze[path[0]][path[1]][path[2]] = ‘P’def getPath( entrance, maze ):
path = []
path.append(entrance)
current = nextMove(path[-1],maze)
while current!=0:
path.append(current)
current = nextMove(path[-1],maze)
return pathdef printPath( path ):
for i in range( len( path ) ):
print( “Move %d is %d,%d,%d” %(i+1,path[i][0],path[i][1],path[i][2]))entrance = getEntrance()
maze = getMaze()
path = getPath( entrance, maze )
if isExit(path[-1],maze):
printPath(path)
else:
print(“No exit found”)
