题解 | 迷宫问题

迷宫问题

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

import copy
# 接受输入
h, w = map(int, input().split())
maps = []
for i in range(h):
    maps.append(list(map(int, input().split())))
# 定义回归迭代
def next_step(last_step, now_step, maps):
    h = len(maps) - 1
    w = len(maps[0]) - 1
    if now_step == [h, w]:
        return True
    next_xy = copy.copy(now_step)
    # 分四类-1
    next_xy[0] = now_step[0] - 1  # 向上走一步
    if next_xy[0] < 0 or next_xy[0] == last_step[0]:
        pass  # 超边界或返货上一步,直接跳过
    elif maps[next_xy[0]][next_xy[1]] == 1:
        pass  # 碰墙壁
    elif next_step(now_step, next_xy, maps):
        return True
    # 分四类-2
    next_xy = copy.copy(now_step)
    next_xy[0] = now_step[0] + 1  # 向下走一步
    if next_xy[0] > h or next_xy[0] == last_step[0]:
        pass  # 超边界或返货上一步,直接跳过
    elif maps[next_xy[0]][next_xy[1]] == 1:
        pass  # 碰墙壁
    elif next_step(now_step, next_xy, maps):
        return True
    # 分四类-3
    next_xy = copy.copy(now_step)
    next_xy[1] = now_step[1] - 1  # 向左走一步
    if next_xy[1] < 0 or next_xy[1] == last_step[1]:
        pass  # 超边界或返货上一步,直接跳过
    elif maps[next_xy[0]][next_xy[1]] == 1:
        pass  # 碰墙壁
    elif next_step(now_step, next_xy, maps):
        return True
    # 分四类-4
    next_xy = copy.copy(now_step)
    next_xy[1] = now_step[1] + 1  # 向右一步
    if next_xy[1] > w or next_xy[1] == last_step[1]:
        pass  # 超边界或返货上一步,直接跳过
    elif maps[next_xy[0]][next_xy[1]] == 1:
        pass  # 碰墙壁
    elif next_step(now_step, next_xy, maps):
        return True
    # 无路可走返回
    return False

# 主函数
steps = []  # 初始化集合
last_step = [0, 0]  # 初始化第一步
steps.append(last_step)
h -= 1
w -= 1
while 1:
    # 分四个方向前进
    now_step = copy.copy(last_step)
    # 方向一,向上一步
    now_step[0] -= 1
    if now_step[0] < 0:
        pass
    elif maps[now_step[0]][now_step[1]] == 1:
        pass
    elif next_step(last_step, now_step, maps):
        steps.append(now_step)
    # 方向二,向左一步
    now_step = copy.copy(last_step)
    now_step[1] -= 1
    if now_step[1] < 0:
        pass
    elif maps[now_step[0]][now_step[1]] == 1:
        pass
    elif next_step(last_step, now_step, maps):
        steps.append(now_step)
    # 方向三,向下一步
    now_step = copy.copy(last_step)
    now_step[0] += 1
    if now_step == [h, w]:
        steps.append(now_step)
        break
    if now_step[0] > h:
        pass
    elif maps[now_step[0]][now_step[1]] == 1:
        pass
    elif next_step(last_step, now_step, maps):
        steps.append(now_step)
    # 方向四,向右一步
    now_step = copy.copy(last_step)
    now_step[1] += 1
    if now_step == [h, w]:
        steps.append(now_step)
        break
    if now_step[1] > w:
        pass
    elif maps[now_step[0]][now_step[1]] == 1:
        pass
    elif next_step(last_step, now_step, maps):
        steps.append(now_step)
    last_step = copy.copy(steps[-1])
# 结果输出
for i in range(len(steps)):
    print('('+str(steps[i][0])+','+str(steps[i][1])+')')

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
爱睡觉的冰箱哥:学历不够啊兄弟,分析师都是9硕✌🏻咱搞不过他们滴
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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