Problem p02580 (python)

Fixed submission:
import sys
# sys.setrecursionlimit(100000)
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    h, w, m = [int(i) for i in input().split()]
    d_h = defaultdict(int)
    d_w = defaultdict(int)
    bom = set()
    for _ in range(m):
        h, w = [int(i) for i in input().split()]
        bom.add((h, w))
        d_h[h] += 1
        d_w[w] += 1
    H = sorted(d_h.items(), key=lambda x: -x[1])
    W = sorted(d_w.items(), key=lambda x: -x[1])
    ans = 0
    for k_h, v_h in H:
        for k_w, v_w in W:
            tmp = v_h + v_w
            if (k_h, k_w) in bom:
                tmp -= 1
                ans = max(tmp, ans)
            else:
                ans = max(tmp, ans)
                break
        else:
            continue
        break

    for k_w, v_w in W:
        for k_h, v_h in H:
            tmp = v_h + v_w
            if (k_h, k_w) in bom:
                tmp -= 1
                ans = max(tmp, ans)
            else:
                ans = max(tmp, ans)
                break
        else:
            continue
        break
    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    INF = 10**7
    h, w = [int(i) for i in input().split()]
    c_h, c_w = [int(i) for i in input().split()]
    d_h, d_w = [int(i) for i in input().split()]
    grid = [["#"] * (w + 2)]
    for _ in range(h):
        line = ["#"] + list(input()) + ["#"]
        grid.append(line)
    grid.append(["#"] * (w + 2))
    warp_count = [[INF] * (w + 2) for _ in range(h + 2)]
    directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
    warps = []
    for x in range(-2, 3):
        for y in range(-2, 3):
            if x == 0 and y == 0:
                continue
            if (x, y) not in directions:
                warps.append((x, y))

    stack = []
    warp_count[c_h][c_w] = 0
    stack.append((c_h, c_w, 0))

    while stack:
        _h, _w, warp = stack.pop()
        warp_count[_h][_w] = warp

        cnt = 0
        # walk
        for _a, _b in directions:
            if grid[_h + _a][_w + _b] != "#" and warp_count[_h + _a][_w + _b] > warp:
                cnt += 1
                stack.append((_h + _a, _w + _b, warp))

        if cnt == 4:  # 4方向行けるときにワープは不要
            continue

        # warp
        for x, y in warps:
            if _h + x < 0 or _h + x > h or _w + y < 0 or _w + y > w:
                continue
            elif grid[_h + x][_w + y] == "#":
                continue

            if warp_count[_h + x][_w + y] > warp + 1:
                stack.append((_h + x, _w + y, warp + 1))

    if warp_count[d_h][d_w] == INF:
        print(-1)
    else:
        print(warp_count[d_h][d_w])
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
# sys.setrecursionlimit(100000)
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    h, w, m = [int(i) for i in input().split()]
    d_h = defaultdict(int)
    d_w = defaultdict(int)
    bom = set()
    for _ in range(m):
        h, w = [int(i) for i in input().split()]
        bom.add((h, w))
        d_h[h] += 1
        d_w[w] += 1
    H = sorted(d_h.items(), key=lambda x: -x[1])
    W = sorted(d_w.items(), key=lambda x: -x[1])
    ans = H[0][1] + W[0][1]
    if (H[0][0], W[0][0]) in bom:
        ans -= 1
    try:
        ans2 = H[1][1] + W[0][1]
        if (H[1][0], W[0][0]) in bom:
        ans2 -= 1
    except:
        ans2 = -1
    try:
        ans3 = H[0][1] + W[1][1]
        if (H[0][0], W[1][0]) in bom:
            ans3 -= 1
    except:
        ans3 = -1
    print(max(ans, ans2, ans3))

    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
# sys.setrecursionlimit(100000)
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    h, w, m = [int(i) for i in input().split()]
    d_h = defaultdict(int)
    d_w = defaultdict(int)
    bom = set()
    for _ in range(m):
        h, w = [int(i) for i in input().split()]
        bom.add((h, w))
        d_h[h] += 1
        d_w[w] += 1
    H = sorted(d_h.items(), key=lambda x: -x[1])
    W = sorted(d_w.items(), key=lambda x: -x[1])
    ans = H[0][1] + W[0][1]
    if (H[0][0], W[0][0]) in bom:
        ans -= 1

    ans2 = H[1][1] + W[0][1]
    if (H[1][0], W[0][0]) in bom:
        ans2 -= 1
    ans3 = H[0][1] + W[1][1]
    if (H[0][0], W[1][0]) in bom:
        ans3 -= 1

    print(max(ans, ans2, ans3))

    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
"""
task statement
==============

H \times W マスの二次元グリッドがあります。この中には M 個の爆破対象があります。 i 個目の爆破対象の位置は \left(h_i, w_i \right)です。
高橋君は 1 つのマスを選び、そのマスに爆弾を設置し、起爆します。爆弾と同じ行または同じ列に存在する爆破対象が爆破されます。爆破対象が存在するマスに爆弾を設置することも出来ます。
高橋君は、爆破する爆破対象の数を最大化しようとしています。最大でいくつの爆破対象を爆破出来るか答えてください。

io_style
-------
H W M
h_1 w_1
\vdots
h_M w_M
"""
import sys
# sys.setrecursionlimit(100000)
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    h, w, m = [int(i) for i in input().split()]
    d_h = defaultdict(int)
    d_w = defaultdict(int)
    bom = set()
    for _ in range(m):
        h, w = [int(i) for i in input().split()]
        bom.add((h, w))
        d_h[h] += 1
        d_w[w] += 1
    H = sorted(d_h.items(), key=lambda x: -x[1])
    W = sorted(d_w.items(), key=lambda x: -x[1])
    ans = H[0][1] + W[0][1]
    if (H[0][0], W[0][0]) in bom:
        ans -= 1

    ans2 = H[1][1] + W[0][1]
    if (H[1][0], W[0][0]) in bom:
        ans2 -= 1
    ans3 = H[0][1] + W[1][1]
    if (H[0][0], W[1][0]) in bom:
        ans3 -= 1

    print(max(ans, ans2, ans3))

    return


if __name__ == "__main__":
    main()