-
알고리즘: 백준 1012번 유기농 배추 (feat. python)알고리즘/백준(BaekJoon) 2021. 1. 7. 18:47
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
import sys read = sys.stdin.readline case = int(read()) def sol(): global count count = 0 m, n, bae = list(map(int, read().split())) box = [[0 for _ in range(m)] for _ in range(n)] for _ in range(bae): x, y = list(map(int, read().split())) box[y][x] = 1 for i in range(n): for j in range(m): dfs(j, i, m, n, box) print(count) def check(x, y, m, n, box): if 0 <= x < m and 0 <= y < n and box[y][x] == 1: return True return False def dfs(start_x, start_y, m, n, box): global count if not check(start_x, start_y, m, n, box): return dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] sx = [start_x] sy = [start_y] count += 1 while sx: cx = sx.pop() cy = sy.pop() for i in range(4): if check(cx + dx[i], cy+dy[i], m, n, box): sx.append(cx + dx[i]) sy.append(cy + dy[i]) box[cy + dy[i]][cx + dx[i]] = 0 for _ in range(case): sol()
반응형'알고리즘 > 백준(BaekJoon)' 카테고리의 다른 글
알고리즘: 백준 1753번 최단경로 (feat.python) (0) 2021.01.11 알고리즘: 백준 11724번 연결 요소의 개수 (feat.python) (0) 2021.01.08 알고리즘: 백준 1260번 DFS와 BFS (feat.python) (0) 2021.01.07 알고리즘: 백준 7576번 토마토 (feat.python) (0) 2021.01.06 알고리즘: 백준 2606번 바이러스 (feat.python) (0) 2021.01.04