题解 | #迷宫问题#

迷宫问题

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

def fun(i, j, ls, tup):
    if i==m-1 and j==n-1:
        for x in ls:
            print("(%s,%s)"%(x[0],x[1]))
        return
    if (i, j - 1) != tup and j - 1 >= 0 and dp[i][j - 1] == "0":
        l = ls[:]
        l.append((i, j - 1))
        fun(i, j - 1, l, (i, j))
    if (i, j + 1) != tup and j + 1 <= n - 1 and dp[i][j + 1] == "0":
        l = ls[:]
        l.append((i, j + 1))
        fun(i, j + 1, l, (i, j))
    if (i - 1, j) != tup and i - 1 >= 0 and dp[i - 1][j] == "0":
        l = ls[:]
        l.append((i - 1, j))
        fun(i - 1, j, l, (i, j))
    if (i + 1, j) != tup and i + 1 <= m-1 and dp[i + 1][j] == "0":
        l = ls[:]
        l.append((i + 1, j))
        fun(i + 1, j, l, (i, j))


l = input().split()
m, n = int(l[0]), int(l[1])
dp = [input().split() for _ in range(m)]
fun(0,0,[(0,0)],(0,0))

全部评论

相关推荐

AFBUFYGRFHJLP:直接去美帝试试看全奖phd吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务