题解 | #迷宫问题#

迷宫问题

http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

while True:
    try:
        n1, n2 = [int(x) for x in input().split(' ')]
        maze = []
        for i in range(n1+2):
            if i == 0 or i == n1+1:
                maze.append([1 for x in range(n2+2)])
            else:
                temp_lst = [int(x) for x in input().split(' ')]
                temp_lst.insert(0, 1)
                temp_lst.insert(n2+1, 1)
                maze.append(temp_lst)

        # print(maze)
        start = (1,1)
        end = (n1,n2)
        lst = [start]

        while lst:
            now = lst[-1]
        #     print(now)
            if now == end:
                for x in lst:
                    i, j = x
                    x = (i-1,j-1)
                    print('(%d,%d)'%(i-1,j-1))
                break

            row, col = now
            maze[row][col] = 2

            if maze[row - 1][col] == 0:
        #         print('up')
                lst.append((row - 1, col))
                continue
            elif maze[row + 1][col] == 0:
        #         print('down')
                lst.append((row + 1, col))
                continue   
            elif maze[row][col + 1] == 0:
        #         print('right')
                lst.append((row, col + 1))
                continue   
            elif maze[row][col - 1]==0:
        #         print('left')
                lst.append((row, col - 1))
                continue
            else:
                 lst.pop()
        else:
            print('This is a maza that can not be finished')
    except:
        break
全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务