32102023-02-22 15:25:5912BotiJegesmedve (50)python3Hibás válasz 34/50215ms18900 KiB
rows, cols = map(int, input().split())

ts = [[int(x) for x in input().split()] for _ in range(rows)]
fs = [[int(x) for x in input().split()] for _ in range(rows)]

x, y = None, None
for ri, r in enumerate(ts):
    for ci, c in enumerate(r):
        if c == -1:
            x, y = ri, ci
ts[x][y] = 1e9


def outside(ri, ci):
    return ri < 0 or ri >= rows or ci < 0 or ci >= cols


def dfs(ri, ci, t, mt):
    global visited
    global minmt
    global minmtc
    if outside(ri, ci) or visited[ri][ci] or ts[ri][ci] <= t:
        return 0
    # print("visit", ri, ci)
    visited[ri][ci] = True
    mt = min(mt, ts[ri][ci])
    if fs[ri][ci] >= mt - t:
        fs[ri][ci] -= mt - t
        return mt - t
    elif fs[ri][ci] > 0:
        if mt < minmt:
            minmt = mt
            minmtc = (ri, ci)
    ns = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    ns = [(ri + i, ci + j) for (i, j) in ns]
    ns = [n for n in ns if not outside(*n)]
    ns = [(ts[i][j], (i, j)) for (i, j) in ns]
    ns.sort(reverse=True)
    for (nt, n) in ns:
        f = dfs(*n, t, mt)
        if f > 0:
            return f
    return 0


t = 0
while True:
    visited = [[False] * cols for _ in range(rows)]
    minmt = 1e10
    minmtc = None
    f = dfs(x, y, t, 1e9)
    if f == 0:
        if minmtc:
            f = fs[minmtc[0]][minmtc[1]]
            fs[minmtc[0]][minmtc[1]] = 0
        else:
            break
    # print(t)
    # for r in ts:
    #     print(*r)
    # for r in fs:
    #     print(*r)
    t += f

print(t + 1)
RészfeladatÖsszpontTesztVerdiktIdőMemória
base34/50
1Elfogadva0/018ms11944 KiB
2Elfogadva0/046ms12420 KiB
3Elfogadva3/332ms12288 KiB
4Elfogadva3/363ms12332 KiB
5Elfogadva3/321ms13144 KiB
6Elfogadva4/430ms13440 KiB
7Elfogadva3/320ms12820 KiB
8Elfogadva3/319ms13152 KiB
9Elfogadva3/319ms12900 KiB
10Elfogadva4/421ms13280 KiB
11Hibás válasz0/4215ms15760 KiB
12Elfogadva4/4185ms15160 KiB
13Elfogadva4/4143ms14384 KiB
14Futási hiba0/435ms18060 KiB
15Futási hiba0/435ms18292 KiB
16Futási hiba0/439ms18900 KiB