Foobar-Escape Pods问题我的代码未通过所有测试

时间:2019-07-03 08:23:42

标签: python arrays python-3.x google-foobar

我正在尝试完成google-foobar挑战#4-逃生吊舱,我的代码通过了google-foobar上4个测试用例中的3个,我不确定我的代码有什么问题。这是问题。

编写一个函数解决方案(入口,出口,路径),该函数需要一个整数数组,该整数数组表示聚集的兔子组所在的位置;一个整数数组,表示转义吊舱的位置;以及一个整数数组,整数的数组表示走廊,以整数形式返回可以在每个时间步通过的兔子的总数。入口和出口不相交,因此永远不会重叠。路径元素path [A] [B] = C描述了从A到B的走廊在每个时间步都可以容纳C个兔子。走廊最多连接50个房间,一次最多容纳200万只兔子。

例如,如果您拥有:

entrances = [0, 1]
exits = [4, 5]
path = [
  [0, 0, 4, 6, 0, 0],  # Room 0: Bunnies
  [0, 0, 5, 2, 0, 0],  # Room 1: Bunnies
  [0, 0, 0, 0, 4, 4],  # Room 2: Intermediate room
  [0, 0, 0, 0, 6, 6],  # Room 3: Intermediate room
  [0, 0, 0, 0, 0, 0],  # Room 4: Escape pods
  [0, 0, 0, 0, 0, 0],  # Room 5: Escape pods
]

然后在每个时间步中可能发生以下情况: 0将4/4兔子发送给2,将6/6兔子发送给3 1将4/5兔子发送给2,将2/2兔子发送给3 2将4/4兔子发送给4,将4/4兔子发送给5 3将4/6兔子发送给4,将4/6兔子发送给5

因此,总共有16个兔子可以在每个时间步长分别在4和5到达逃生舱。 (请注意,在此示例中,第3会议室可以将8个兔子的任何变体发送给4和5,例如2/6和6/6,但最终解决方案保持不变。)

我试图多次重写代码,但是我不明白自己在做什么错。

def solution(entrances, exits, layout):
    exit_rooms = layout[exits[0]:]

    new_layout = []
    for i in layout:
        temp = []
        for item in i:
            if item != 0:
                temp.append(item)
        new_layout.append(temp)

    entrance = sorted(new_layout[:entrances[-1]+1])[::-1]

    rooms_to_remove = sorted(entrances + exits)[::-1]
    for i in rooms_to_remove:
        del new_layout[i]

    intermediate_rooms = new_layout

    max_exit_room = []
    for pods in exit_rooms:
        max_exit_room.append(max(pods))
    max_exit_room = max(max_exit_room)

    count = 0
    while True:
        max_room_switch = len(intermediate_rooms)
        current_room = 0 # switch intermediate rooms
        for room in entrance:
            xroom = room
            # if the number of intermediate rooms are less than number of bunnies in rooms
            if (max_room_switch != len(room) and len(room) > 1):
                xroom = sorted(room)
                xroom.remove(min(room))

            for bunnies in xroom:
                max_room = max(intermediate_rooms[current_room])
                n = max_room - bunnies

                if n <= 0:
                    count += max_room
                elif n > 0:
                    count += bunnies

                # room swtiching
                if max_room_switch > 1:
                    if max_room_switch == current_room:
                        break
                    else:
                        if current_room == max_room_switch-1:
                            current_room = 0
                        else:
                            current_room += 1

        break
    # print(count)
    return count

对于前两个给定的测试,代码通过(6,16),对于最后一个,答案为935

1 个答案:

答案 0 :(得分:0)

我希望你能解决这个问题。无论如何,让我发布该问题的可能解决方案:

请注意,在每个中间房间级别,房间的索引是兔子数的索引。例如。对于中间房间2,穿过房间2的兔子为“ 4 + 5”,这些值分别在兔子列表中的索引2处。因此,我们可以使用它草拟一个可能的解决方案:

def solution(entrances, exits, path):
    le = len(entrances)
    lp = len(path)
    lx = len(exits)
    bunn_count = 0
    inter_paths = path[le:(lp-lx)]                # To find all intermediate rooms
    for i in range(lp - le - lx):                 # Loop through range of length of intermediate rooms
        sum_range = sum(inter_paths[i])           # Sum of an intermediate room's possible number of bunnies allowed
        sum_enter = 0                             # Sum of bunnies that enter that room
        for j in entrances:
            sum_enter += path[j][le + i]          # Get all bunnies that enter a room
        bunn_count += min(sum_enter, sum_range)
    return bunn_count

注意:这只是我针对该问题的解决方案。也许您可以参考https://surajshetiya.github.io/Google-foobar/https://vitaminac.github.io/Google-Foobar-Escape-Pods/以获得更好的解决方案。