checkio -- Open Labyrinth python BFS的使用


Stephen and Sofia wander off for a moment, taking a break from the packing and piling and fixing of things on the ship. By accident they wander by an Open Labyrinth they had never seen before on their home island.

“Look, there’s plenty of adventure right here at home.” exclaimed Stephen. “Are you really sure you want to leave?”

“Nonsense. The real adventure is out there!” Sofia responded while waving to the sky. “But, maybe we can conquer this little challenge right here before we lift off into the unknown...”

The labyrinth has no walls, but pits surround the path on each side. If a players falls into a pit, they lose. The labyrinth is presented as a matrix (a list of lists): 1 is a pit and 0 is part of the path. The labyrinth's size is 12 x 12 and the outer cells are also pits. Players start at cell (1,1). The exit is at cell (10,10). You need to find a route through the labyrinth. Players can move in only four directions--South (down [1,0]), North (up [-1,0]), East (right [0,1]), West (left [0, -1]). The route is described as a string consisting of different characters: "S"=South, "N"=North, "E"=East, and "W"=West.

Input: A labyrinth map as a list of lists with 1 and 0.

Output: The route as a string that contains "W", "E", "N" and "S". 



Example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
checkio([
     [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ],
     [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ],
     [ 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ],
     [ 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ],
     [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 ],
     [ 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ],
     [ 1 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 ],
     [ 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 1 ],
     [ 1 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 ],
     [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 1 ],
     [ 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 ],
     [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]])



Open Labyrinth  
def checkio(maze):
    vis=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    direction=[[0,-1,'W'],[0,1,'E'],[-1,0,'N'],[1,0,'S']]
    Node=[1,1,""]
    q=[]
    q.append(Node)
    while len(q)>0:
        n=q[0]
        del q[0]
        if n[0]==10 and n[1]==10:
            return n[2]
        vis[n[0]][n[1]]=1
        for l in direction:
            x=n[0]+l[0]
            y=n[1]+l[1]
            print x,y,n[2]
            if (x > 0 and y >0 and x <12 and y <12 and vis[x][y]==0 and maze[x][y]==0):
                N=[x,y,n[2]+l[2]]
                vis[x][y]=1
                q.append(N)
    return -1


你可能感兴趣的:(checkio -- Open Labyrinth python BFS的使用)