Skip to content

Commit 173b26d

Browse files
committed
백준 1012번 유기농 배추
문제 링크: https://www.acmicpc.net/problem/1012
1 parent c68e556 commit 173b26d

12 files changed

+148
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from typing import List
3+
4+
5+
def solve():
6+
T = int(sys.stdin.readline().strip())
7+
drcs = [[1, 0], [0, 1], [-1, 0], [0, -1]]
8+
9+
for _ in range(T):
10+
answer: int = 0
11+
M, N, cabbage_cnt = map(int, sys.stdin.readline().strip().split(' '))
12+
board: List[List[int]] = [[0 for _ in range(N)] for _ in range(M)]
13+
14+
for _ in range(cabbage_cnt):
15+
row, col = map(int, sys.stdin.readline().strip().split(' '))
16+
board[row][col] = 1
17+
18+
for col in range(N):
19+
for row in range(M):
20+
if board[row][col] == 1:
21+
answer += 1
22+
queue: List[List[int]] = [[row, col]]
23+
board[row][col] = 0
24+
25+
while queue:
26+
r, c = queue.pop(0)
27+
28+
for dr, dc in drcs:
29+
nr, nc = r + dr, c + dc
30+
31+
if ((0 <= nr < M) and (0 <= nc < N)) and (board[nr][nc] == 1):
32+
queue.append([nr, nc])
33+
board[nr][nc] = 0
34+
35+
print(answer)
36+
37+
38+
if __name__ == '__main__':
39+
solve()
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2
2+
10 8 17
3+
0 0
4+
1 0
5+
1 1
6+
4 2
7+
4 3
8+
4 5
9+
2 4
10+
3 4
11+
7 4
12+
8 4
13+
9 4
14+
7 5
15+
8 5
16+
9 5
17+
7 6
18+
8 6
19+
9 6
20+
10 10 1
21+
5 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1
2+
5 3 6
3+
0 2
4+
1 2
5+
2 2
6+
3 2
7+
4 2
8+
4 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1
2+
5 1 5
3+
0 0
4+
1 0
5+
2 0
6+
3 0
7+
4 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
1
2+
5 5 15
3+
0 0
4+
1 0
5+
2 0
6+
4 0
7+
0 1
8+
2 1
9+
3 1
10+
4 1
11+
0 2
12+
0 3
13+
0 4
14+
1 4
15+
2 4
16+
3 4
17+
4 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1
2+
7 3 10
3+
1 1
4+
3 1
5+
5 1
6+
0 2
7+
1 2
8+
2 2
9+
3 2
10+
4 2
11+
5 2
12+
6 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')
24+
25+
def test2_solve(self):
26+
self.test_solve('2')
27+
28+
def test3_solve(self):
29+
# 출처 https://www.acmicpc.net/board/view/139316
30+
self.test_solve('3')
31+
32+
def test4_solve(self):
33+
# 출처 https://www.acmicpc.net/board/view/126545
34+
self.test_solve('4')
35+
36+
def test5_solve(self):
37+
# 출처 https://www.acmicpc.net/board/view/126545
38+
self.test_solve('5')

0 commit comments

Comments
 (0)