Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- OS
- 우아한레디스
- HTTP
- 10026번
- 열혈 TCP/IP 소켓 프로그래밍
- 이펙티브코틀린
- Window-Via-c/c++
- 김영한
- 우아한 테크 세미나
- 윤성우 저자
- 제프리리처
- TCP/IP
- BOJ
- inflearn
- 스프링 핵심 원리
- 열혈 tcp/ip 프로그래밍
- C++
- FIFO paging
- n타일링2
- Four Squares
- Operating System.
- 토마토
- 운영체제
- Operating System
- 2475번
- C#
- 스프링 입문
- Spring
- 에러핸들링
- redis
Archives
- Today
- Total
나의 브을로오그으
[c++] 2667번 : 단지번호붙이기 본문
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;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 9095번 : 1, 2, 3 더하기 (0) | 2022.08.30 |
---|---|
[c++] 5430번 : AC (0) | 2022.06.30 |
[c++] 2630번 색종이 만들기 (0) | 2022.06.28 |
[c++] 2606번 : 바이러스 (0) | 2022.06.28 |
[c++] 2579번 : 계단 오르기 (0) | 2022.06.27 |