题解 | #迷宫问题#

迷宫问题

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

import sys




maze_y,maze_x = map(int, input().split())
array = []
for x in range(maze_y):
    array.append(list(map(int, input().split())))


def bfs(start, end):
    quene = [(start,[start])] # 起点和路径
    DELTA = ((1,0),(-1,0),(0,1),(0,-1))
    visited =set()

    if start == end:
        return start

    while quene:
        status, path = quene.pop(0)
        x,y = status
        if status == end:
            return path
        for z in DELTA:
            m_x, m_y = z
            next_x, next_y = x+m_x, y+m_y
            
            if next_x < 0 or next_y < 0  or next_x >(maze_x-1) or next_y>(maze_y-1): #出界
                continue 
            if array[next_y][next_x] == 1: 
                continue
            
            new_status = next_x, next_y
            
            if new_status not in visited:
                visited.add(new_status)
                quene.append((new_status, path+[new_status]))
    return None


r = bfs((0,0),(maze_x-1,maze_y-1))

for x in r:
    print(f'({x[1]},{x[0]})')



全部评论

相关推荐

像好涩一样好学:这公司我也拿过 基本明确周六加班 工资还凑活 另外下次镜头往上点儿
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务