๐Ÿ”ฅ ๋ถˆ!

๋ฐฑ์ค€ Gold4

๋ถˆ!

๋ฌธ์ œ


์ง€ํ›ˆ์ด๋Š” ๋ฏธ๋กœ์—์„œ ์ผ์„ ํ•œ๋‹ค. ์ง€ํ›ˆ์ด๋ฅผ ๋ฏธ๋กœ์—์„œ ํƒˆ์ถœํ•˜๋„๋ก ๋„์™€์ฃผ์ž!

๋ฏธ๋กœ์—์„œ์˜ ์ง€ํ›ˆ์ด์˜ ์œ„์น˜์™€ ๋ถˆ์ด ๋ถ™์€ ์œ„์น˜๋ฅผ ๊ฐ์•ˆํ•ด์„œ ์ง€ํ›ˆ์ด๊ฐ€ ๋ถˆ์— ํƒ€๊ธฐ์ „์— ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š”์ง€์˜ ์—ฌ๋ถ€, ๊ทธ๋ฆฌ๊ณ  ์–ผ๋งˆ๋‚˜ ๋นจ๋ฆฌ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์•ผํ•œ๋‹ค.

์ง€ํ›ˆ์ด์™€ ๋ถˆ์€ ๋งค ๋ถ„๋งˆ๋‹ค ํ•œ์นธ์”ฉ ์ˆ˜ํ‰๋˜๋Š” ์ˆ˜์ง์œผ๋กœ(๋น„์Šค๋“ฌํ•˜๊ฒŒ ์ด๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค) ์ด๋™ํ•œ๋‹ค.

๋ถˆ์€ ๊ฐ ์ง€์ ์—์„œ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ํ™•์‚ฐ๋œ๋‹ค.

์ง€ํ›ˆ์ด๋Š” ๋ฏธ๋กœ์˜ ๊ฐ€์žฅ์ž๋ฆฌ์— ์ ‘ํ•œ ๊ณต๊ฐ„์—์„œ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง€ํ›ˆ์ด์™€ ๋ถˆ์€ ๋ฒฝ์ด ์žˆ๋Š” ๊ณต๊ฐ„์€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค.

์ž…๋ ฅ


์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์—๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋‘ ์ •์ˆ˜ R๊ณผ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ, 1 โ‰ค R, C โ‰ค 1000 ์ด๋‹ค. R์€ ๋ฏธ๋กœ ํ–‰์˜ ๊ฐœ์ˆ˜, C๋Š” ์—ด์˜ ๊ฐœ์ˆ˜์ด๋‹ค.

๋‹ค์Œ ์ž…๋ ฅ์œผ๋กœ R์ค„๋™์•ˆ ๊ฐ๊ฐ์˜ ๋ฏธ๋กœ ํ–‰์ด ์ฃผ์–ด์ง„๋‹ค.

๊ฐ๊ฐ์˜ ๋ฌธ์ž๋“ค์€ ๋‹ค์Œ์„ ๋œปํ•œ๋‹ค.

  • #: ๋ฒฝ
  • .: ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„
  • J: ์ง€ํ›ˆ์ด์˜ ๋ฏธ๋กœ์—์„œ์˜ ์ดˆ๊ธฐ์œ„์น˜ (์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„)
  • F: ๋ถˆ์ด ๋‚œ ๊ณต๊ฐ„ J๋Š” ์ž…๋ ฅ์—์„œ ํ•˜๋‚˜๋งŒ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ


์ง€ํ›ˆ์ด๊ฐ€ ๋ถˆ์ด ๋„๋‹ฌํ•˜๊ธฐ ์ „์— ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ IMPOSSIBLE ์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ง€ํ›ˆ์ด๊ฐ€ ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ํƒˆ์ถœ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

4 4
####
#JF#
#..#
#..#

์˜ˆ์ œ ์ถœ๋ ฅ 1

3

๐Ÿ“– ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฌธ์ œ์—์„œ ๋ฌป๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€ ๋ฏธ๋กœ์—์„œ ์ง€ํ›ˆ์ด๊ฐ€ ๊ฐ€์žฅ์ž๋ฆฌ๋กœ ์–ผ๋งˆ๋‚˜ ๋นจ๋ฆฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€์ด๋‹ค.

๋‹ค์‹œ ๋งํ•ด, Weight๊ฐ€ ์—†๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๊ณ  [ํŠน์ • ๋…ธ๋“œ์—์„œ ํŠน์ • ๋…ธ๋“œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ] ๋ฌธ์ œ๋กœ ์น˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” ์‰ฝ๊ฒŒ BFS๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, ์ฃผ์˜ํ•  ์ ์€ ์ง€ํ›ˆ์ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ถˆ์˜ ์ด๋™๋„ ๋™์‹œ์— ๊ณ ๋ คํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

๐Ÿ“ ์„ค๊ณ„

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-34

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-35 2

TRY 1, ์‹œ๊ฐ„ ์ดˆ๊ณผ

์ง€ํ›ˆ์ด์™€ ๋ถˆ๋กœ๋ถ€ํ„ฐ BFS๋ฅผ ์ถœ๋ฐœํ•˜์—ฌ ์‹œ๊ฐ„์„ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋ชจ๋“  BFS ํƒ์ƒ‰์„ ๋๋งˆ์น˜๊ณ  ๊ฐ€์žฅ์ž๋ฆฌ์— ์œ„์น˜ํ•˜๋ฉฐ ๋ฒฝ๊ณผ ๋ถˆ์„ ์ œ์™ธํ•œ ์‹œ๊ฐ„๋“ค ์ค‘์—์„œ ์ œ์ผ ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„์„œ ์ถœ๋ ฅํ•œ๋‹ค. ์ด ๋•Œ, ํƒˆ์ถœํ•˜๊ธฐ ์œ„ํ•œ ๋งˆ์ง€๋ง‰ 1๋ถ„๋„ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋‹ˆ +1๋ถ„์„ ํ•ด์ค€๋‹ค.

TRY 2, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ

1ํŠธ์—์„œ๋Š” BFS ํƒ์ƒ‰ ์ „๋ถ€ ๋๋งˆ์น˜๊ณ  ์‹œ๊ฐ„์„ ์ฐพ์•˜๋Š”๋ฐ 2ํŠธ์—์„œ๋Š” while๋ฌธ ์ค‘๊ฐ„์— ์ง€ํ›ˆ์ด๊ฐ€ ๊ฐ€์žฅ์ž๋ฆฌ์— ๋„๋‹ฌํ–ˆ์„ ๊ฒฝ์šฐ ๋ฐ”๋กœ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•˜์˜€๋‹ค.

# ์ง€ํ›ˆ์ด ํƒˆ์ถœ ์กฐ๊ฑด
        if (x == 0 or x == R-1 or y == 0 or y == C-1) and  who == 'J':
            return count[x][y] + 1

TRY 3, ์ •๋‹ต

์†”๋ฃจ์…˜ ์ฐธ๊ณ : https://velog.io/@hygge/Python-๋ฐฑ์ค€-4179-๋ถˆ-BFS

๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ์ตœ๋Œ€ํ•œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ์ค„์˜€๋‹ค. ์•„๋ž˜๋Š” 2ํŠธ์—์„œ ์‚ญ์ œํ•œ ๋ณ€์ˆ˜๋“ค์ด๋‹ค.

๋‚˜๋ฆ„ ์ค„์ธ๋‹ค๊ณ  ์ค„์˜€๋Š”๋ฐ ๋” ์ด์ƒ ๋ญ˜ ๋” ์ค„์—ฌ์•ผํ• ์ง€ ๋ชฐ๋ผ์„œ ์†”๋ฃจ์…˜์„ ์ฐธ๊ณ ํ•˜์˜€๋‹ค. ๐Ÿ˜ก

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-07-27 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 6 32 59

  • ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ visited ๋ฐฐ์—ด
  • ๋ถˆ๊ณผ ์ง€ํ›ˆ์ด๋ฅผ ํŒ๋ณ„ํ•˜๋Š” who
  • ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜๋Š” time ๋ฐฐ์—ด
  • ๋ถˆ์˜ ์œ„์น˜๋ฅผ ๋‹ด๋Š” fire ๋ฐฐ์—ด

์ตœ๋Œ€ํ•œ Queue ํ•˜๋‚˜๋กœ ์ „๋ถ€ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

TRY 1

import sys
input = sys.stdin.readline
from collections import deque

R,C = map(int,input().split())
maze = [] # ๊ทธ๋ž˜ํ”„
dx = [1,-1,0,0]
dy = [0,0,1,-1]
fire = [] # ๋ถˆ ์œ„์น˜
edge = [] # ๊ฐ€์žฅ์ž๋ฆฌ

for i in range(R):
    maze.append(list(input().rstrip()))

# ์ง€ํ›ˆ,๋ถˆ ์œ„์น˜ ์ฐพ๊ธฐ
for i in range(R):
    for j in range(C):
        if maze[i][j] == 'J':
            j_x,j_y = (i,j)
        if maze[i][j] == 'F':
            fire.append((i,j))

# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ฐพ๊ธฐ
def bfs(a,b):
    q = deque([])
    q.append([(a,b),'J'])
    for i in range(len(fire)):
        q.append([(fire[i][0],fire[i][1]),'F'])
    visited = [[False]*C for _ in range(R)]
    count = [[0]*C for _ in range(R)]

    while q:
        temp = q.popleft()
        x,y = temp[0]
        who = temp[1]

        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]

            if 0<=nx<R and 0<=ny<C and visited[nx][ny] == False and (nx,ny) not in fire:
                if maze[nx][ny] == '.':
                    if who == 'J': 
                        q.append([(nx,ny),'J'])
                        visited[nx][ny] = True
                        count[nx][ny] = count[x][y] + 1
                    if who == 'F':
                        q.append([(nx,ny),'F'])
                        fire.append((nx,ny))
    
    return count

distance = bfs(j_x,j_y)
answer = []
# ๊ฐ€์žฅ์ž๋ฆฌ ์ตœ๋‹จ๊ฑฐ๋ฆฌ
for i in range(R):
    if i == 0 or i == R-1:
        for j in range(C):
            if distance[i][j] > 0:
                answer.append(distance[i][j])
    else:
        if distance[i][0] > 0:
            answer.append(distance[i][0])
        if distance[i][C-1] > 0:
            answer.append(distance[i][C-1])

if not answer:
    print("IMPOSSIBLE")
else:
    print(min(answer)+1)

TRY 2

import sys
input = sys.stdin.readline
from collections import deque

R,C = map(int,input().split())
maze = [] # ๊ทธ๋ž˜ํ”„
dx = [1,-1,0,0]
dy = [0,0,1,-1]
fire = [] # ๋ถˆ ์œ„์น˜
edge = [] # ๊ฐ€์žฅ์ž๋ฆฌ

for i in range(R):
    maze.append(list(input().rstrip()))

# ์ง€ํ›ˆ,๋ถˆ ์œ„์น˜ ์ฐพ๊ธฐ
for i in range(R):
    for j in range(C):
        if maze[i][j] == 'J':
            j_x,j_y = (i,j)
        if maze[i][j] == 'F':
            fire.append((i,j))

# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ฐพ๊ธฐ
def bfs(a,b):
    q = deque([])
    q.append([(a,b),'J'])
    for i in range(len(fire)):
        q.append([(fire[i][0],fire[i][1]),'F'])
    visited = [[False]*C for _ in range(R)]
    count = [[0]*C for _ in range(R)]

    while q:
        temp = q.popleft()
        x,y = temp[0]
        who = temp[1]

        # ์ง€ํ›ˆ์ด ํƒˆ์ถœ ์กฐ๊ฑด
        if (x == 0 or x == R-1 or y == 0 or y == C-1) and  who == 'J':
            return count[x][y] + 1

        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]

            if 0<=nx<R and 0<=ny<C and visited[nx][ny] == False and maze[nx][ny] != '#':
                if who == 'J' and maze[nx][ny] == '.': 
                    q.append([(nx,ny),'J'])
                    visited[nx][ny] = True
                    count[nx][ny] = count[x][y] + 1
                if who == 'F' and maze[nx][ny] != 'F':
                    q.append([(nx,ny),'F'])
                    maze[nx][ny] == 'F'

    return None

count = bfs(j_x,j_y)

if count == None:
    print("IMPOSSIBLE")
else:
    print(count)

TRY 3

import sys
input = sys.stdin.readline
from collections import deque

R,C = map(int,input().split())
maze = [] # ๊ทธ๋ž˜ํ”„
dx = [1,-1,0,0]
dy = [0,0,1,-1]

q = deque([])
for i in range(R):
    maze.append(list(input().rstrip()))
    # ์ง€ํ›ˆ ์œ„์น˜ ์ฐพ๊ธฐ
    if 'J' in maze[i]:
        q.append((i,maze[i].index('J'),0))

# ๋ถˆ ์œ„์น˜ ์ฐพ๊ธฐ
for i in range(R):
    for j in range(C):
        if maze[i][j] == 'F':
            q.append((i,j,-1))

answer = "IMPOSSIBLE"

while q:
    x,y,time = q.popleft()

    # ์ง€ํ›ˆ์ด ํƒˆ์ถœ ์กฐ๊ฑด
    if (x == 0 or x == R-1 or y == 0 or y == C-1) and  maze[x][y] != 'F' and time > -1:
        answer = time + 1
        break

    for i in range(4):
        nx = x+dx[i]
        ny = y+dy[i]

        if 0<=nx<R and 0<=ny<C and maze[nx][ny] != '#':
            # ์ง€ํ›ˆ์ด
            if time > -1 and maze[nx][ny] == '.': 
                q.append((nx,ny,time+1))
                maze[nx][ny] = 'V'
            # ๋ถˆ
            elif time == -1 and maze[nx][ny] != 'F':
                q.append((nx,ny,-1))
                maze[nx][ny] = 'F'

print(answer)

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6