๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 16469 - ์†Œ๋…„ ์ ํ”„

by HandHand 2021. 3. 5.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 16469๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋„‰์‚ด, ์Šค์œ™์Šค, ์ฐฝ๋ชจ ์„ธ ์‚ฌ๋žŒ์˜ ์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋งˆ๋ฏธ์† ์ด ์œ„์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋งจ ์ฒ˜์Œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์€ ๋งˆ๋ฏธ์† ์€ ์„ธ ์‚ฌ๋žŒ์ด ๋ชจ์ผ ์ˆ˜ ์žˆ๋Š” ํ•œ ์ขŒํ‘œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๊ฐ’์ด ๊ฐ€์žฅ ์งง์€ ๊ณณ์— ์œ„์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
์ด๋ฅผ ์œ„ํ•ด์„œ ๊ฐ ์ ์— ๋Œ€ํ•ด์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(V^2*ElogV) ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๊ณ , ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ์˜ ์ ‘๊ทผ์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ƒ๊ฐํ•ด๋ณด๋ฉด ๋ชจ๋“  ์ขŒํ‘œ์—์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฅผ ์ˆ˜ํ–‰ํ•  ํ•„์š” ์—†์ด ๊ทธ๋ƒฅ ๋„‰์‚ด, ์Šค์œ™์Šค, ์ฐฝ๋ชจ ์„ธ ์‚ฌ๋žŒ์„ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ,
๊ฐ ์ขŒํ‘œ๋“ค์— ๋Œ€ํ•ด์„œ ๊ฑฐ๋ฆฌ ์ตœ๋Œ“๊ฐ’๋ผ๋ฆฌ ๋น„๊ตํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๊ทธ๋ƒฅ BFS ๋ฅผ ์ด์šฉํ•ด์„œ ์„ธ ์‚ฌ๋žŒ์„ ์‹œ์ž‘์ง€์ ์œผ๋กœ ํ•˜๋Š” ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฑฐ๋ฆฌ ๊ฐ’์„ dist 3์ฐจ์› ๋ฐฐ์—ด์— ๊ธฐ๋กํ–ˆ์Šต๋‹ˆ๋‹ค.
๋„‰์‚ด, ์Šค์œ™์Šค, ์ฐฝ๋ชจ ๊ฐ๊ฐ์€ 0, 1, 2 ๋ผ๋Š” id ๊ฐ’์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

R, C = list(map(int, sys.stdin.readline().split()))
maze = [list(sys.stdin.readline().strip()) for _ in range(R)]
nucksal = list(map(int, sys.stdin.readline().split()))
swings = list(map(int, sys.stdin.readline().split()))
changmo = list(map(int, sys.stdin.readline().split()))

dr = [0, 0, 1, -1]
dc = [1, -1, 0, 0]


def in_range(r, c):
    return 0 <= r < R and 0 <= c < C


def bfs(id, r, c, dist):
    visit = [[0] * C for _ in range(R)]
    q = deque()

    dist[r][c][id] = 0
    visit[r][c] = 1
    q.append([r, c, 0])

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

        for i in range(4):
            nr, nc = r + dr[i], c + dc[i]
            if in_range(nr, nc) and not visit[nr][nc] and maze[nr][nc] == '0':
                dist[nr][nc][id] = cost + 1
                visit[nr][nc] = 1
                q.append([nr, nc, cost + 1])

    return sys.maxsize


def solution():
    dist = [[[-1] * 3 for _ in range(C)] for _ in range(R)]

    for id, [r, c] in enumerate([nucksal, swings, changmo]):
        bfs(id, r - 1, c - 1, dist)

    min_cost, count = sys.maxsize, 0
    for r in range(R):
        for c in range(C):
            if all(map(lambda x: x != -1, dist[r][c])):
                cost = max(dist[r][c])
                if cost < min_cost:
                    min_cost = cost
                    count = 1
                elif cost == min_cost:
                    count += 1

    if min_cost != sys.maxsize:
        print(min_cost, count, sep='\n')
    else:
        print(-1)

solution()
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 8972 - ๋ฏธ์นœ ์•„๋‘์ด๋…ธ  (0) 2021.03.08
BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ  (0) 2021.03.08
BOJ 13565 - ์นจํˆฌ  (0) 2021.03.05
BOJ 1105 - ํŒ”  (0) 2021.03.03
BOJ 16509 - ์žฅ๊ตฐ  (0) 2021.03.03

๐Ÿ’ฌ ๋Œ“๊ธ€