목차

낚시왕

ps
링크acmicpc.net/…
출처BOJ
문제 번호17143
문제명낚시왕
레벨골드 2
분류

구현

시간복잡도O(r*c^2)
인풋사이즈r<=100, c<=100
사용한 언어Python
제출기록31244KB / 528ms
최고기록528ms
해결날짜2021/11/12

풀이

코드

"""Solution code for "BOJ 17143. 낚시왕".

- Problem link: https://www.acmicpc.net/problem/17143
- Solution link: http://www.teferi.net/ps/problems/boj/17143

Tags: [Implementation]
"""

import sys

INF = float('inf')
UP, DOWN, RIGHT, LEFT = 1, 2, 3, 4


def main():
    R, C, M = [int(x) for x in sys.stdin.readline().split()]
    sharks = [[None] * C for _ in range(R)]
    min_r = INF
    for _ in range(M):
        r, c, s, d, z = [int(x) for x in sys.stdin.readline().split()]
        dr = dc = 0
        if d in (UP, DOWN):
            dr = -s if d == UP else s
        else:
            dc = -s if d == LEFT else s
        sharks[r - 1][c - 1] = (dr, dc, z)
        if c - 1 == 0:
            min_r = min(r - 1, min_r)

    answer = 0
    r_round, c_round = (R - 1) * 2, (C - 1) * 2
    for col in range(C):
        if min_r != INF:
            answer += sharks[min_r][col][2]
            sharks[min_r][col] = None

        prev_sharks, sharks = sharks, [[None] * C for _ in range(R)]
        min_r = INF
        for r, prev_sharks_r in enumerate(prev_sharks):
            for c, prev_shark in enumerate(prev_sharks_r):
                if not prev_shark:
                    continue
                dr, dc, size = prev_shark
                nr, nc = (r + dr) % r_round, (c + dc) % c_round
                if nr >= R:
                    nr, dr = r_round - nr, -dr
                elif nc >= C:
                    nc, dc = c_round - nc, -dc
                if not (shark := sharks[nr][nc]) or shark[2] < size:
                    sharks[nr][nc] = (dr, dc, size)
                if nc == col + 1:
                    min_r = min(nr, min_r)

    print(answer)


if __name__ == '__main__':
    main()