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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

19237๋ฒˆ: ์–ด๋ฅธ ์ƒ์–ด

์ฒซ ์ค„์—๋Š” N, M, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 20, 2 ≤ M ≤ N2, 1 ≤ k ≤ 1,000) ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฒฉ์ž์˜ ๋ชจ์Šต์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ์นธ์ด๊ณ , 0์ด ์•„๋‹Œ ์ˆ˜ x๋Š” x๋ฒˆ ์ƒ์–ด๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์นธ์„ ์˜๋ฏธ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์ƒ์–ด์˜ ์œ„์น˜์™€ ์ƒ์–ด์˜ ๋ฐฉํ–ฅ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ ๊ฐ ์ƒ์–ด๋“ค์€ ์ด๋™ํ•˜๋ฉฐ ๋ƒ„์ƒˆ๋ฅผ ๋ฟŒ๋ฆฌ๊ณ , ์ด๋™ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฟŒ๋ ค์ง„ ๋ƒ„์ƒˆ๋Š” K์ดˆ๊ฐ€ ์ง€๋‚˜๋ฉด ์‚ฌ๋ผ์ง„๋‹ค. ์ด๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด 1) ๋ƒ„์ƒˆ ๋ฟŒ๋ฆฌ๊ธฐ, 2) ์ƒ์–ด ์ด๋™, 3) ์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ๋ƒ„์ƒˆ ๊ฐ์†Œ๋กœ ๋‚˜๋ˆ„์–ด ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

 

 ํ•˜์ง€๋งŒ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ 3๊ฐ€์ง€ ๋ฌธ์ œ๋ฅผ ๋งž์ดํ•˜๋ฉฐ ํ‘ธ๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ง€์—ฐ๋˜์—ˆ๋‹ค.

  • ๋™์ผํ•œ ์ž๋ฆฌ์— ์—ฌ๋Ÿฌ ๋งˆ๋ฆฌ์˜ ์ƒ์–ด๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ, ๊ฐ€์žฅ ์ž‘์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์ƒ์–ด๋งŒ ๋‚จ๋Š”๋‹ค.
    • ์ฒ˜์Œ์—๋Š” ๋™์ผํ•œ ์ž๋ฆฌ์˜ ๊ฒฝ์šฐ append๋ฅผ ํ•ด์ฃผ๋ฉด์„œ ์ฒดํฌํ•˜๊ฑฐ๋‚˜ ์ƒ์–ด์˜ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ต ์—ฐ์‚ฐ์„ ํ†ตํ•ด ํ•˜๋‚˜๋งŒ ๋‚จ๋„๋ก ํ•ด๋ณผ ์ƒ๊ฐ์ด์—ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ, ๊ฐ€์žฅ ์ž‘์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์ƒ์–ด๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด, ์•Œ์•„์„œ ์ฒ˜๋ฆฌ๋  ์ผ์ด๋‹ค!
  • ์ƒ, ํ•˜, ์ขŒ, ์šฐ์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์˜ค๋žœ๋งŒ์— ์ฃผ๋‹ค ๋ณด๋‹ˆ x, y ์ˆ˜์ง์„  ์ขŒํ‘œ์ƒ์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค.
    • ๋‡Œ์ ˆ์ด ์™”๋Š”์ง€, ํ–‰๋ ฌ์˜ ์ขŒํ‘œ๊ฐ€ ์•„๋‹Œ x, y ์ขŒํ‘œ ์ƒ์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  `dirs = ((0, -1), (0, 1), (-1, 0), (1, 0))`์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด๋‘๊ณ  ๋ฌธ์ œ์— ์ ‘๊ทผํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์˜ณ์€ ๋‹ต์ด ์•ˆ ๋‚˜์˜ฌ ์ˆ˜๋ฐ–์—...๐Ÿ˜ซ
  • ๋‚ด๊ฐ€ 3์ฐจ์› ๋ฐฐ์—ด๋กœ ์„ค๊ณ„๋ฅผ ํ•˜๊ณ , ๊ฐ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ฐฉ๊ฐ ํ•˜์—ฌ ๋””๋ฒ„๊น…ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์„ ์“ฐ๊ณ  ์žˆ์—ˆ๋‹ค...

 

์ฝ”๋“œ

from sys import stdin


SHARK, SCENT = 0, 1


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


def simulation():
    # ๋ƒ„์ƒˆ ๋ฟŒ๋ฆฌ๊ธฐ.
    for s_idx, values in enumerate(sharks):
        if values is None:
            continue

        r, c, _ = values
        # ๋ƒ„์ƒˆ๋ฅผ ๋ฟŒ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ.
        if board[r][c] is None or board[r][c][SHARK] == s_idx:
            board[r][c] = [s_idx, K]
        else:
            sharks[s_idx] = None

    # ์ƒ์–ด ์ด๋™
    for s_idx, values in enumerate(sharks):
        if values is None:
            continue

        r, c, cur_dir = values

        # ๋นˆ์นธ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
        for next_dir in priority[s_idx][cur_dir]:
            dr, dc = dirs[next_dir]
            next_r, next_c = r + dr, c + dc

            if visitable(next_r, next_c) and board[next_r][next_c] is None:
                sharks[s_idx] = [next_r, next_c, next_dir]
                break
        # ์ž์‹ ์˜ ๋ƒ„์ƒˆ๊ฐ€ ์žˆ๋Š” ์นธ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
        else:
            for next_dir in priority[s_idx][cur_dir]:
                dr, dc = dirs[next_dir]
                next_r, next_c = r + dr, c + dc

                if visitable(next_r, next_c) and board[next_r][next_c][SHARK] == s_idx:
                    sharks[s_idx] = [next_r, next_c, next_dir]
                    break
            # ์œ„์˜ 2๊ฐ€์ง€๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ด๋™ ๋ถˆ๊ฐ€.
            else:
                sharks[s_idx] = None

    # ๋ƒ„์ƒˆ ๊ฐ์†Œ
    for r in range(N):
        for c in range(N):
            if board[r][c] is None:
                continue

            board[r][c][SCENT] -= 1

            if board[r][c][SCENT] == 0:
                board[r][c] = None


if __name__ == "__main__":
    dirs = ((-1, 0), (1, 0), (0, -1), (0, 1))
    N, M, K = map(int, stdin.readline().split())
    board = [list(map(int, stdin.readline().split())) for _ in range(N)]
    cur_shark_dirs = list(map(int, stdin.readline().split()))  # ๊ฐ ์ƒ์–ด์˜ ๋ฐฉํ–ฅ
    sharks = [0] * M

    for i in range(N):
        for j in range(N):
            if board[i][j] != 0:
                # ์œ„์น˜, ๋ฐฉํ–ฅ์„ ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ ๊ฐ์†Œ
                sharks[board[i][j] - 1] = [i, j, cur_shark_dirs[board[i][j] - 1] - 1]
                board[i][j] -= 1
            board[i][j] = None

    priority = [[[0] for _ in range(4)] for _ in range(M)]

    for shark_idx in range(M):
        for p_idx in range(4):
            # ์šฐ์„  ์ˆœ์œ„ ๋ฐฉํ–ฅ์„ dirs ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ ๊ฐ์†Œ
            priority[shark_idx][p_idx] = [p - 1 for p in list(map(int, stdin.readline().split()))]

    for cur_time in range(-1, 1001):
        # 1๋ฒˆ ์ƒ์–ด๋งŒ ๋‚จ๋Š” ๊ฒฝ์šฐ
        if sharks.count(None) == M - 1:
            print(cur_time)
            exit(0)
        else:
            simulation()
    else:
        print(-1)

 

 

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