Python 3.x BFS准备好兔子逃跑 提示

Python 3.x BFS准备好兔子逃跑 提示,python-3.x,python-2.7,search,multidimensional-array,breadth-first-search,Python 3.x,Python 2.7,Search,Multidimensional Array,Breadth First Search,你有空间站部分的地图,每个都从监狱出口开始,到逃生舱的门为止。地图表示为0和1的矩阵,其中0是可通行的空间,1是不可通行的墙。出监狱的门在左上角(0,0),进入逃生舱的门在右下角(w-1,h-1) 写一个函数答案(地图),生成从监狱门到逃生舱的最短路径长度,在那里你可以拆除一面墙,作为重建计划的一部分。路径长度是您通过的节点总数,包括入口和出口节点。起始和结束位置始终可以通过(0)。地图始终是可解的,尽管您可能需要也可能不需要移除墙。地图的高度和宽度可以从2到20。移动只能在基本方向上进行;不允

你有空间站部分的地图,每个都从监狱出口开始,到逃生舱的门为止。地图表示为0和1的矩阵,其中0是可通行的空间,1是不可通行的墙。出监狱的门在左上角(0,0),进入逃生舱的门在右下角(w-1,h-1)

写一个函数答案(地图),生成从监狱门到逃生舱的最短路径长度,在那里你可以拆除一面墙,作为重建计划的一部分。路径长度是您通过的节点总数,包括入口和出口节点。起始和结束位置始终可以通过(0)。地图始终是可解的,尽管您可能需要也可能不需要移除墙。地图的高度和宽度可以从2到20。移动只能在基本方向上进行;不允许对角移动

测试用例 我的代码
从集合导入数据
def溶液(map):
H=len(映射)
W=len(映射[0])
开始=((0,0),False)
轨道={
(0, 0): (-1, -1)
}
to_explore=deque([开始])
访问=设置()
w、 h=0,0
而不是(w==w-1和h==h-1):
current=to_explore.popleft()
打开的位置=当前位置
邻居=获取邻居(pos)
零,一=检查邻域(邻域,映射,(W,H))
对于零中的节点:
如果不在轨迹中的节点和未访问的节点:
形式_顶点=(节点,打开)
添加(形成顶点)
跟踪[节点]=位置
对于1中的节点:
如果节点不在轨道中,且节点不在已访问且未打开:
形式_顶点=(节点,真)
添加(形成顶点)
跟踪[节点]=位置
w、 h=位置
已访问。添加(pos)
路径=[]
电流=(W-1,H-1)
当前时!=(-1, -1):
path.append(当前)
当前=轨道[当前]
返回len(路径)
def get_邻居(pos):
w、 h=位置
返回[(w+1,h)、(w-1,h)、(w,h+1)、(w,h-1)]
def检查_邻居(邻居、网格、边缘):
零=[]
1=[]
W、 H=边缘
对于邻居中的邻居:
w、 h=邻居

如果0,你能解释一下“谷歌隐藏的测试用例”是什么意思吗?@RichardHunter是的。所以我们(用户)知道一些测试用例的输入,但是,有些测试用例的输入是隐藏的。我认为这是为了防止人们使用硬编码解决方案,但缺点是我不能确切地说出我的代码遇到了什么错误/bug。
maze = [
    [0, 0, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 0],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 1, 1, 1, 1, 0, 0],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0]
]
#15

maze = [
    [0, 0, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 0],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0]
]
#21

maze = [
    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
#39

maze = [
    [0, 1, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 1, 0]
]
#10
from collections import deque


def solution(map):
    H = len(map)
    W = len(map[0])
    start = ((0, 0), False)

    track = {
        (0, 0): (-1, -1)
    }
    to_explore = deque([start])
    visited = set()
    w, h = 0, 0
    while not (w == W-1 and h == H-1):
        current = to_explore.popleft()
        pos, opened = current
        neighbours = get_neighbours(pos)
        zeroes, ones = check_neighbours(neighbours, map, (W, H))
        for node in zeroes:
            if not node in track and node not in visited:
                form_vertex = (node, opened)
                to_explore.append(form_vertex)
                track[node] = pos
        for node in ones:
            if not node in track and node not in visited and not opened:
                form_vertex = (node, True)
                to_explore.append(form_vertex)
                track[node] = pos
        w, h = pos
        visited.add(pos)

    path = []
    current = (W - 1, H - 1)
    while current != (-1, -1):
        path.append(current)
        current = track[current]
    return len(path)


def get_neighbours(pos):
    w, h = pos
    return [(w+1, h), (w-1, h), (w, h+1), (w, h-1)]


def check_neighbours(neighbours, grid, edge):
    zeroes = []
    ones = []
    W, H = edge
    for neighbour in neighbours:
        w, h = neighbour
        if 0 <= w < W and 0 <= h < H:
            value = grid[h][w]
            if value == 0:
                zeroes.append(neighbour)
            else:
                ones.append(neighbour)

    return zeroes, ones