문제
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
from collections import deque
import sys
t = int(input())
dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]
def bfs(x, y, f):
queue = deque()
queue.append((x, y))
f[x][y] = 0
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dr[i]
ny = y + dc[i]
if 0 <= nx < n and 0 <= ny < m and field[nx][ny] == 1:
queue.append((nx, ny))
field[nx][ny] = cnt
for i in range(t):
m, n, k = map(int, sys.stdin.readline().split())
field = [[0] * m for _ in range(n)]
cnt = 2
for j in range(k):
a, b = map(int, sys.stdin.readline().split())
field[b][a] = 1
for a in range(n):
for b in range(m):
if field[a][b] == 1:
bfs(a, b, field)
cnt += 1
print(cnt-2)
bfs 문제
지렁이 수+1을 cnt에 집어넣고 시작했다
좌표 x, y가 너무 헷갈려서 고생했던 기억... 익숙해지는 중이다!
'공부하자! > 알고리즘' 카테고리의 다른 글
백준 4963 섬의 개수 파이썬 - bfs (0) | 2021.09.09 |
---|---|
백준 7576번 토마토 파이썬 - bfs (0) | 2021.09.09 |
백준 2667번 단지번호붙이기 파이썬 (0) | 2021.09.07 |
프로그래머스 | 짝지어 제거하기 파이썬 (0) | 2021.08.22 |
프로그래머스 | 오픈채팅방 파이썬 (0) | 2021.08.19 |