7 条题解

  • 0
    @ 2024-5-24 17:06:56
    n, m = map(int, input().split())
    a = [[0] * (m + 2)]
    a += [[0] + list(map(int, input().split())) + [0] for _ in range(n)]
    a += [[0] * (m + 2)]
    startx, starty = map(int, input().split())
    endx, endy = map(int, input().split())
    
    dirs = [(0, -1), (-1, 0), (0, 1), (1, 0)]
    paths = []
    
    def dfs(x, y, path):
        if (x, y) == (endx, endy):
            paths.append(path[:])
            return
        a[x][y] = 0
        for dx, dy in dirs:
            nx, ny = x + dx, y + dy
            if a[nx][ny] == 1:
                path.append((nx, ny))
                dfs(nx, ny, path)
                path.pop()
        a[x][y] = 1
    
    dfs(startx, starty, [(startx, starty)])
    
    if paths:
        for path in paths:
            print("->".join(f"({x},{y})" for x, y in path))
    else:
        print(-1)
    
    

    信息

    ID
    84
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    775
    已通过
    118
    上传者