题解 | #迷宫问题#

迷宫问题

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
全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务