// Copyright © 2021 ์์ฃผํ. All rights reserved.
// https://www.acmicpc.net/problem/1012
// BOJ1012 ์ ๊ธฐ๋ ๋ฐฐ์ถ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int M, N, K; //M:๊ฐ๋ก๊ธธ์ด N:์ธ๋ก๊ธธ์ด K: ๋ฐฐ์ถ ๊ฐ์
int map[51][51];
int visited[51][51];
int dy[4] = { -1,1,0,0 };
int dx[4] = { 0,0,-1,1 };
void dfs(int x, int y) {
visited[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0<=nx&&nx<N&&0<=ny&&ny<M) {
if (map[nx][ny] && !visited[nx][ny]) { //๋ฐฉ๋ฌธํ์ง ์์๊ณ , ์ง๋ ์ด๊ฐ ์์๋
visited[nx][ny] = 1;
dfs(nx, ny);
}
}
}
}
void reset() {
for (int i = 0; i < N; i++) {
for (int k = 0; k < M; k++) {
map [i][k] = 0;
visited[i][k] = 0;
}
}
}
int main() {
int T, a, b;
cin >> T;
while (T--) {
int cnt = 0;
reset();
cin >> M >> N >> K;
for (int i = 0; i < K; i++) {
cin >> a >> b;
map[b][a] = 1; //๋ฐฐ์ถ ์์น ์
๋ ฅ
}
for (int i = 0; i < N; i++) { //์ง๋ ์ด ํ์
for (int k = 0; k < M; k++) {
if (map[i][k] && !visited[i][k]) { // ์ง๋ ์ด๊ฐ ์๊ณ , ๋ฐฉ๋ฌธํ์ง ์์์๋
dfs(i, k);
cnt++;
}
}
}
cout << cnt << "\n";
}
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 10174๋ฒ - ํฐ๋ฆฐ๋๋กฌ (0) | 2021.10.16 |
---|---|
[๋ฐฑ์ค,c++] 10159๋ฒ - ์ ์ธ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1015๋ฒ - ์์ด ์ ๋ ฌ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1010๋ฒ - ๋ค๋ฆฌ ๋๊ธฐ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1009๋ฒ - ๋ถ์ฐ์ฒ๋ฆฌ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1005๋ฒ - ACM Craft (0) | 2021.10.16 |
๋๊ธ