백준
백준 소스코드 [C++] 2667 단지번호붙이기
Hani_Levenshtein
2020. 8. 17. 07:36
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집들의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. �
www.acmicpc.net
백준 소스코드 [C++] 2667 단지번호붙이기
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int arr[26][26];
bool check[26][26] = { false };
pair<int, int>direction[4] = { {-1,0},{1,0},{0,1} ,{0,-1} };
queue<pair<int, int> > q;
int bfs() {
int num = 1;
while (q.empty() != true) {
pair<int, int> p = q.front();
q.pop();
for (int a = 0;a < 4;a++)
if (arr[p.first + direction[a].first][p.second + direction[a].second] == 1 &&
check[p.first + direction[a].first][p.second + direction[a].second] == false)
{
q.push({ p.first + direction[a].first, p.second + direction[a].second });
check[p.first + direction[a].first][p.second + direction[a].second] = true;
num++;
}
}
return num;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m=0;
string s;
cin >> n;
vector<int> v;
for (int i = 1;i <= n;i++) {
cin >> s;
for (int j = 1;j <= n;j++)
arr[i][j] = s[j-1]-'0';
}
for (int i = 1;i <= n;i++)for (int j = 1;j <= n;j++) {
if (check[i][j] == false && arr[i][j]==1) {
q.push({ i,j });
check[i][j] = true;
v.push_back(bfs());
m++;
}
}
sort(v.begin(), v.end());
cout << m << '\n';
for (int i = 0;i < v.size();i++) cout << v[i] << '\n';
return 0;
}