https://www.acmicpc.net/problem/21736
많이 풀어봤던 BFS 유형이었다~
from collections import deque
N,M = map(int,input().split()) #N:행, M:열
graph = [ list(input()) for _ in range(N)]
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]
def bfs(i,j):
queue = deque()
queue.append((i,j))
result = 0
while queue: #queue가 빌 때까지 반복
x,y = queue.popleft()
for i in range(4):
cur_x,cur_y = x+dx[i], y+dy[i]
if cur_x < 0 or cur_x >= N or cur_y < 0 or cur_y >= M or graph[cur_x][cur_y] == 'X' or graph[cur_x][cur_y] == 'I':
pass
else:
queue.append((cur_x,cur_y))
if graph[cur_x][cur_y] == 'P':
result += 1
graph[cur_x][cur_y] = 'I'
return result
for i in range(N):
for j in range(M):
if graph[i][j] == 'I':
x,y = i,j
result = bfs(x,y)
if result == 0:
print('TT')
else:
print(result)
'백준' 카테고리의 다른 글
[백준] 2667번: 단지번호붙이기 (파이썬) (1) | 2024.12.23 |
---|---|
[백준] 1389번: 케빈 베이컨의 6단계 법칙 (0) | 2024.12.20 |
[백준] 1541번: 잃어버린 괄호 (1) | 2024.12.11 |
[백준] 25193번: 곰곰이의 식단 관리 (파이썬) (2) | 2024.12.04 |
[백준] 7576번: 토마토 (파이썬) (0) | 2024.12.01 |