#37754: python 90% WA13, 16 求助Orz 註解都寫好了


ericshen19555@gmail.com (暴力又被TLE)

學校 : 南光中學
編號 : 103121
來源 : [220.143.180.32]
最後登入時間 :
2024-05-03 22:46:29
g596. 2. 動線安排 -- 2021年11月APCS | From: [220.132.122.21] | 發表日期 : 2023-10-05 00:17

# 動線安排 - AC90% WA10%
# https://zerojudge.tw/ShowProblem?problemid=g596

from sys import stdin
e = stdin.readline

def P():
    return # 快把我註解掉~~
    s = ["_", "-", "|", "+", "@", " "]
    [print(" ".join([s[j] for j in i[:-1]])) for i in l[:-1]]
    print("cnt", cnt)
    print("ans", ans)
    print()

m, n, h = map(int, e().split())
l = [[0] * n + [-1] for i in range(m)] + [[-1] * n] # -1為邊界

ans = 0
cnt = 0
d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for _ in range(h):
    r, c, t = map(int, e().split())
    if t: # remove
        l[r][c] = 0 ; cnt -= 1 # 拔掉柱子
        for dx, dy in d: # 四個方向都試一遍
            nx, ny = r + dx, c + dy
            while l[nx][ny] != -1: # 出界就淘汰啦
                if l[nx][ny] == 4: break # 碰到柱子就停
                if dx == 0: # 現在橫著走
                    if l[nx][ny] == 1: l[nx][ny] = 0 ; cnt -= 1 # - -> _
                    if l[nx][ny] == 3: l[nx][ny] = 2            # + -> |
                if dy == 0: # 現在直著走
                    if l[nx][ny] == 2: l[nx][ny] = 0 ; cnt -= 1 # | -> _
                    if l[nx][ny] == 3: l[nx][ny] = 1            # + -> -
                nx += dx
                ny += dy
    else: # add
        if l[r][c] == 0: cnt += 1 # 原本是空地才+1
        l[r][c] = 4 # 插下柱子
        for dx, dy in d: # 四個方向都試一遍
            stack = [] # 用來記錄可能要放線的座標 他不是stack但我懶得改名子
            nx, ny = r + dx, c + dy
            while l[nx][ny] != -1: # 出界就淘汰啦
                if l[nx][ny] == 4: # 碰到柱子 代表可以連線
                    for x, y in stack: # 把剛剛紀錄的座標都連上線
                        if l[x][y] == 0: cnt += 1 # 原本是空地才+1
                        if dx == 0: # 現在橫著走
                            if l[x][y] == 2: l[x][y] = 3 # | -> +
                            else:            l[x][y] = 1 # _ -> -
                        if dy == 0: # 現在直著走
                            if l[x][y] == 1: l[x][y] = 3 # - -> +
                            else:            l[x][y] = 2 # _ -> |
                    break # 把線連完這個方向就結束
                stack.append((nx, ny))
                nx += dx
                ny += dy
    ans = max(cnt, ans)
    P()
print(ans)
print(cnt)
 
#37755: Re: python 90% WA13, 16 求助Orz 註解都寫好了


ericshen19555@gmail.com (暴力又被TLE)

學校 : 南光中學
編號 : 103121
來源 : [220.143.180.32]
最後登入時間 :
2024-05-03 22:46:29
g596. 2. 動線安排 -- 2021年11月APCS | From: [220.132.122.21] | 發表日期 : 2023-10-05 10:59

# 動線安排 - AC90% WA10%
# https://zerojudge.tw/ShowProblem?problemid=g596

from sys import stdin
e = stdin.readline

def P():
    return # 快把我註解掉~~
    s = ["_", "-", "|", "+", "@", " "]
    [print(" ".join([s[j] for j in i[:-1]])) for i in l[:-1]]
    print("cnt", cnt)
    print("ans", ans)
    print()

m, n, h = map(int, e().split())
l = [[0] * n + [-1] for i in range(m)] + [[-1] * n] # -1為邊界

ans = 0
cnt = 0
d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for _ in range(h):
    r, c, t = map(int, e().split())
    if t: # remove
        l[r][c] = 0 ; cnt -= 1 # 拔掉柱子
        for dx, dy in d: # 四個方向都試一遍
            nx, ny = r + dx, c + dy
            while l[nx][ny] != -1: # 出界就淘汰啦
                if l[nx][ny] == 4: break # 碰到柱子就停
                if dx == 0: # 現在橫著走
                    if l[nx][ny] == 1: l[nx][ny] = 0 ; cnt -= 1 # - -> _
                    if l[nx][ny] == 3: l[nx][ny] = 2            # + -> |
                if dy == 0: # 現在直著走
                    if l[nx][ny] == 2: l[nx][ny] = 0 ; cnt -= 1 # | -> _
                    if l[nx][ny] == 3: l[nx][ny] = 1            # + -> -
                nx += dx
                ny += dy
    else: # add
        if l[r][c] == 0: cnt += 1 # 原本是空地才+1
        l[r][c] = 4 # 插下柱子
        for dx, dy in d: # 四個方向都試一遍
            stack = [] # 用來記錄可能要放線的座標 他不是stack但我懶得改名子
            nx, ny = r + dx, c + dy
            while l[nx][ny] != -1: # 出界就淘汰啦
                if l[nx][ny] == 4: # 碰到柱子 代表可以連線
                    for x, y in stack: # 把剛剛紀錄的座標都連上線
                        if l[x][y] == 0: cnt += 1 # 原本是空地才+1
                        if dx == 0: # 現在橫著走
                            if l[x][y] == 2: l[x][y] = 3 # | -> +
                            else:            l[x][y] = 1 # _ -> -
                        if dy == 0: # 現在直著走
                            if l[x][y] == 1: l[x][y] = 3 # - -> +
                            else:            l[x][y] = 2 # _ -> |
                    break # 把線連完這個方向就結束
                stack.append((nx, ny))
                nx += dx
                ny += dy
    ans = max(cnt, ans)
    P()
print(ans)
print(cnt)

已解決 :)
感謝吳邦一教授~

 
ZeroJudge Forum