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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

19236๋ฒˆ: ์ฒญ์†Œ๋…„ ์ƒ์–ด

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ 4๊ฐœ์˜ ์ค„์— ๊ฐ ์นธ์˜ ๋“ค์–ด์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ์˜ ์ •๋ณด๊ฐ€ 1๋ฒˆ ํ–‰๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ฌผ๊ณ ๊ธฐ์˜ ์ •๋ณด๋Š” ๋‘ ์ •์ˆ˜ ai, bi๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ai๋Š” ๋ฌผ๊ณ ๊ธฐ์˜ ๋ฒˆํ˜ธ, bi๋Š” ๋ฐฉํ–ฅ์„ ์˜๋ฏธํ•œ๋‹ค. ๋ฐฉํ–ฅ bi๋Š”

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 4x4 ํฌ๊ธฐ์— ๋ฌผ๊ณ ๊ธฐ๋“ค์ด ์žˆ๊ณ , ์ƒ์–ด๋Š” `(0, 0)` ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์žก์•„๋จน๋Š”๋‹ค. ์ƒ์–ด๋Š” ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์žก์•„๋จน์œผ๋ฉด ๊ทธ ๋ฌผ๊ณ ๊ธฐ์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ์„œ ์›€์ง์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•ด๋‹น ๋ฐฉํ–ฅ์˜ ๋ชจ๋“  ์นธ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ƒ์–ด๊ฐ€ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์žก์•„๋จน์€ ํ›„์—” ๋ฌผ๊ณ ๊ธฐ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋™ํ•œ๋‹ค.

 

  • ๋ฌผ๊ณ ๊ธฐ๋Š” ํ•œ ์นธ์”ฉ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์นธ์€ 4x4 ํฌ๊ธฐ๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ์˜์—ญ์ด๊ฑฐ๋‚˜, ์ƒ์–ด์˜ ์œ„์น˜๊ฐ€ ์žˆ๋Š” ์˜์—ญ์ด๋‹ค.
    • ์ด๋™ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ๊นŒ์ง€, ๋ฐฉํ–ฅ์„ 45๋„๋กœ ํšŒ์ „ํ•˜์—ฌ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๊ณณ์„ ์ ‘๊ทผํ•œ๋‹ค.
  • ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๊ณณ์˜ ๋ฌผ๊ณ ๊ธฐ์™€ ์ž๋ฆฌ๋ฅผ ์ „ํ™˜ํ•œ๋‹ค.

์ฆ‰ ๋ฌธ์ œ๋Š” ์ƒ์–ด๊ฐ€ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ , ๋ฌผ๊ณ ๊ธฐ๋“ค์„ ์ด๋™์‹œํ‚ค๋Š” ๊ณผ์ •์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ˜๋ณต์‹œํ‚ค๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์„ค๊ณ„๋ฅผ ์ž˜ํ•˜๊ณ  ํ‘ผ๋‹ค๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from copy import deepcopy


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


def find_fish(cur_graph, fish_num):
    for x in range(4):
        for y in range(4):
            if cur_graph[x][y][FISH] == fish_num:
                return [x, y]
    return False


def dfs(sx, sy, eat, cur_graph):
    global answer

    # ํ˜„์žฌ ์ƒ์–ด๊ฐ€ ์žˆ๋Š” ์œ„์น˜์˜ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์Œ.
    eat += cur_graph[sx][sy][FISH]
    cur_graph[sx][sy][FISH] = 0

    # 1๋ฒˆ๋ถ€ํ„ฐ 16๋ฒˆ๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด๋™๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ.
    for fish_num in range(1, 17):
        loc = find_fish(cur_graph, fish_num)

        if not loc:
            continue

        fx, fy = loc[X], loc[Y]
        fish_dir = cur_graph[fx][fy][DIR]

        for z in range(8):
            # 45๋„์”ฉ ๋ฐฉํ–ฅ์„ ์ „ํ™˜ํ•ด๊ฐ€๋ฉฐ ์ ‘๊ทผํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ.
            next_dir = (fish_dir + z) % 8
            dx, dy = dirs[next_dir]
            fnx, fny = fx + dx, fy + dy

            # 4x4 ์˜์—ญ์˜ ๋ฐ–์ด๊ณ , ์ƒ์–ด์˜ ์œ„์น˜์™€ ๋™์ผํ•˜๋‹ค๋ฉด ์ด๋™ ๋ถˆ๊ฐ€!
            if not visitable(fnx, fny) or (fnx, fny) == (sx, sy):
                continue
            # ํ˜„์žฌ ๋ฌผ๊ณ ๊ธฐ์˜ ๋ฐฉํ–ฅ์„ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋ณ€๊ฒฝ.
            cur_graph[fx][fy][DIR] = next_dir
            # ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ฌผ๊ณ ๊ธฐ์™€ ์Šค์œ„์น˜ํ•จ.
            cur_graph[fx][fy], cur_graph[fnx][fny] = cur_graph[fnx][fny], cur_graph[fx][fy]
            break

    answer = max(answer, eat)

    shark_dir = cur_graph[sx][sy][DIR]
    # ํ˜„์žฌ ์ƒ์–ด์˜ ๋ฐฉํ–ฅ์—์„œ ์ตœ๋Œ€ 3์นธ ์•ž์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์Œ.
    for delta in range(1, 4):
        dx, dy = dirs[shark_dir]
        next_x, next_y = sx + dx * delta, sy + dy * delta

        if visitable(next_x, next_y) and cur_graph[next_x][next_y][FISH] > 0:
            dfs(next_x, next_y, eat, deepcopy(cur_graph))


if __name__ == "__main__":
    X, Y = 0, 1
    FISH, DIR = 0, 1
    answer = 0
    dirs = ((-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1))

    graph = [[[]] * 4 for _ in range(4)]

    for i in range(4):
        info = list(map(int, stdin.readline().split()))
        for j in range(4):
            graph[i][j] = [info[j * 2], info[j * 2 + 1] - 1]

    dfs(0, 0, 0, graph)
    print(answer)

 ๋ฐฉํ–ฅ์„ ํšŒ์ „ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” `%` (๋ชจ๋“ˆ๋Ÿฌ)๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๊ณ , ์ œํ•œ ์กฐ๊ฑด์— ๋งž์ถฐ ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋ฉด `๋งž์•˜์Šต๋‹ˆ๋‹ค!` ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

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