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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

19238๋ฒˆ: ์Šคํƒ€ํŠธ ํƒ์‹œ

์ฒซ ์ค„์— N, M, ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐ ์—ฐ๋ฃŒ์˜ ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 20, 1 ≤ M ≤ N2, 1 ≤ ์ดˆ๊ธฐ ์—ฐ๋ฃŒ ≤ 500,000) ์—ฐ๋ฃŒ๋Š” ๋ฌดํ•œํžˆ ๋งŽ์ด ๋‹ด์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ดˆ๊ธฐ ์—ฐ๋ฃŒ์˜ ์–‘์„ ๋„˜์–ด์„œ ์ถฉ์ „๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ๋ณด๋ฉด, ์ฒซ ๋ฒˆ์งธ BFS๋ฅผ ํƒ์ƒ‰์„ ํ†ตํ•ด ํƒ์‹œ์˜ ์œ„์น˜๋กœ๋ถ€ํ„ฐ ์†๋‹˜์˜ ์œ„์น˜๊นŒ์ง€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ BFS ํƒ์ƒ‰์„ ํ†ตํ•ด ์†๋‹˜์˜ ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์ฐพ๋Š”๋‹ค.

 

 ๋‹จ์ˆœํžˆ BFS๋ฅผ ๋‘ ๋ฒˆ ์ƒ๊ฐํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ์ง€๋งŒ ๋ฌธ์ œ์— ์ œ์‹œ๋˜๋Š” ์กฐ๊ฑด๋“ค์„ ์ง€์ผœ์•ผ ํ•œ๋‹ค.

  1. ํƒ์‹œ์™€ ์†๋‹˜๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๊ณ , ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€๊นŒ์šด ์†๋‹˜๋ถ€ํ„ฐ ํƒœ์šด๋‹ค.
    • ์†๋‹˜์ด ์žˆ๋Š” ๊ณณ ๊นŒ์ง€ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
      • ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์€ ์†๋‹˜์ด ์—ฌ๋Ÿฌ ๋ช…์ผ ๊ฒฝ์šฐ, `[๊ฑฐ๋ฆฌ, ํ–‰, ์—ด]`์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์†๋‹˜์„ ํƒœ์šด๋‹ค.
    • ์†๋‹˜์ด ์žˆ๋Š” ๊ณณ ๊นŒ์ง€ ์ด๋™ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
      • ์—ฐ๋ฃŒ๊ฐ€ 0์ด ๋œ ๊ฒฝ์šฐ๋กœ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. ์†๋‹˜์˜ ๋ชฉ์ ์ง€๊นŒ์ง€ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํŒŒ์•…ํ•œ๋‹ค.
    • ํƒ์‹œ์˜ ์ถœ๋ฐœ์œ„์น˜๋Š” ์†๋‹˜์ด ์žˆ๋Š” ์œ„์น˜๊ฐ€ ๋œ๋‹ค.
    • ์†๋‹˜์˜ ๋ชฉ์ ์ง€ ๊นŒ์ง€ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
      • ๋‹ค์Œ ์Šน๊ฐ์„ ํƒœ์šฐ๊ธฐ ์œ„ํ•ด, ํƒ์‹œ์˜ ์œ„์น˜๊ฐ€ ์†๋‹˜์˜ ๋ชฉ์ ์ง€๊ฐ€ ๋œ๋‹ค.
      • ์—ฐ๋ฃŒ๋Š” ๋ชฉ์ ์ง€ ๊นŒ์ง€ ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ * 2 ๋งŒํผ ์ถฉ์ „ํ•œ๋‹ค.
        • ์ด๋Š” ํ˜„์žฌ์—ฐ๋ฃŒ - ์‚ฌ์šฉํ•œ ์—ฐ๋ฃŒ + ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ * 2์ด์ง€๋งŒ, ํ˜„์žฌ ์—ฐ๋ฃŒ + ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ์™€๋„ ๊ฐ™๋‹ค.
    • ์†๋‹˜์˜ ๋ชฉ์ ์ง€๊นŒ์ง€ ์ด๋™ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
      • ์—ฐ๋ฃŒ๊ฐ€ 0์ด ๋œ ๊ฒฝ์šฐ๋กœ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 ์ฒ˜์Œ์— ํ’€ ๋•Œ, `์†๋‹˜๋งˆ๋‹ค BFS๋กœ ํƒ์ƒ‰ ํ›„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒ`ํ–ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ๋‹ค๋ฅธ ๋กœ์ง์—์„œ ์‹œ๊ฐ„์„ ์žก์•„๋จน๋Š” ์ค„ ์•Œ๊ณ  ํ•œ ์‹œ๊ฐ„ ๋™์•ˆ ๋””๋ฒ„๊น…์„ ํ–ˆ๋Š”๋ฐ... ๋„์ €ํžˆ ๋ชป ์ฐพ๊ฒ ์–ด์„œ ๋ฐฑ์ค€์˜ ์งˆ๋ฌธ ๋‹ต๋ณ€ ๊ฒŒ์‹œํŒ์„ ์ฐธ๊ณ ํ•˜์˜€๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋‹ˆ, ํƒ์‹œ ์œ„์น˜๋กœ๋ถ€ํ„ฐ ์†๋‹˜์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๋ฐ๋Š” BFS๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๋Œ๋ฉด ๋˜๋Š”๋ฐ ์™œ ์†๋‹˜๋งŒํผ ๋Œ๊ณ  ์žˆ์—ˆ๋Š”๊ฐ€ ์‹ถ์—ˆ๋‹ค.๐Ÿ˜‚

 

์ฝ”๋“œ

from sys import stdin
from collections import deque

WALL = 1


def visitable(r, c):
    return 0 <= r < N and 0 <= c < N and board[r][c] != WALL


def bfs(t_r, t_c):
    visited = [[0] * N for _ in range(N)]
    q = deque([[t_r, t_c]])
    visited[t_r][t_c] = 1

    while q:
        r, c = q.popleft()

        for dr, dc in dirs:
            next_r, next_c = r + dr, c + dc
            if visitable(next_r, next_c) and not visited[next_r][next_c]:
                visited[next_r][next_c] = visited[r][c] + 1
                q.append((next_r, next_c))

    return visited


def find_passenger():
    dist_board = bfs(*taxi)
    p_dist = []
    for idx, info in enumerate(passengers):
        # ์†๋‹˜ ์ค‘๋ณต ํ™•์ธ ๋ฐฉ์ง€.
        if pick_up[idx]:
            continue

        # ํƒ์‹œ ์œ„์น˜๋ฅผ 1๋กœ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ, -1ํ•ด์ค˜์•ผ ํ•จ
        cur_dist = dist_board[info[0]][info[1]] - 1

        # ์—ฐ๋ฃŒ๊ฐ€ ์—†๊ณ  ๋ฐฉ๋ฌธ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ์ค‘๋‹จ
        if fuel - cur_dist <= 0 or cur_dist == -1:
            print(-1)
            exit(0)
        p_dist.append([cur_dist, info[0], info[1], idx])

    # 2. ๊ฑฐ๋ฆฌ, R, C ์ˆœ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜์˜€์œผ๋ฏ€๋กœ ์ •๋ ฌํ•˜๋ฉด ์ˆœ์ฐจ์ ์œผ๋กœ ๋จ.
    # ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์Šน๊ฐ๋งŒ ๋ฐ˜ํ™˜.
    return sorted(p_dist)[0]


def move_passenger(p_dist):
    global fuel, taxi

    dist, _, _, idx = p_dist
    # ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์Šน๊ฐ์ด ์žˆ๋Š” ์œ„์น˜๋กœ ์ด๋™
    taxi = passengers[idx][:2]
    # ์Šน๊ฐ์ด ์žˆ๋Š” ๊ณณ ๊นŒ์ง€ ์‚ฌ์šฉํ•œ ์—ฐ๋ฃŒ ๊ฐ์†Œ
    fuel -= dist
    # ์†๋‹˜ pick up check
    pick_up[idx] = 1

    dist_board = bfs(*taxi)
    # ์Šน๊ฐ์˜ ๋ชฉ์ ์ง€์— ๋„์ฐฉ ๋„๋‹ฌํ•˜์˜€๋Š”์ง€ ํ™•์ธ
    e_r, e_c = passengers[idx][2:]
    cur_dist = dist_board[e_r][e_c] - 1

    # ์—ฐ๋ฃŒ๊ฐ€ ์—†๊ณ , ๋ฐฉ๋ฌธ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ค‘๋‹จ.
    if fuel - cur_dist <= 0 or cur_dist == -1:
        print(-1)
        exit(0)

    # ํƒ์‹œ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์†๋‹˜์˜ ๋ชฉ์ ์ง€๋กœ ๋ณ€๊ฒฝ
    taxi = [e_r, e_c]
    # ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ * 2 ๋งŒํผ ์—ฐ๋ฃŒ๊ฐ€ ์ถฉ์ „๋จ.
    # ์—ฐ๋ฃŒ๋ฅผ ๊ฐ์†Œ์‹œํ‚ค์ง€ ์•Š๊ณ  ๋”ํ•˜๋ฉด ๋™์ผํ•œ ๊ฒฐ๊ณผ
    fuel += cur_dist


if __name__ == '__main__':
    N, M, fuel = map(int, stdin.readline().split())
    board = [list(map(int, stdin.readline().split())) for _ in range(N)]
    taxi = list(map(int, stdin.readline().split()))
    # list index์— ๋งž๊ฒŒ 1์”ฉ ๊ฐ์†Œ
    taxi = [taxi[0] - 1, taxi[1] - 1]
    passengers = [[0] for _ in range(M)]
    pick_up = [0] * M

    for idx in range(M):
        s_r, s_c, e_r, e_c = map(int, stdin.readline().split())
        # list index์— ๋งž๋„๋ก 1์”ฉ ๊ฐ์†Œ
        passengers[idx] = [s_r - 1, s_c - 1, e_r - 1, e_c - 1]

    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))

    for _ in range(M):
        # 1. ํƒ์‹œ์™€ ์†๋‹˜์˜ ๊ฑฐ๋ฆฌ ์ฐพ๊ณ , ์กฐ๊ฑด์— ์ ํ•ฉํ•œ ์Šน๊ฐ ์ฐพ๊ธฐ
        cur_passenger = find_passenger()
        # 2. ์†๋‹˜ ๋ชฉ์ ์ง€ ๊นŒ์ง€ ๋ฐ๋ ค๋‹ค ์ฃผ๊ธฐ
        move_passenger(cur_passenger)

    print(fuel)

 

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