백준 알고리즘 1012. 유기농 배추
2024. 3. 13. 13:47ㆍ코딩 테스트/백준
https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
전체 코드
import collections
import sys
def solution(mat):
def bfs(mat, x, y, width, height):
queue = collections.deque()
queue.append((x, y))
while queue:
x, y = queue.popleft()
if 0 <= x < width and 0 <= y < height and mat[y][x] == 1:
mat[y][x] = 0
queue.append((x + 1, y))
queue.append((x - 1, y))
queue.append((x, y + 1))
queue.append((x, y - 1))
width = len(mat[0])
height = len(mat)
count = 0
for y in range(height):
for x in range(width):
if mat[y][x] == 1:
bfs(mat, x, y, width, height)
count += 1
return count
t = int(input())
results = []
for _ in range(t):
m, n, k = map(int, input().split())
mat = [[0] * m for _ in range(n)]
for _ in range(k):
x, y = map(int, sys.stdin.readline().split())
mat[y][x] = 1
results.append(solution(mat))
for r in results:
print(r)
'코딩 테스트 > 백준' 카테고리의 다른 글
백준 알고리즘 7576. 토마토 (2) | 2024.03.19 |
---|---|
백준 알고리즘 13549. 숨박꼭질 3 (0) | 2024.03.18 |
백준 알고리즘 15828. Router (0) | 2024.03.13 |
백준 알고리즘 13335. 트럭 (0) | 2024.03.13 |
백준 알고리즘 15903. 카드 합체 놀이 (0) | 2024.03.12 |