Skip to content

Commit 3ee2859

Browse files
committed
[BOJ] 1012 유기농 배추 (S2)
1 parent fc3cbf0 commit 3ee2859

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

심수연/3주차/.cph/.260116.py_0b885d0b665401c00f87bbed06854464.prob

Lines changed: 0 additions & 1 deletion
This file was deleted.

심수연/4주차/260119.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# https://www.acmicpc.net/problem/1012
2+
3+
import sys
4+
from collections import deque
5+
input = sys.stdin.readline
6+
7+
queue = deque()
8+
9+
dx = [1, -1, 0, 0]
10+
dy = [0, 0, 1, -1]
11+
12+
# bfs(x, y)
13+
def bfs(x, y):
14+
queue.append((x, y)) # 큐에 넣기
15+
graph[y][x] = 0 # 방문 처리
16+
17+
while(queue): # while 로 반복
18+
x, y = queue.popleft() # x, y 뽑아내서
19+
# 4방향 돌리기
20+
for i in range(4):
21+
nx = x + dx[i]
22+
ny = y + dy[i]
23+
if 0 <= nx < M and 0 <= ny < N and graph[ny][nx] == 1: # 범위 + 그래프에 있는지
24+
graph[ny][nx] = 0 # 방문처리
25+
queue.append((nx, ny)) # 큐에 넣기
26+
27+
T = int(input())
28+
29+
for _ in range(T):
30+
M, N, K = map(int, input().split())
31+
32+
# 그래프 초기화
33+
graph = [[0] * M for _ in range(N)]
34+
35+
# 그래프 그리기
36+
for i in range(K):
37+
x, y = map(int, input().split())
38+
graph[y][x] = 1
39+
40+
answer = 0
41+
42+
for i in range(N):
43+
for j in range(M):
44+
if graph[i][j] == 1:
45+
bfs(j, i)
46+
answer += 1
47+
48+
print(answer)

0 commit comments

Comments
 (0)