문제 링크: https://www.acmicpc.net/problem/3055
백준 알고리즘 중급 1/3 611에서 11번째 - 3055번 탈출을 풀어보았다.
풀이: https://wookcode.tistory.com/167
C++
Python
import collections
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
graph = [list(input().strip()) for _ in range(n)]
distance = [[0] *m for _ in range(n)]
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
queue = collections.deque()
def bfs(Dx,Dy):
while queue:
x,y = queue.popleft()
if graph[Dx][Dy] == 'S':
return distance[Dx][Dy]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if (graph[nx][ny] == '.' or graph[nx][ny] == 'D') and graph[x][y] == 'S':
graph[nx][ny] = 'S'
distance[nx][ny] = distance[x][y] + 1
queue.append((nx,ny))
elif (graph[nx][ny] =='.' or graph[nx][ny] =='S') and graph[x][y] == '*':
graph[nx][ny] = '*'
queue.append((nx,ny))
return "KAKTUS"
for x in range(n):
for y in range(m):
if graph[x][y] == 'S':
queue.append((x,y))
elif graph[x][y] == 'D':
Dx,Dy = x,y
for x in range(n):
for y in range(m):
if graph[x][y] == '*':
queue.append((x,y))
print(bfs(Dx,Dy))
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
레이저 통신 풀이 (0) | 2023.03.01 |
---|---|
아기 상어 풀이 (0) | 2023.02.28 |
움직이는 미로 탈출 풀이 (0) | 2023.02.28 |
벽 부수고 이동하기 4 풀이 (0) | 2023.02.28 |
벽 부수고 이동하기 풀이 (0) | 2023.02.23 |