백준 - 21736

KangMyungJoe·2023년 8월 30일
0

algorithm

목록 보기
40/55

문제 설명

캠퍼스 맵이 주어지고, O, X, I, P에 따라 행동을 구분하여, 만날 수 있는 사람의 수를 구하는 문제다.

만약 사람을 만나지 못한다면 TT를 출력하도록 한다.


접근 방법

  1. 캠퍼스 입력을 받고, 도연이가 있는 I 좌표를 찾아 Queue에 넣으며 Visit 처리
  2. 상하좌우를 탐색할 수 있도록 drowdcol을 선언
  3. 도연이를 시작점으로 하여 BFS 수행 시작
  4. BFS 수행 중 P를 만나면 결과 값 + 1
  5. O를 만나면 상하좌우 탐색만 수행, X를 만나면 continue를 사용하여 다음 자리 탐색
  6. 만약 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')
profile
소통을 잘하는 개발자가 되고 싶습니다.

0개의 댓글