[BOJ][๐ŸŸก4][๋ฐฑ์ค€#05427] ๋ถˆ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #5427


๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ๋นˆ ๊ณต๊ฐ„๊ณผ ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฑด๋ฌผ์— ๊ฐ‡ํ˜€์žˆ๋‹ค. ๊ฑด๋ฌผ์˜ ์ผ๋ถ€์—๋Š” ๋ถˆ์ด ๋‚ฌ๊ณ , ์ƒ๊ทผ์ด๋Š” ์ถœ๊ตฌ๋ฅผ ํ–ฅํ•ด ๋›ฐ๊ณ  ์žˆ๋‹ค. ๋งค ์ดˆ๋งˆ๋‹ค, ๋ถˆ์€ ๋™์„œ๋‚จ๋ถ ๋ฐฉํ–ฅ์œผ๋กœ ์ธ์ ‘ํ•œ ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ„๋‹ค. ๋ฒฝ์—๋Š” ๋ถˆ์ด ๋ถ™์ง€ ์•Š๋Š”๋‹ค. ์ƒ๊ทผ์ด๋Š” ๋™์„œ๋‚จ๋ถ ์ธ์ ‘ํ•œ ์นธ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, 1์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค. ์ƒ๊ทผ์ด๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๊ณ , ๋ถˆ์ด ์˜ฎ๊ฒจ์ง„ ์นธ ๋˜๋Š” ์ด์ œ ๋ถˆ์ด ๋ถ™์œผ๋ ค๋Š” ์นธ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†๋‹ค. ์ƒ๊ทผ์ด๊ฐ€ ์žˆ๋Š” ์นธ์— ๋ถˆ์ด ์˜ฎ๊ฒจ์˜ด๊ณผ ๋™์‹œ์— ๋‹ค๋ฅธ ์นธ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋นŒ๋”ฉ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์–ผ๋งˆ๋‚˜ ๋นจ๋ฆฌ ๋นŒ๋”ฉ์„ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ตœ๋Œ€ 100๊ฐœ์ด๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๋นŒ๋”ฉ ์ง€๋„์˜ ๋„ˆ๋น„์™€ ๋†’์ด w์™€ h๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค w,h โ‰ค 1000) ๋‹ค์Œ h๊ฐœ ์ค„์—๋Š” w๊ฐœ์˜ ๋ฌธ์ž, ๋นŒ๋”ฉ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

โ€™.โ€™: ๋นˆ ๊ณต๊ฐ„ โ€˜#โ€™: ๋ฒฝ โ€˜@โ€™: ์ƒ๊ทผ์ด์˜ ์‹œ์ž‘ ์œ„์น˜ โ€˜*โ€™: ๋ถˆ

๊ฐ ์ง€๋„์— @์˜ ๊ฐœ์ˆ˜๋Š” ํ•˜๋‚˜์ด๋‹ค.


์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋นŒ๋”ฉ์„ ํƒˆ์ถœํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋นŒ๋”ฉ์„ ํƒˆ์ถœํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” โ€œIMPOSSIBLEโ€์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5
4 3
####
#*@.
####
7 6
###.###
#*#.#*#
#.....#
#.....#
#..@..#
#######
7 4
###.###
#....*#
#@....#
.######
5 5
.....
.***.
.*@*.
.***.
.....
3 3
###
#@#
###


์ถœ๋ ฅ

2
5
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE


My Sol

from collections import deque

def findsang():
    global mat
    for i in range(I):
        for j in range(J):
            if mat[i][j]=='@':
                return i, j
            
def main():
    # ๋ถˆ ์ขŒํ‘œ ํ™•์ธ
    fires = deque()
    fl = 0
    for i in range(I):
        for j in range(J):
            if mat[i][j] == '*':
                fires.append((i, j))
                fl += 1

    # ์ƒ๊ทผ ์ขŒํ‘œ ํ™•์ธ
    ni, nj = findsang()
    sangmoves = deque()
    sangmoves.append((ni, nj))
    ml = 1

    # visit
    firevisit = [[0]*J for _ in range(I)]
    for fi, fj in fires:
        firevisit[fi][fj] = 1
    sangvisit = [[0]*J for _ in range(I)]
    sangvisit[ni][nj] = 1

    time = 1
    while sangmoves:
        # ๋ถˆ ์ด๋™
        for _ in range(fl):
            fi, fj = fires.popleft()
            fl -= 1

            for di, dj in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                sfi, sfj = fi+di, fj+dj
                if not (0<=sfi<I and 0<=sfj<J): continue
                if firevisit[sfi][sfj]: continue
                if mat[sfi][sfj] == '#': continue
                firevisit[sfi][sfj] = 1
                fires.append((sfi, sfj))
                fl += 1

        # ์ƒ๊ทผ ์ด๋™
        for _ in range(ml):
            ni, nj = sangmoves.popleft()
            ml -= 1

            for di, dj in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                si, sj = ni+di, nj+dj
                if not (0<=si<I and 0<=sj<J): return time
                if firevisit[si][sj]: continue
                if sangvisit[si][sj]: continue
                if mat[si][sj]=='#': continue
                sangvisit[si][sj] = 1
                sangmoves.append((si, sj))
                ml += 1
        time += 1

    return 'IMPOSSIBLE'

T = int(input())
for _ in range(T):
    J, I = map(int, input().split())
    mat = [list(input()) for _ in range(I)]
    print(main())

BFS๋ฅผ 2์ค‘์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ถˆ์ด ์ด๋™ํ•˜๊ณ , ์ƒ๊ทผ์ด๊ฐ€ ์ด๋™ํ•˜๋Š” ์ƒํ™ฉ์„ ๊ณ ๋ คํ•ด์„œ ์ƒ๊ทผ์ด๊ฐ€ ๋ฒ”์œ„ ์™ธ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํƒˆ์ถœํ•˜๊ธฐ๊นŒ์ง€ while๋ฌธ์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ƒ๊ทผ์ด๊ฐ€ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ์˜ ์ง‘ํ•ฉํ์ธ sangmoves๊ฐ€ ๋นˆ๋‹ค๋ฉด, ๋ถˆ์ด๋‚˜ ์—ฌ๋Ÿฌ ์š”์ธ์œผ๋กœ ์ƒ๊ทผ์ด๊ฐ€ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ IMPOSSIBLE์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด ๋ฐ˜ํ™˜๊ฐ’์„ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ