From 28d6277ce77f8597332ce390a75691e8d99e8ba5 Mon Sep 17 00:00:00 2001 From: DaeWook Kim Date: Thu, 11 Dec 2025 16:55:06 +0900 Subject: [PATCH] =?UTF-8?q?feat:=202025-12-11=20baekjoon=201012=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daewook/2025-12-11_baekjoon_1012.cpp | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 daewook/2025-12-11_baekjoon_1012.cpp diff --git a/daewook/2025-12-11_baekjoon_1012.cpp b/daewook/2025-12-11_baekjoon_1012.cpp new file mode 100644 index 0000000..422d662 --- /dev/null +++ b/daewook/2025-12-11_baekjoon_1012.cpp @@ -0,0 +1,76 @@ +#include +#include +using namespace std; + +int m, n; // m = 가로(X), n = 세로(Y) +int baechu[50][50]; +bool visited[50][50]; + +int dx[4] = {1, -1, 0, 0}; +int dy[4] = {0, 0, 1, -1}; + +void bfs(int x, int y) { // x = 열, y = 행 + queue> q; + q.push({x, y}); + visited[y][x] = true; + + while (!q.empty()) { + auto [cx, cy] = q.front(); + q.pop(); + + for (int i = 0; i < 4; i++) { + int nx = cx + dx[i]; + int ny = cy + dy[i]; + + if (nx < 0 || nx >= m || ny < 0 || ny >= n) + continue; + + if (baechu[ny][nx] == 1 && !visited[ny][nx]) { + visited[ny][nx] = true; + q.push({nx, ny}); + } + } + } +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + int k; + cin >> m >> n >> k; + + // 초기화 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + baechu[i][j] = 0; + visited[i][j] = false; + } + } + + // 배추 위치 입력 + for (int i = 0; i < k; i++) { + int x, y; + cin >> x >> y; + baechu[y][x] = 1; // 중요!! + } + + int worm = 0; + + // 모든 좌표 탐색 + for (int y = 0; y < n; y++) { + for (int x = 0; x < m; x++) { + if (baechu[y][x] == 1 && !visited[y][x]) { + bfs(x, y); + worm++; + } + } + } + + cout << worm << "\n"; + } +}