토마토 문제들을 모두 풀고 나니 이제는 그래프 이론에 대해서 전반적인 이해도가 생겼다. 그렇다보니 문제를 보자마자 뭔가 노하우 같은 것들도 생겼고, 아주 빠른 시간 내에 풀어 낼 수 있었다. 슬슬 BFS 몇 문제만 더 해결하고 DFS로 넘어가보면 될 것 같다.
import sys
from collections import deque
N, M = map(int, sys.stdin.readline().split())
arr = []
for _ in range(N):
tempp = []
temp = sys.stdin.readline().strip()
for i in temp:
tempp.append(i)
arr.append(tempp)
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
queue = deque()
for i in range(N):
for j in range(M):
if arr[i][j] == 'I':
queue.append([i, j])
cnt = 0
while queue:
x, y = queue.popleft()
for i in range(4):
xx, yy = dx[i] + x, dy[i] + y
if 0 <= xx < N and 0 <= yy < M:
if arr[xx][yy] == 'O':
arr[xx][yy] = 'I'
queue.append([xx, yy])
elif arr[xx][yy] == 'P':
cnt += 1
arr[xx][yy] = 'I'
queue.append([xx, yy])
if cnt == 0:
print("TT")
exit()
print(cnt)