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 |
Tags
- Window-Via-c/c++
- 스프링 핵심 원리
- Spring
- FIFO paging
- OS
- 열혈 tcp/ip 프로그래밍
- 이펙티브코틀린
- 토마토
- 김영한
- 에러핸들링
- 우아한레디스
- 2475번
- HTTP
- 우아한 테크 세미나
- TCP/IP
- redis
- Four Squares
- C++
- 열혈 TCP/IP 소켓 프로그래밍
- 윤성우 저자
- BOJ
- Operating System.
- Operating System
- n타일링2
- 10026번
- C#
- inflearn
- 제프리리처
- 운영체제
- 스프링 입문
Archives
- Today
- Total
나의 브을로오그으
[c++] 2667번 : 단지번호붙이기 본문
https://www.acmicpc.net/problem/2667
#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 |