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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17837๋ฒˆ: ์ƒˆ๋กœ์šด ๊ฒŒ์ž„ 2

์žฌํ˜„์ด๋Š” ์ฃผ๋ณ€์„ ์‚ดํŽด๋ณด๋˜ ์ค‘ ์ฒด์ŠคํŒ๊ณผ ๋ง์„ ์ด์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์€ ํฌ๊ธฐ๊ฐ€ N×N์ธ ์ฒด์ŠคํŒ์—์„œ ์ง„ํ–‰๋˜๊ณ , ์‚ฌ์šฉํ•˜๋Š” ๋ง์˜ ๊ฐœ์ˆ˜๋Š” K๊ฐœ์ด๋‹ค. ๋ง์€ ์›ํŒ๋ชจ์–‘์ด๊ณ , ํ•˜

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์˜ ์š”๊ตฌ์ƒ์„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ๋น„์Šทํ•œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋กœ๋Š” ์–ด๋ฅธ ์ƒ์–ด์ด์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค. ์ด์ „์— ๋‹ค๋ฃฌ ๋ฌธ์ œ๋ณด๋‹ค ๊นŒ๋‹ค๋กœ์šด ๊ฒƒ์€ ์ฒด์Šค ๋ง์ด ์ค‘์ฒฉ๋˜๋Š” ๊ฒฝ์šฐ, ๊ทธ๋Œ€๋กœ ์Œ“์ธ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ์ด๋™ํ•  ๋•Œ ํ˜„์žฌ ์ฒด์Šค ๋ง์˜ ๋ฒˆํ˜ธ์™€ ์Œ“์ธ ์ฒด์Šค ๋ง๋“ค์„ ํŒ๋‹จํ•˜์—ฌ์„œ ์ด๋™์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๋‹ฌ๋ž๋‹ค. ์—ฌ๊ธฐ์— ์ดˆ๊ธฐ์— ์ฃผ์–ด์ง€๋Š” ์ •๋ณด์— ํฐ์ƒ‰, ๋นจ๊ฐ„์ƒ‰, ํŒŒ๋ž€์ƒ‰์˜ ์ƒ‰๊น”์ด ๊ตฌ๋ถ„๋˜๋Š” ์นธ์ด ์ฃผ์–ด์ง€๋ฉด ๊ฐ ์นธ๋“ค์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

  • ํฐ์ƒ‰์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ์ด๋™ํ•œ๋‹ค.
  • ๋นจ๊ฐ„์ƒ‰์ธ ๊ฒฝ์šฐ ์ด๋™ํ•˜๋ ค๋Š” ๋ง์„ ๋’ค์ง‘์–ด์„œ ์ด๋™ํ•œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋™ํ•˜๋ ค๋Š” ์นธ์— A, B, C๊ฐ€ ์žˆ๋‹ค๋ฉด C, B, A ๊ฐ€ ๋œ๋‹ค.
  • ํŒŒ๋ž€์ƒ‰๊ณผ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พผ๋‹ค.
    • ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊พผ ํ›„์—๋„ ๋™์ผํ•œ ์ƒํ™ฉ์ด ๋ฐœ์ƒ(ํŒŒ๋ž€์ƒ‰ or ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚จ)ํ•œ๋‹ค๋ฉด, ๊ฐ€๋งŒํžˆ ์žˆ๋Š”๋‹ค.

 ์œ„์˜ ์ƒํ™ฉ๋“ค์„ ์ž˜ ๊ณ ๋ คํ•˜์—ฌ์„œ ๊ทธ๋Œ€๋กœ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์ž˜ ๋ชป ์ƒ๊ฐํ–ˆ๋˜ ๋ถ€๋ถ„์€ ์ค‘์ฒฉ๋œ ๊ฒฝ์šฐ ํ•œ ๋ฒˆ์— ์ด๋™์‹œํ‚ค๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค. (์‚ฌ์‹ค, ๊ทธ๋ฆผ ์˜ˆ์ œ๋ฅผ ์ž์„ธํžˆ ์•ˆ ๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.๐Ÿ˜ฅ) ์ƒ๊ฐํ•œ ๋ฐ๋กœ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๊ณ„์† ๋‚˜์™€์„œ ์ฐพ์•„๋ณด๋‹ˆ ํ˜„์žฌ ์ด๋™ํ•˜๋ ค๋Š” ๋ง ๋ฒˆํ˜ธ๋ณด๋‹ค ์œ„์— ์žˆ๋Š” ๊ฒƒ์€ ๊ฐ™์ด ์›€์ง์ด๊ณ , ์•„๋ž˜์— ์žˆ๋Š” ๊ฒƒ์€ ๊ทธ๋Œ€๋กœ ๋‘์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค. ์ด ์—ญ์‹œ ๊ทธ๋Œ€๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin

RED, BLUE = 1, 2


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


def move_cp(cp_idx):
    r, c, cur_dir = cp_loc[cp_idx]
    dr, dc = dirs[cur_dir]
    next_r, next_c = r + dr, c + dc

    if not visitable(next_r, next_c) or board[next_r][next_c] == BLUE:
        dr, dc = dirs[reversed_dir[cur_dir]]
        next_r, next_c = r + dr, c + dc
        # ๋ฐฉํ–ฅ๋งŒ ๋ฐ˜๋Œ€๋กœ ๋ณ€๊ฒฝ
        cp_loc[cp_idx][2] = reversed_dir[cur_dir]

        if not visitable(next_r, next_c) or board[next_r][next_c] == BLUE:
            return False

    # ๊ฒน์ณ์ง€๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜์—ฌ, ์œ„ ์•„๋ž˜ ๋ง๋“ค์„ ํŒ๋ณ„ํ•˜์—ฌ์•ผ ํ•จ
    idx = check[r][c].index(cp_idx)
    temp = check[r][c][idx:]
    check[r][c] = check[r][c][:idx]

    # ๋นจ๊ฐ„์ƒ‰์ธ ๊ฒฝ์šฐ ๋’ค์ง‘์Œ
    if board[next_r][next_c] == RED:
        temp = temp[::-1]

    # ์œ„์น˜ ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ ๋ณ€๊ฒฝ
    for cp_idx in temp:
        check[next_r][next_c].append(cp_idx)
        cp_loc[cp_idx][:2] = [next_r, next_c]

    return len(check[next_r][next_c]) >= 4


if __name__ == '__main__':
    # ์šฐ, ์ขŒ, ์ƒ, ํ•˜
    dirs = ((0, 1), (0, -1), (-1, 0), (1, 0))
    reversed_dir = {0: 1, 1: 0, 2: 3, 3: 2}

    N, K = map(int, stdin.readline().split())
    board = [list(map(int, stdin.readline().split())) for _ in range(N)]
    check = [[[] for _ in range(N)] for _ in range(N)]

    # chase piece location
    cp_loc = [list(map(int, stdin.readline().split())) for _ in range(K)]

    # ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ 1์”ฉ ๊ฐ์†Œ
    for idx, cp in enumerate(cp_loc):
        x, y, cur_dir = cp
        cp_loc[idx] = [x - 1, y - 1, cur_dir - 1]
        check[x - 1][y - 1].append(idx)

    time = 1
    while time <= 1000:
        for i in range(K):
            if move_cp(i):
                print(time)
                exit(0)
        time += 1
    print(-1)

 

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