Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 1012번 유기농 배추 [Python] 본문
- 문제 : https://www.acmicpc.net/problem/1012
- BFS
t = int(input())
def bfs(k, l):
global answer
answer += 1
zx, zy = [0, 0, 1, -1], [1, -1, 0, 0]
Q = [[k, l]]
ddang[k][l] = 0
while Q:
x, y = Q.pop(0)
for i in range(4):
nx, ny = x + zx[i], y + zy[i]
if 0 <= nx < m and 0 <= ny < n and ddang[nx][ny] == 1:
ddang[nx][ny] = 0
Q.append([nx, ny])
for i in range(t):
answer = 0
n, m, k = map(int, input().split()) # 가로, 세로, 배추 개수
ddang = [[0] * n for _ in range(m)]
for j in range(k):
x, y = map(int, input().split())
ddang[y][x] = 1
for k in range(len(ddang)):
for l in range(len(ddang[0])):
if ddang[k][l] == 1:
bfs(k, l)
print(answer)
'백준' 카테고리의 다른 글
[백준] 2638번 치즈 [Python] (0) | 2023.08.02 |
---|---|
[백준] 7576번 토마토 [Python] (0) | 2023.07.31 |
[백준] 7562번 나이트의 이동 [Python] (0) | 2023.07.31 |
[백준] 1260번 DFS와 BFS [Python] (0) | 2023.07.31 |
[백준] 2606번 바이러스, 양방향 그래프 표현 (defaultdict) [Python] (0) | 2023.07.28 |
Comments