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
- 브루트포스
- 그리디
- DP
- GROUP BY
- 투포인터
- 수학
- 분할정복
- DFS
- BFS
- 그래프 탐색
- 플로이드-워셜
- 다이나믹프로그래밍
- 구현
- 해시
- 크루스칼
- 다시
- join
- 에라토스테네스의 체
- 그래프 이론
- 시뮬레이션
- 백트래킹
- 다익스트라
- 트리
- 누적합
- 서브쿼리
- 다이나믹 프로그래밍
- 우선순위큐
Archives
- Today
- Total
기록하고 까먹지 말기
1012 본문
날짜 : 2022. 10. 30
사용 언어 : python
문제

코드
import sys
from collections import deque
def bfs(a, b):
cabbage.append([a, b])
field[a][b] = False
# 이동
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
while cabbage:
temp = cabbage.popleft()
for i in range(4):
x = temp[0] + dx[i]
y = temp[1] + dy[i]
if 0 <= x < m and 0 <= y < n and field[x][y] == True: # 범위에 있는 경우
field[x][y] = False
cabbage.append([x, y])
continue
return
t = int(sys.stdin.readline()) # 테스트케이스
cabbage = deque() # 배추
for _ in range(t):
m, n, k = map(int, sys.stdin.readline().split()) # 가로, 세로, 배추 심긴 위치 개수
field = [[False] * n for i in range(m)]
cnt = 0
for i in range(k):
a, b = map(int, sys.stdin.readline().split()) # 심긴 위치
field[a][b] = True
#print(field) # 디버그
for i in range(m):
for j in range(n):
if field[i][j] :
cnt += 1
bfs(i, j)
print(cnt)
알게된 점
- BFS를 활용한 문제였다.
- 이전에 풀어본 아파트 단지 문제와 상당히 비슷한 유형이라 수월하게 풀 수 있었다.
참고 사이트
-