Skip to content

Commit

Permalink
2667 단지번호붙이기 / 깊이 우선 탐색
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored Apr 4, 2021
1 parent 0c48db7 commit 7aa303e
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 2021_03/BJ_2667.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

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

int map[25][25];
vector<int> res;
int numcount = 0;

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

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

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

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

int n;
cin >> n;
for (int i = 0; i < n; i++) {
string temp;
cin >> temp;
for (int j = 0; j < n; j++) {
map[i][j] = temp[j] - '0';
}
}

int numres = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 1) {
numres++;
solution(i, j, n);
res.push_back(numcount);
numcount = 0;
}
}
}

cout << numres + 1 << endl;
sort(res.begin(), res.end());
for (int i = 0; i < numres + 1; i++) cout << res[i] << endl;
return 0;
}

0 comments on commit 7aa303e

Please sign in to comment.