๋ฌธ์
์ฌ์ ํ ์ํ์ ๊ตฐ์ฃผ ์ด๋ฏผํ์ ๋๋์ด ๋ง๋ฒ ๊ตฌ์ฌ์ ์์ ๋ฃ์๊ณ , ๊ทธ ๋ฅ๋ ฅ์ ์คํํด๋ณด๊ธฐ ์ํด ๊ทผ์ฒ์ ํฐ๋ฑ์ฒ์ ํ์๋ฅผ ์ผ์ผํค๋ ค๊ณ ํ๋ค. ์ด ์ฒ์๋ ๊ณ ์ด๋์น๊ฐ ํ ๋ง๋ฆฌ ์ด๊ณ ์๋ค. ๊ณ ์ด๋์น๋ ์ ์ผ ์นํ ์น๊ตฌ์ธ ๋น๋ฒ์ ๊ตด๋ก ๊ฐ๋ฅํ ๋นจ๋ฆฌ ๋๋ง๊ฐ ํ์๋ฅผ ํผํ๋ ค๊ณ ํ๋ค.
ํฐ๋ฑ์ฒ์ ์ง๋๋ Rํ C์ด๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๋น์ด์๋ ๊ณณ์ '.'๋ก ํ์๋์ด ์๊ณ , ๋ฌผ์ด ์ฐจ์๋ ์ง์ญ์ '*', ๋์ 'X'๋ก ํ์๋์ด ์๋ค. ๋น๋ฒ์ ๊ตด์ 'D'๋ก, ๊ณ ์ด๋์น์ ์์น๋ 'S'๋ก ๋ํ๋ด์ด์ ธ ์๋ค.
๋งค ๋ถ๋ง๋ค ๊ณ ์ด๋์น๋ ํ์ฌ ์๋ ์นธ๊ณผ ์ธ์ ํ ๋ค ์นธ ์ค ํ๋๋ก ์ด๋ํ ์ ์๋ค. (์, ์๋, ์ค๋ฅธ์ชฝ, ์ผ์ชฝ) ๋ฌผ๋ ๋งค ๋ถ๋ง๋ค ๋น์ด์๋ ์นธ์ผ๋ก ํ์ฅํ๋ค. ๋ฌผ์ด ์๋ ์นธ๊ณผ ์ธ์ ํด์๋ ๋น์ด์๋ ์นธ(์ ์ด๋ ํ ๋ณ์ ๊ณต์ )์ ๋ฌผ์ด ์ฐจ๊ฒ ๋๋ค. ๋ฌผ๊ณผ ๊ณ ์ด๋์น๋ ๋์ ํต๊ณผํ ์ ์๋ค. ๋, ๊ณ ์ด๋์น๋ ๋ฌผ๋ก ์ฐจ์๋ ๊ตฌ์ญ์ผ๋ก ์ด๋ํ ์ ์๊ณ , ๋ฌผ๋ ๋น๋ฒ์ ์๊ตด๋ก ์ด๋ํ ์ ์๋ค.
ํฐ๋ฑ์ฒ์ ์ง๋๊ฐ ์ฃผ์ด์ก์ ๋, ๊ณ ์ด๋์น๊ฐ ์์ ํ๊ฒ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ๊ธฐ ์ํด ํ์ํ ์ต์ ์๊ฐ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
๊ณ ์ด๋์น๋ ๋ฌผ์ด ์ฐฐ ์์ ์ธ ์นธ์ผ๋ก ์ด๋ํ ์ ์๋ค. ์ฆ, ๋ค์ ์๊ฐ์ ๋ฌผ์ด ์ฐฐ ์์ ์ธ ์นธ์ผ๋ก ๊ณ ์ด๋์น๋ ์ด๋ํ ์ ์๋ค. ์ด๋ํ ์ ์์ผ๋ฉด ๊ณ ์ด๋์น๊ฐ ๋ฌผ์ ๋น ์ง๊ธฐ ๋๋ฌธ์ด๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์ R๊ณผ C๊ฐ ์ฃผ์ด์ง๋ค.
๋ค์ R๊ฐ ์ค์๋ ํฐ๋ฑ์ฒ์ ์ง๋๊ฐ ์ฃผ์ด์ง๋ฉฐ, ๋ฌธ์ ์์ ์ค๋ช ํ ๋ฌธ์๋ง ์ฃผ์ด์ง๋ค. 'D'์ 'S'๋ ํ๋์ฉ๋ง ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ๊ณ ์ด๋์น๊ฐ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ ์ ์๋ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ์ ์ถ๋ ฅํ๋ค. ๋ง์ฝ, ์์ ํ๊ฒ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ ์ ์๋ค๋ฉด, "KAKTUS"๋ฅผ ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ 1 ๋ณต์ฌ
3 3
D.*
...
.S.
์์ ์ถ๋ ฅ 1 ๋ณต์ฌ
3
์์ ์ ๋ ฅ 2 ๋ณต์ฌ
3 3
D.*
...
..S
์์ ์ถ๋ ฅ 2 ๋ณต์ฌ
KAKTUS
์์ ์ ๋ ฅ 3 ๋ณต์ฌ
3 6
D...*.
.X.X..
....S.
์์ ์ถ๋ ฅ 3 ๋ณต์ฌ
6
์์ ์ ๋ ฅ 4 ๋ณต์ฌ
5 4
.D.*
....
..X.
S.*.
....
์์ ์ถ๋ ฅ 4 ๋ณต์ฌ
4
# ํ์ด ๋ฐฉ๋ฒ
๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ์ ๋ฌผ์ด๋ดค๊ธฐ ๋๋ฌธ์ bfs๋ก ์ ๊ทผํ๋ค.
(1) ๋ฌผ ํ์ฅ (2) ๊ณ ์ด๋์น์ ์ด๋ < ์ด ๊ตฌ์กฐ๋ฅผ ๋ ์ฌ๋ฆฌ๋๊ฑด ์ด๋ ต์ง ์์๋๋ฐ
(2) ์์ queue๋ฅผ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋์ง ์์๊ธฐ ๋๋ฌธ์
๋ฌผ๊ณผ ๋ฌ๋ฆฌ ๊ณ ์ด๋์น๋ ํ์ ์๋ ๋ชจ๋ ์นธ์ ํ ํด์ ์ด๋ํ์ง ์์ ๊ฒฐ๊ณผ์ ์ผ๋ก ๋ฌผ์ด ๋๋ฌด ๋นจ๋ฆฌ ํผ์ง๋ ๊ฒ์ฒ๋ผ ๋์๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก ํ ํ์์ ํ๋ฒ๋ง ๋ฌผ์ด ํ์ฅ๋๊ณ , ๊ณ ์ด๋์น๊ฐ ์ด๋ํ๋ค๋๊ฑธ ๊ตฌํํ๋๊ฑฐ์์ ๋งํ์๋ค.
# ์ฝ๋
# 2025-04-15 11:55-12:40
import sys
from collections import deque
sys.stdin = open("input.txt","r")
r,c = map(int,sys.stdin.readline().split())
graph = [list(sys.stdin.readline().strip()) for _ in range(r)]
visited = [[False]*c for _ in range(r)]
# ์ํ์ข์ฐ
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# ๊ณ ์ด๋์น์ ๊ตด์ ์์น ์ธ๋ฑ์ค ๊ตฌํ๊ธฐ
water = deque()
for i in range(r):
for j in range(c):
if graph[i][j] == 'D':
tx, ty = i, j
elif graph[i][j] == 'S':
sx, sy = i, j
elif graph[i][j] == '*':
water.append((i,j))
def bfs(x,y,cnt):
cnt = 0
queue = deque()
queue.append((x,y,cnt))
while queue:
# ๋ฌผ์ ํ์ฅ
for _ in range(len(water)):
x, y = water.popleft()
for i in range(4):
wx, wy = x + dx[i], y + dy[i]
if 0 <= wx < r and 0 <= wy < c:
if graph[wx][wy] == '.':
graph[wx][wy] = '*'
water.append((wx,wy))
# ๊ณ ์ด๋์น์ ์ด๋
for _ in range(len(queue)):
x, y, cnt = queue.popleft()
visited[x][y] = True
# print((x,y,cnt))
if (x, y) == (tx, ty):
return cnt
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < r and 0 <= ny < c:
if (graph[nx][ny] == '.' or graph[nx][ny] == 'D') and not visited[nx][ny]:
visited[nx][ny] = True
queue.append((nx,ny,cnt+1))
return -1
cnt = bfs(sx,sy,0)
if cnt == -1: print('KAKTUS')
else: print(cnt)
# ์ฐธ๊ณ (๋ฐํ์ ์ค๋ฅ ํด๊ฒฐ)
๐ฝ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ
water, tmp_water๋ฅผ ์งํฉ์ผ๋ก ์ ์ํด์ ๋ฌธ์ ๊ฐ ๋์๋ค.
# 2025-04-15 11:55-12:40
import sys
from collections import deque
sys.stdin = open("input.txt","r")
r,c = map(int,sys.stdin.readline().split())
graph = [list(sys.stdin.readline().strip()) for _ in range(r)]
visited = [[False]*c for _ in range(r)]
# ์ํ์ข์ฐ
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# ๊ณ ์ด๋์น์ ๊ตด์ ์์น ์ธ๋ฑ์ค ๊ตฌํ๊ธฐ
water = set()
for i in range(r):
for j in range(c):
if graph[i][j] == 'D':
tx, ty = i, j
elif graph[i][j] == 'S':
sx, sy = i, j
elif graph[i][j] == '*':
water.add((i,j))
def bfs(x,y,cnt):
cnt = 0
queue = deque()
queue.append((x,y,cnt))
while queue:
# ๋ฌผ์ ํ์ฅ
tmp_water = set()
for w in water:
for i in range(4):
wx, wy = w[0] + dx[i], w[1] + dy[i]
if 0 <= wx < r and 0 <= wy < c:
if graph[wx][wy] == '.':
graph[wx][wy] = '*'
tmp_water.add((wx,wy))
water.update(tmp_water)
for _ in range(len(queue)):
x, y, cnt = queue.popleft()
visited[x][y] = True
# print((x,y,cnt))
if (x, y) == (tx, ty):
return cnt
# ๊ณ ์ด๋์น์ ์ด๋ ()
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < r and 0 <= ny < c:
if graph[nx][ny] == '.' or graph[nx][ny] == 'D' and not visited[nx][ny]:
visited[nx][ny] = True
queue.append((nx,ny,cnt+1))
return -1
cnt = bfs(sx,sy,0)
if cnt == -1: print('KAKTUS')
else: print(cnt)
'๐ Study > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Gold V] 7576 - ํ ๋งํ (0) | 2025.04.19 |
---|---|
[Silver II] 10971 - ์ธํ์ ์ํ2 (0) | 2025.04.16 |
[Gold IV] 1987 - ์ํ๋ฒณ (0) | 2025.04.14 |
[Gold III] 1726 - ๋ก๋ด (0) | 2025.04.14 |
[Gold V] 14503 - ๋ก๋ด ์ฒญ์๊ธฐ (0) | 2025.04.13 |