728x90
반응형

기본적인 BFS 문제이다.

from collections import deque

def bfs(y, x):
    q = deque()
    q.append((y, x))
    while q:
        y, x = q.popleft()
        for dy, dx in d:
            Y, X = y+dy, x+dx
            if (0 <= Y < H) and (0 <= X < W) and graph[Y][X] == 'T':
                q.append((Y, X))
                graph[Y][X] = 'S'
    
while 1:
    W, H = map(int, input().split())
    if W == H == 0:
        break
    graph = [list(input()) for _ in range(H)]
    d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    for i in range(H):
        for j in range(W):
            if graph[i][j] == 'S':
                bfs(i, j)
    for line in graph:
        print(''.join(line))
728x90
반응형

+ Recent posts