알고리즘/BaekJoon
[c++] 2667번 : 단지번호붙이기
__jhp_+
2022. 6. 28. 14:24
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define MAP_SIZE 26
int countOfHouseGroup(int map[][MAP_SIZE], vector<int>& houseCounter, int N);
int countOfHouse(int map[][MAP_SIZE], bool houses[][MAP_SIZE], int N, int x, int y);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
vector<int> houseCnt;
int map[MAP_SIZE][MAP_SIZE] = { 0, };
char input[MAP_SIZE] = { 0, };
int N = 0, count = 0;
cin >> N;
cin.ignore();
for (int i = 1; i <= N; ++i)
{
cin.getline(input, MAP_SIZE);
for (int j = 1; j <= N; ++j)
{
map[i][j] = input[j-1] - '0';
}
}
count = countOfHouseGroup(map, houseCnt, N);
cout << count << '\n';
for (size_t i = 0; i < houseCnt.size(); ++i)
{
cout << houseCnt[i] << '\n';
}
return 0;
}
int countOfHouseGroup(int map[][MAP_SIZE], vector<int>& houseCounter, int N)
{
bool houses[MAP_SIZE][MAP_SIZE] = { false, };
int houseGroups = 0;
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= N; ++j)
{
if (houses[i][j] == false && map[i][j] == 1)
{
int houseCnt = countOfHouse(map, houses, N, j, i);
houseCounter.push_back(houseCnt);
++houseGroups;
}
}
}
sort(houseCounter.begin(), houseCounter.end());
return houseGroups;
}
int countOfHouse(int map[][MAP_SIZE], bool houses[][MAP_SIZE], int N, int x, int y)
{
int direct[2][4] = { {0, 1, 0, -1}, {-1, 0, 1, 0} };
int houseCnt = 0;
queue<pair<int, int>> q;
q.push(make_pair(y, x));
houses[y][x] = true;
++houseCnt;
while (q.empty() == false)
{
pair<int, int> pos = q.front();
int nextX = 0, nextY = 0;
q.pop();
for (int i = 0; i < 4; ++i)
{
nextY = pos.first + direct[0][i];
nextX = pos.second + direct[1][i];
if (houses[nextY][nextX] == false && map[nextY][nextX] == 1)
{
houses[nextY][nextX] = true;
q.push(make_pair(nextY, nextX));
++houseCnt;
}
}
}
return houseCnt;
}