走迷宫

来源:互联网 发布:飞了网络用语什么意思 编辑:程序博客网 时间:2024/05/01 21:01

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

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

“Nonsense. The real adventure is out there!” She 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 player 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.



先用BFS实现以下(note:输出的路径能走通就行,并不是最短路径  one of possible)

def BFS(labyrinth):    sX,sY = 1,1    eX,eY = 10,10    paths = [(sX,sY,'')]    visited = [(sX,sY)]    while len(paths):        x,y,p = paths.pop(0)        if x==eX and y==eY:            return p        for i,j,s in [(x+1,y,'S'),(x-1,y,'N'),(x,y+1,'E'),(x,y-1,'W')]:            if labyrinth[i][j] != 1 and (i,j) not in visited: # the borders are always 1                visited.append((i,j))                paths.append((i,j,p+s))print(BFS([        [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]]))

下面是DFS搜索

maps = ''#1,1#10,10#NSWEg_end=(10,10)g_visit=[[0]*12 for c in range(12)]g_visit[1][1]=1# start point#path=['']*12*12flag=Truedef checkio1(x,y,depath):    #find end    global  queue    global  flag    global maps    if (x,y)==g_end:        flag=False        return    #south    if not maps[x+1][y] and not g_visit[x+1][y] and x+1<12 and flag:        path[depath+1]='S'        g_visit[x+1][y]=1        checkio1(x+1,y,depath+1)        g_visit[x+1][y]=0    #East    if not maps[x][y+1] and not g_visit[x][y+1] and  y+1<12 and flag:        path[depath+1]='E'        g_visit[x][y+1]=1        checkio1(x,y+1,depath+1)        g_visit[x][y+1]=0    #North    if not maps[x-1][y ] and not g_visit[x-1][y ] and x-1>=0 and flag:        path[depath+1] = 'N'        g_visit[x-1][y] = 1        checkio1(x-1, y, depath + 1)        g_visit[x-1][y] = 0    #West    if not maps[x][y-1] and not g_visit[x][y-1] and y-1>=0 and flag:        path[depath+1] = 'W'        g_visit[x][y-1] = 1        checkio1(x, y-1, depath + 1)        g_visit[x][y-1] = 0    else:      pass        def checkio(data):    global  maps    maps=data    global queue    checkio1(1,1,0)    str=''    for c in queue:        if c:            str+=c;    return strprint(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]]))




原创粉丝点击