Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 투포인터
- 재귀
- 구현
- MST
- 그리디
- join
- 크루스칼
- 서브쿼리
- 브루트포스
- 분할정복
- 자료구조
- 에라토스테네스의 체
- 수학
- 트리
- 다이나믹 프로그래밍
- 다익스트라
- 플로이드-워셜
- 백트래킹
- 우선순위큐
- 그래프 탐색
- 다시
- 그래프 이론
- GROUP BY
- DP
- 시뮬레이션
- 해시
- 다이나믹프로그래밍
- BFS
- DFS
- 누적합
Archives
- Today
- Total
기록하고 까먹지 말기
21736 본문
날짜 : 2023. 07. 25
사용 언어 : python
문제
코드
import sys
from collections import deque
def bfs(r, c):
queue = deque()
queue.append([r, c])
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
visited[r][c] = True
cnt = 0
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx in range(n) and ny in range(m) and not visited[nx][ny] and graph[nx][ny] != 'X':
visited[nx][ny] = True
queue.append([nx, ny])
if graph[nx][ny] == 'P': cnt += 1 # 사람 만남
return cnt
n, m = map(int, sys.stdin.readline().split()) # 행, 열
graph = list()
visited = [[False] * m for _ in range(n)]
res = 0
for _ in range(n):
graph.append(''.join(sys.stdin.readline().rstrip().split()))
for i in range(n):
for j in range(m):
if graph[i][j] == 'I':
res = bfs(i, j)
break
else: continue
break
if res == 0:
print("TT")
else:
print(res)
풀이
- BFS로 풀이한다.
- 카운트했을 때 수가 0인 케이스와 아닌 케이스에 대해 조건문을 달아 값을 다르게 출력
알게된 점
-
참고 사이트
-