python - 逃生舱 Google Foobar 挑战 |最大流量问题

标签 python arrays algorithm depth-first-search max-flow

我处于 google foobar 挑战赛的第四级。我面临着这个问题的问题。我已经在提供的测试用例上尝试了我的解决方案,但是谷歌的等级检查器显示我的代码对于这些测试用例也是错误的。有谁能够帮助我?我到底哪里错了?

问题

逃生舱

给定兔子组的起始房间号、逃生舱的房间号,以及每条走廊之间的每个方向一次可以穿过多少只兔子,算出有多少只兔子可以安全通过在高峰期前往逃生舱。

编写一个函数解决方案(入口、导出、路径),它采用一个整数数组(表示聚集的兔子组所在的位置)、一个整数数组(表示逃生舱的位置)以及一个由整数数组组成的数组走廊,以 int 形式返回每个时间步可以通过的兔子总数。入口和导出是不相交的,因此永远不会重叠。路径元素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,但最终解决方案保持不变。)

测试用例

您的代码应该通过以下测试用例。 请注意,它也可能针对此处未显示的隐藏测试用例运行。

-- Python 案例 -- 输入:

solution.solution([0], [3], [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]])

输出:

6

输入:

solution.solution([0, 1], [4, 5], [[0, 0, 4, 6, 0, 0], [0, 0, 5, 2, 0, 0], [0, 0, 0, 0, 4, 4], [0, 0, 0, 0, 6, 6], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])

输出:

16

我的解决方案

def get_new_path(entrances,exits,path):
    for p in path:
        p.insert(0, 0)
        p.append(0)
    new_length = len(path[0])
    path.insert(0,[0]* new_length)
    path.append([0]* new_length)
    for e in entrances:
        path[0][e+1] = 2e+6
    for e in exits:
        path[e+1][new_length-1] =  2e+6
    return path

def get_route(stack, open, path):
    next = stack[-1]
    list_of_nexts = [i for i,val in enumerate(path[next]) if ( i not in open and val != 0)]
    for i in list_of_nexts:
        open.add(i)
    if len(path)-1 in list_of_nexts:
        stack.append(len(path)-1)
        return stack
    for i in list_of_nexts:
        new_stack = stack.copy()
        new_stack.append(i)
        r = get_route(new_stack, open, path)
        if r == -1: continue
        else: return r
    return -1
    
def solution(entrances, exits, path):
    path = get_new_path(entrances, exits, path)
    flow = 0
    while True:
        stack = [0]
        open = set()
        open.add(0)
        route = get_route(stack, open, path)
        if(route == -1):
            return flow
        else:
            f = min([path[route[i-1]][route[i]] for i in range(1,len(route))])
            for i in range(2,len(route)-1):
                temp = path[route[i-1]][route[i]]
                path[route[i-1]][route[i]] = temp - f
                path[route[i]][route[i-1]] += f
            flow += f    
    return flow

print(solution([0, 1], [4, 5], [[0, 0, 4, 6, 0, 0], [0, 0, 5, 2, 0, 0], [0, 0, 0, 0, 4, 4], [0, 0, 0, 0, 6, 6], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]))

最佳答案

如果您的解决方案可以在您自己的计算机上运行,​​但不能在 Foobar 自动评分器上运行,只需知道自动评分器不允许的一些操作会立即导致编译错误:

  1. 任何打印语句:不允许打印语句(事实上,不允许系统输入或输出操作)。通过查看您的代码,您似乎有一个打印语句。摆脱它并重试。

  2. 缺少“解决方案”类和方法。如果您在自己的编辑器上编辑文件时不小心更改了类的名称,请务必将其更改回来。

  3. 使用不允许的库。

所有这些规则都可以在自述文件中找到。从外观上看,最后一行有一个 print 语句,这会自动导致自动评分器中出现编译错误。

祝你好运。

关于python - 逃生舱 Google Foobar 挑战 |最大流量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61992364/

相关文章:

Ruby:来自 YAML 的列名称和值对的哈希数组

javascript - 使用Javascript在ARRAY中获取PHP变量时出现问题

javascript - 在更长的数字中查找序列的算法

algorithm - 想出一个算法

Python:并行运行多个网络请求

Python字符串分割连接4

python - 如何修复 selenium 中的此错误 : AttributeError: 'list' object has no attribute 'find_elements_by_css_selector'

python - 查找两个具有重叠字符的子字符串的索引

c - 需要帮助读取 C 中的文件

algorithm - 直径提取的 MATLAB 圆形滑动窗口和像素值