题解 | #迷宫问题#

迷宫问题

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]})')



全部评论

相关推荐

09-03 17:49
已编辑
四川大学 供应链管理
迷茫的大四🐶:提前实习有些坑的,卡毕业后薪资以及能力不达预期提前违约这种
我的OC时间线
点赞 评论 收藏
分享
昨天 17:43
已编辑
北京化工大学 硬件开发
易才一飞:感觉项目写细节一些吧,掌握技能和校内经历感觉占比太大,而且这是找嵌软还是硬件呢,似乎大家都说要有针对的写相关技术才好吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务