Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved 탈출 - 60ms 35164kb #260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Baekjoon/탈출/탈출_박단비.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from collections import deque

# 물, 고슴도치 -> 돌 X
# 고슴도치 -> 물 X
# 물 -> 비버 집 X

# 물이 못 가는 곳 : 돌, 비버 집
# 고슴도치가 못 가는 곳 : 돌, 물

# 물부터 큐에 입력하기
R, C = map(int, input().split())

board = []
water =[]

for i in range(R):
line = list(input())
board.append(line)
for j in range(C):
if line[j] == 'D':
e_r = i
e_c = j
if line[j] == 'S':
s_r = i
s_c = j
if line[j] == '*':
water.append((i, j))

dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]
ans = -1 # 비버 굴로 갈 수 있는지 판단하기 위해서 음수로 두기

# 물인 곳은 처음부터 다 q에 넣고, 방문 처리하고 시작
q = deque()
visited = [[False] * C for _ in range(R)]
for wr, wc in water:
q.append((wr, wc, 0))
visited[wr][wc] = True

def bfs(q):
global ans
q.append((s_r, s_c, 0))
visited[s_r][s_c] = True
while q:
r, c, time = q.popleft()
if r == e_r and c == e_c:
ans = time
for k in range(4):
nr = r + dr[k]
nc = c + dc[k]
if 0 <= nr < R and 0 <= nc < C:
if board[r][c] == '*': # 물인 경우
# 돌, 비버집이 아닌 경우. 빈 공간일 경우만 이동
if board[nr][nc] == '.' and not visited[nr][nc]:
q.append((nr, nc, time+1))
visited[nr][nc] = True
board[nr][nc] = '*'
elif board[r][c] == 'S': # 고슴도치인 경우
# 돌, 물이 아닌 경우. 빈 공간이거나 비버 집인 경우만 이동
if (board[nr][nc] == '.' or board[nr][nc] == 'D') and not visited[nr][nc]:
q.append((nr, nc, time+1))
visited[nr][nc] = True
board[nr][nc] = 'S'

bfs(q)
if ans == -1:
print("KAKTUS")
else:
print(ans)