老鼠走迷宫问题

 问题描述:
给定一个二维数组,数组中1表示墙壁,0表示通路,由此数组可展示为一个迷宫图。给定入口位置和出口位置,判断之间是否存在通路并显示出走出迷宫的道路。

class Node(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.next = None


class TraceRecode(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, x, y):
        newNode = Node(x, y)
        if self.first == None:
            self.first = newNode
            self.last = newNode
        else:
            self.last.next = newNode
            self.last = newNode

    def delete(self):
        if self.first == None:
            print('队列已经空了')
            return
        newNode = self.first
        while newNode.next != self.last:
            newNode = newNode.next
        newNode.next = self.last.next  # 删除栈顶元素
        self.last = newNode


def chkExit(MAZE, x, y, ex, ey):
    '''
    判断是否为出口,
    条件:上下左右肯定存在一个走过的路(坐标)
    '''
    if x == ex and y == ey:
        if (MAZE[x - 1][y] == 1 or MAZE[x + 1][y] == 1 or MAZE[x][y - 1] == 1 or MAZE[x][y + 1] == 2):
            return 1
        if (MAZE[x - 1][y] == 1 or MAZE[x + 1][y] == 1 or MAZE[x][y - 1] == 2 or MAZE[x][y + 1] == 1):
            return 1
        if (MAZE[x - 1][y] == 1 or MAZE[x + 1][y] == 2 or MAZE[x][y - 1] == 1 or MAZE[x][y + 1] == 1):
            return 1
        if (MAZE[x - 1][y] == 2 or MAZE[x + 1][y] == 1 or MAZE[x][y - 1] == 1 or MAZE[x][y + 1] == 1):
            return 1
    return 0


if __name__ == '__main__':
    Exitx,Exity = 8,10
    # 迷宫,0表示路,1表示墙
    MAZE = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1],
            [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
            [1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1],
            [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
            [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
            [1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]
    path = TraceRecode()
    x, y = 1, 1  # 初始路径
    print('迷宫的路径(0标记的部分)')
    for i in range(10):
        for j in range(12):
            print(MAZE[i][j], end='')
        print()

    while x <= Exitx and y <= Exity:
        MAZE[x][y] = 2  # 走过的路径标注为2
        '''
        判断当前位置的上下左右是否可以移动,如果可以移动,将该位置入栈,若无路可走,则回退到上一个位置,重新检查
        '''
        if MAZE[x - 1][y] == 0:
            # 向上走
            x -= 1
            path.insert(x, y)
        elif MAZE[x][y + 1] == 0:
            # 向右走
            y += 1
            path.insert(x, y)
        elif MAZE[x + 1][y] == 0:
            # 向下走
            x += 1
            path.insert(x, y)
        elif MAZE[x][y - 1] == 0:
            # 向左走
            y -= 1
            path.insert(x, y)
        # 检查是否为出口
        elif chkExit(MAZE, x, y, Exitx, Exity) == 1:
            break
        else:
            # 如果上下左右都不通了,也不是出口,就回退到拐点
            MAZE[x][y] = 2
            path.delete()  # 删除栈顶元素,获得上次添加的坐标
            x = path.last.x
            y = path.last.y
    print('老鼠走过的路径(2标记的部分)')
    for i in range(10):
        for j in range(12):
            print(MAZE[i][j], end='')
        print()

 

你可能感兴趣的:(数据结构,水题)