반응형
https://www.acmicpc.net/problem/3055
import collections
x_lim, y_lim = map(int, input().split())
graph = []
visit = []
start = []
target = []
for x in range(x_lim):
row = [x for x in input()]
graph.append(row)
visit.append([False]*len(row))
for y in range(len(row)):
if row[y] == 'D':
target = [x, y]
if row[y] == 'S':
start = [x, y]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
q = collections.deque()
q.append([0, start[0], start[1]]) # time, x, y
while q:
for _ in range(len(q)):
time, x, y = q.popleft()
if not visit[x][y] and graph[x][y] != '*':
visit[x][y] = True
if [x, y] == target: # finish
print(time)
quit()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < x_lim and 0 <= ny < y_lim and not visit[nx][ny]:
if graph[nx][ny] not in "X*": # possible
q.append([time + 1, nx, ny])
# water spread
for x in range(x_lim):
for y in range(y_lim):
if graph[x][y] == "*":
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < x_lim and 0 <= ny < y_lim:
if graph[nx][ny] == ".":
graph[nx][ny] = "#"
for x in range(x_lim):
for y in range(y_lim):
if graph[x][y] == "#":
graph[x][y] = "*"
print("KAKTUS")
댓글