[백준 17143번] 낚시왕

박형진·2022년 5월 23일
0

https://www.acmicpc.net/problem/17143


1. 코드

import copy
import sys
from collections import defaultdict
​
​
def reverse(a):
    if a == 1:
        return 2
    elif a == 2:
        return 1
    elif a == 3:
        return 4
    elif a == 4:
        return 3
​
​
def move(x, y, dir, speed):
    global r, c
    n_r = r - 1  # 예를들어 0~5까지 총 6개의 행이 있다면 칸의 이동은 5칸이 최대
    n_c = c - 1
    if dir == 1:
        if x >= speed:
            x -= speed
        else:
            k = speed - x  # 0행으로 이동한 다음 남은 이동의 양 k
            a, b = divmod(k, n_r)
            if a % 2 == 0:  # 0행에서 b만큼 이동
                dir = reverse(dir)
                x = b
            else:  # 마지막 행에서 -b만큼 이동
                x = n_r - b
            if x == 0:
                dir = 1
            elif x == n_r:
                dir = 2
    elif dir == 2:
        if x + speed <= n_r:
            x += speed
        else:
            k = speed - (n_r - x)
            a, b = divmod(k, n_r)
            if a % 2 == 0:
                dir = reverse(dir)
                x = n_r - b
            else:
                x = b
            if x == 0:
                dir = 1
            elif x == n_r:
                dir = 2
    # 오른쪽
    elif dir == 3:
        if y + speed <= n_c:
            y += speed
        else:
            k = speed - (n_c - y)
            a, b = divmod(k, n_c)
            if a % 2 == 0:
                dir = reverse(dir)
                y = n_c - b
            else:
                y = b
            if y == 0:
                dir = 4
            elif y == n_c:
                dir = 3
    elif dir == 4:
        if y >= speed:
            y -= speed
        else:
            k = speed - y
            a, b = divmod(k, n_c)
            if a % 2 == 0:
                dir = reverse(dir)
                y = b
            else:
                y = n_c - b
            if y == 0:
                dir = 4
            elif y == n_c:
                dir = 3
    return x, y, dir
​
​
ans = 0
r, c, m = map(int, (sys.stdin.readline().rstrip().split()))
per_col_shark = defaultdict(list)
for _ in range(m):
    x, y, s, d, z = map(int, (sys.stdin.readline().rstrip().split()))
    per_col_shark[y - 1].append([x - 1, s, d, z])  # (x,y) 상어의 위치, s=속력 d=이동방향 z=크기for i in range(c):
    graph = [[0] * 100 for _ in range(100)]
    temp = defaultdict(list)
    # todo 1. 상어 먹기
    if per_col_shark[i]:
        per_col_shark[i].sort()
        ans += per_col_shark[i].pop(0)[3]# todo 2. 상어 이동
    for y in per_col_shark:
        while per_col_shark[y]:
            # x=행, s=속력, d=이동방향, z=크기
            x, s, d, z = per_col_shark[y].pop()
            nx, ny, nd = move(x, y, d, s)
​
            graph[nx][ny] += 1
            temp[ny].append([nx, s, nd, z])
            
    # todo 3. 상어 합치기
    for a in range(100):
        for b in range(100):
            if graph[a][b] >= 2:
                new = []
                target = []
                for f in temp[b]:
                    if f[0] == a:
                        target.append(f)
                    else:
                        new.append(f)
                new.append(sorted(target, key=lambda q: -q[3])[0])
                temp[b] = new[:]
    per_col_shark = copy.deepcopy(temp)
print(ans)
profile
안녕하세요!

0개의 댓글