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 | 31 |
Tags
- 크루스칼
- 그리디
- 그래프 이론
- 다시
- 서브쿼리
- MST
- GROUP BY
- 다이나믹 프로그래밍
- 브루트포스
- 누적합
- BFS
- DP
- 구현
- 시뮬레이션
- join
- 다익스트라
- 트리
- 재귀
- 투포인터
- 해시
- 수학
- 백트래킹
- 우선순위큐
- 그래프 탐색
- 자료구조
- 분할정복
- 에라토스테네스의 체
- 다이나믹프로그래밍
- DFS
- 플로이드-워셜
Archives
- Today
- Total
기록하고 까먹지 말기
4963 본문
날짜 : 2022. .
사용 언어 : python
문제
코드
import sys
from collections import deque
def bfs(a, b, idx):
queue.append([a, b])
earth[a][b] = idx
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]
while queue:
temp = queue.popleft()
#print(temp)
for i in range(8):
x = temp[0] + dx[i]
y = temp[1] + dy[i]
if x in range(h) and y in range(w): # 범위에 있는 경우
if earth[x][y] == 1:
earth[x][y] = idx
queue.append([x, y])
return
while True:
w, h = map(int, sys.stdin.readline().split()) # 너비, 높이
if w == h and w == 0: break
earth = []
queue = deque()
cnt = 2
for _ in range(h):
earth.append(list(map(int, sys.stdin.readline().split()))) # 입력
for i in range(h):
for j in range(w):
if earth[i][j] == 1:
bfs(i, j, cnt)
cnt += 1
"""for i in earth:
print(i)"""
res = max(map(max, earth))
if res == 0: print(res)
else: print(res - 1)
풀이
- 평소에 풀어보았던 BFS 문제
- 다만 방향이 대각선까지 포함되었기 때문에 해당 케이스들을 이동방향을 저장하는 리스트에 넣어 저장 후 연산하는 것이 차이점이었다.
알게된 점
-
참고 사이트
-
'전공 > 백준' 카테고리의 다른 글
1149 (0) | 2022.11.16 |
---|---|
12904 (0) | 2022.11.16 |
DP 응용문제(이코테) - 3 (0) | 2022.11.15 |
DP 응용문제(이코테) - 2 (0) | 2022.11.15 |
DP 응용문제(이코테) - 1 (0) | 2022.11.15 |