ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17143๋ฒˆ: ๋‚š์‹œ์™•

๋‚š์‹œ์™•์ด ์ƒ์–ด ๋‚š์‹œ๋ฅผ ํ•˜๋Š” ๊ณณ์€ ํฌ๊ธฐ๊ฐ€ R×C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฉ์žํŒ์˜ ๊ฐ ์นธ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. r์€ ํ–‰, c๋Š” ์—ด์ด๊ณ , (R, C)๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์— ์žˆ๋Š” ์นธ์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋งž์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‚š์‹œ์™•์ด ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์žก๊ณ , ์ƒ์–ด๊ฐ€ ์›€์ง์ด๋Š” ๊ฒƒ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • ๋‚š์‹œ์™•์ด `c + 1`, ์ฆ‰ ์—ด์„ ํ•œ ์นธ ์ด๋™ํ•˜์—ฌ ํ•ด๋‹น ์—ด ์ค‘ ๊ฐ€์žฅ ๋•…๊ณผ ๊ฐ€๊นŒ์šด ํ–‰์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋‚ฎ์€ ์ƒ์–ด๋ถ€ํ„ฐ ์žก๊ฒŒ ๋œ๋‹ค.
    • ์žก์€ ์ƒ์–ด๋Š” ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.
  • 1์ดˆ ๋™์•ˆ ์ƒ์–ด๊ฐ€ ์ด๋™ํ•œ๋‹ค.
    • ์ƒ์–ด๋Š” ๊ฐ๊ฐ 1์ดˆ์— ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์†๋„๊ฐ€ 5๋ผ๋ฉด 5์นธ ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค.
    • ์ƒ์–ด๊ฐ€ ์›€์ง์ด๋Š” ์ค‘ ๊ฒฉ์žํŒ์„ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด, ์ง„ํ–‰ ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พผ ํ›„ ์›€์ง์ธ๋‹ค.
      • `์ƒ → ํ•˜`, `ํ•˜ → ์ƒ`, `์ขŒ → ์šฐ`, `์šฐ → ์ขŒ`๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
      • ๋™์ผํ•œ ์œ„์น˜์— ์ƒ์–ด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ํฌ๊ธฐ๊ฐ€ ํฐ ์ƒ์–ด๊ฐ€ ์ž‘์€ ์ƒ์–ด๋ฅผ ์žก์•„๋จน๋Š”๋‹ค.
  • ๋‚š์‹œ์™•์ด ๋ชจ๋“  ์—ด์˜ ์ƒ์–ด๋ฅผ ๋‹ค ์žก์„ ๋•Œ๊นŒ์ง€, ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ ํ›„ ์žก์€ ์ƒ์–ด ํฌ๊ธฐ์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def visitable(x, y):
    return 0 <= x < r and 0 <= y < c


def catch_shark(cur_loc):
    for x in range(r):
        if graph[x][cur_loc]:
            size = graph[x][cur_loc][SIZE]
            # ์ƒ์–ด๋ฅผ ์žก์•˜์œผ๋ฉด ์ •๋ณด ์‚ญ์ œ.
            graph[x][cur_loc] = 0
            return size
    return 0


def switch_dir(cur_dir):
    if cur_dir == 0:
        return 1
    elif cur_dir == 1:
        return 0
    elif cur_dir == 2:
        return 3
    return 2


def move_shark():
    next_graph = [[0] * c for _ in range(r)]

    for x in range(r):
        for y in range(c):

            if not graph[x][y]:
                continue

            next_x, next_y, cur_speed, cur_dir, cur_size = x, y, *graph[x][y]

            # speed ๋งŒํผ ์ด๋™ํ•˜๋„๋ก ํ•จ.
            # speed์— ํ•œ๋ฒˆ์— ์œ„์น˜๋ฅผ ์ฐพ๋„๋ก ํ•˜๋ฉด, Python์œผ๋กœ๋„ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค.
            while cur_speed > 0:
                dx, dy = dirs[cur_dir]
                next_x += dx
                next_y += dy
                # ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ์ด๋™ ๊ฐ€๋Šฅํ•œ ํšŸ์ˆ˜ ๊ฐ์†Œ
                if visitable(next_x, next_y):
                    cur_speed -= 1
                # ๋ฐฉ๋ฌธ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ๋’ค๋กœ ๊ฐ€๋ฉฐ ๊ฒฝ๋กœ ๋ณ€๊ฒฝ
                else:
                    next_x -= dx
                    next_y -= dy
                    cur_dir = switch_dir(cur_dir)
                    graph[x][y][DIRECTION] = cur_dir
            # ํ˜„์žฌ ์œ„์น˜์— ์ƒ์–ด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ.
            if not next_graph[next_x][next_y]:
                next_graph[next_x][next_y] = graph[x][y]
            # ์ƒ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ, ๊ฐฑ์‹ ํ•จ
            elif next_graph[next_x][next_y][SIZE] < cur_size:
                next_graph[next_x][next_y] = graph[x][y]

    return next_graph

if __name__ == "__main__":
    SPEED, DIRECTION, SIZE = 0, 1, 2
    dirs = ((-1, 0), (1, 0), (0, 1), (0, -1))
    r, c, m = map(int, stdin.readline().split())
    graph = [[0] * c for _ in range(r)]
    answer = 0

    for _ in range(m):
        i, j, s, d, z = map(int, stdin.readline().split())
        # d๋Š” ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ - 1
        graph[i - 1][j - 1] = [s, d - 1, z]

    for loc in range(c):
        # ์ƒ์–ด๋ฅผ ์žก์Œ
        answer += catch_shark(loc)
        # ์ƒ์–ด๋ฅผ ์ด๋™ ์‹œํ‚ด
        graph = move_shark()

    print(answer)

 ์ƒ์–ด์˜ ๋‹ค์Œ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด `while`๋ฌธ์„ ํ†ตํ•ด ๋ฐ˜๋ณตํ•˜์—ฌ ์ฐพ์œผ๋ฏ€๋กœ, `Python`์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.๐Ÿ˜ข ํ•˜์ง€๋งŒ `PyPy3`๋กœ๋Š” ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์–ด๊ฐ€ ๋ฐฉํ–ฅ์„ ์ฐพ์„ ๋•Œ ํ•œ ๋ฒˆ์— ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฉด `Python`์œผ๋กœ๋„ ์‹œ๊ฐ„ ๋‚ด์— ํ†ต๊ณผ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€