๋ฌธ์
ํ์ด
์ ๋ ฅ๋ฐ๋ ์ฝ๋
from collections import deque
input = __import__('sys').stdin.readline
r, c = map(int, input().split())
graph = [list(input().strip()) for _ in range(r)]
waterVisited = [[False] * c for _ in range(r)]
dociVisited = [[False] * c for _ in range(r)]
water = deque()
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
๊ตด, ๊ณ ์ด๋์น, ๋ฌผ์ ์์น๋ฅผ ์ฐพ์์ฃผ์๋ค.
๋ฌผ ๊ฐ์ ๊ฒฝ์ฐ bfs๋ฅผ ์ํํด์ผ ํ๋ฏ๋ก deque์ ๋ฃ์๋ค.
# ์ด๊ธฐ๊ฐ ์ค์
for i in range(r):
for j in range(c):
if graph[i][j] == 'D':
cave = (i, j)
if graph[i][j] == 'S':
doci = (i, j, 0)
dociVisited[i][j] = True
if graph[i][j] == '*':
water.append((i, j, 0))
waterVisited[i][j] = True
๋ฌผ์ ํ๋ฆ์ ์ฒ๋ฆฌํ๋ ์ฝ๋
์๋ก ์ถ๊ฐ๋ ๋ฌผ์ ๋ค์ ์๊ฐ์ ํ๋ฅด๋ฏ๋ก ๊ธฐ์กด์ ์๋ ๋ฌผ์ ์๋งํผ๋ง ๋ฐ๋ณต๋ฌธ ์ํ
def waterFlow():
for _ in range(len(water)):
i, j, cnt = water.popleft()
move(i, j, 0, waterVisited, 'D', 'X', water, True)
๊ณ ์ด๋์น์ ์ด๋์ ์ฒ๋ฆฌํ๋ ์ฝ๋
prevCnt๋ฅผ ํตํด ์๊ฐ์ด ํ๋ ์ ๋๋ง ๋ฌผ์ด ํ๋ฅด๊ฒ ๊ตฌํ (ํ์ ํน์ง์ ์๋ฉด ์ดํดํ๊ธฐ ์ฝ๋ค)
def moveDoci():
q = deque()
q.append(doci)
cnt = 0
prevCnt = -1
while q:
i, j, cnt = q.popleft()
if prevCnt < cnt:
prevCnt = cnt
waterFlow()
if ((i, j) == cave):
print(cnt)
return
move(i, j, cnt, dociVisited, '*', 'X', q, False)
print("KAKTUS")
move ํจ์
๊ณ ์ด๋์น์ ๋ฌผ์ ์ด๋์ ๋์์ ์ฒ๋ฆฌํ๋ค.
cnt์ ๊ฒฝ์ฐ ๊ณ ์ด๋์น์๋ง ํ์ํ ๋ณ์์ด๊ณ
mark๊ฐ True์ธ ๊ฒฝ์ฐ ํด๋น ์์น๋ฅผ ๋ฌผ๋ก ์ฑ์ด๋ค.
def move(i ,j, cnt, visited, s1, s2, que, mark):
for idx in range(4):
ni, nj = i + dy[idx], j + dx[idx]
if check(ni, nj, visited, s1, s2): continue
que.append((ni, nj, cnt + 1))
visited[ni][nj] = True
if mark: graph[ni][nj] = '*'
check ํจ์
๋ฐฐ์ด์ด ๊ฐ์ ๋ฒ์ด๋์ง ์์๋์ง ๋ฑ์ ์ฒดํฌํ๋ ํจ์
def check(ni, nj, visited, s1, s2):
return ni < 0 or ni >= r or nj < 0 or nj >= c or visited[ni][nj] or graph[ni][nj] == s1 or graph[ni][nj] == s2
์ฝ๋
from collections import deque
input = __import__('sys').stdin.readline
r, c = map(int, input().split())
graph = [list(input().strip()) for _ in range(r)]
waterVisited = [[False] * c for _ in range(r)]
dociVisited = [[False] * c for _ in range(r)]
water = deque()
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
# ์ด๊ธฐ๊ฐ ์ค์
for i in range(r):
for j in range(c):
if graph[i][j] == 'D':
cave = (i, j)
if graph[i][j] == 'S':
doci = (i, j, 0)
dociVisited[i][j] = True
if graph[i][j] == '*':
water.append((i, j, 0))
waterVisited[i][j] = True
def waterFlow():
for _ in range(len(water)):
i, j, cnt = water.popleft()
move(i, j, 0, waterVisited, 'D', 'X', water, True)
def check(ni, nj, visited, s1, s2):
return ni < 0 or ni >= r or nj < 0 or nj >= c or visited[ni][nj] or graph[ni][nj] == s1 or graph[ni][nj] == s2
def moveDoci():
q = deque()
q.append(doci)
cnt = 0
prevCnt = -1
while q:
i, j, cnt = q.popleft()
if prevCnt < cnt:
prevCnt = cnt
waterFlow()
if ((i, j) == cave):
print(cnt)
return
move(i, j, cnt, dociVisited, '*', 'X', q, False)
print("KAKTUS")
def move(i ,j, cnt, visited, s1, s2, que, mark):
for idx in range(4):
ni, nj = i + dy[idx], j + dx[idx]
if check(ni, nj, visited, s1, s2): continue
que.append((ni, nj, cnt + 1))
visited[ni][nj] = True
if mark: graph[ni][nj] = '*'
moveDoci()
๋ง๋ฌด๋ฆฌ
์ ๋ฒ์ ํผ ๊ณจ๋ 1 ๋ฌธ์ ๋ณด๋ค๋ ์ฌ์์ ๊ธ๋ฐฉ ํ์๋ค ใ ใ