
캠퍼스 맵이 주어지고, O, X, I, P에 따라 행동을 구분하여, 만날 수 있는 사람의 수를 구하는 문제다.
만약 사람을 만나지 못한다면 TT를 출력하도록 한다.
- 캠퍼스 입력을 받고, 도연이가 있는
I좌표를 찾아Queue에 넣으며Visit처리- 상하좌우를 탐색할 수 있도록
drow와dcol을 선언- 도연이를 시작점으로 하여
BFS수행 시작BFS수행 중P를 만나면 결과 값 + 1O를 만나면 상하좌우 탐색만 수행,X를 만나면continue를 사용하여 다음 자리 탐색- 만약
BFS수행 종료 후 결과 값이 0인 경우TT출력
import sys
from collections import deque
input = sys.stdin.readline
a, b = map(int, input().split())
campus = []
queue = deque()
result = 0
visit = [[False] * b for _ in range(a)]
for i in range(a):
line = ''.join(input().split())
campus.append(line)
for j in range(b):
if line[j] == 'I': # 도연이 위치 찾기
queue.append([i, j])
visit[i][j] = True
drow = [-1, 1, 0, 0]
dcol = [0, 0, -1, 1]
while(queue):
row, col = queue.popleft()
if campus[row][col] == 'P':
result += 1
for _ in range(4):
arow = row + drow[_]
acol = col + dcol[_]
if 0 <= arow < a and 0 <= acol < b:
if not visit[arow][acol]:
if campus[arow][acol] == 'X':
continue
else:
queue.append([arow, acol])
visit[arow][acol] = True
if result > 0:
print(result)
else:
print('TT')