Skip to content

Commit

Permalink
1012 유기농배추 / 깊이 우선 탐색
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored Apr 4, 2021
1 parent 907b2bd commit 91df3b9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions 2021_03/BJ_1012.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int location[50][50];

void solution(int x, int y, int m, int n) {
if (location[x][y] == 0) {
return;
}

else {
location[x][y] = 0;
for (int i = 0; i < 4; i++) {
int rx = x + dx[i];
int ry = y + dy[i];

if (rx >= 0 && ry >= 0 && rx < m && ry < n) {
solution(rx, ry, m, n);
}
}
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int T;
cin >> T;

for (int test_case = 1; test_case <= T; test_case++) {
int n, m, loc;
memset(location, 0, sizeof(location));
cin >> n >> m >> loc;

for (int i = 0; i < loc; i++) {
int x, y;
cin >> x >> y;
location[x][y] = 1;
}

int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (location[j][i] == 1) {
res++;
solution(j, i, n, m);
}
}
}

cout << res << endl;
}
return 0;
}

0 comments on commit 91df3b9

Please sign in to comment.