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 |
Tags
- 백준 dfs
- 백준 패션왕 신해빈
- c++ split
- 백준 2309번 일곱 난쟁이
- 월곡동 학원추천
- 고정 소수점
- 상월곡역 학원
- C++ 9996
- 서울사대부고 학원
- 상월곡동 학원
- 백준 토마토
- OS
- 백준 K보다 큰 구간
- C# 병합정렬
- c++ 조합
- 백준 14246번
- 월곡역 학원
- 백준 14246번 K보다 큰 구간
- C++ 문자열
- 백준 1049번
- 월곡중학교 학원추천
- DFS
- 운영체제
- 백준 1049번 기타줄
- 월곡중 학원
- 성북구 학원
- 백준 10709
- 백준 한국이 그리울 땐 서버에 접속하지
- 관리형 학원
- 백준 9375번 패션왕 신해빈
Archives
- Today
- Total
키베이루's diary
[C++] 백준 2583번 영역 구하기 본문
1) 문제설명
2) 아이디어
처음 M,N 행렬을 0으로 초기화 한 뒤 입력받은 위치(X1 X2 ~ Y1 Y2)는 1로 변경해 주었다.
이후 BFS를 실행하여 영역크기는 각각 vector에 저장하여 오름차순으로 출력하였고 영역의 개수는 따로 카운트 하였다.
3) 코드
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<deque>
#include<string.h>
#include<math.h>
#include<cmath>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
queue<pair<int, int>> q;
int arr[101][101];
int range[101][101];
int result = 0;
int cnt = 0;
int n, m, k;
int x1, y11, x2, y2;
void bfs(int y, int x) {
result = 1;
range[y][x] = 1;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
while (q.size()) {
y = q.front().first;
x = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx <0 || ny <0 || nx >= m || ny >= n || arr[ny][nx] == 1) {
continue;
}
if (range[ny][nx]) {
continue;
}
range[ny][nx] = 1;
result += 1;
q.push({ ny,nx });
}
}
}
int main() {
vector<int> v;
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
cin >> x1 >> y11 >> x2 >> y2;
for (int x = x1; x < x2; x++) { //
for (int y = y11; y < y2; y++) {
arr[y][x] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
result = 0;
if (range[i][j] == 0 && arr[i][j] == 0) {
cnt++;
q.push({ i,j });
bfs(i, j);
}
if (result != 0) {
v.push_back(result);
}
}
}
sort(v.begin(), v.end());
cout << cnt << '\n';
for (int i = 0; i < v.size(); i++) {
cout << v[i] << ' ';
}
}
'알고리즘 > BFS, DFS' 카테고리의 다른 글
[C++] 백준 1707번 이분 그래프 (0) | 2022.07.16 |
---|---|
[C++] 백준 4963번 섬의 개수 (0) | 2022.07.13 |
[C++] 백준 15650번 N과 M (2) (0) | 2022.06.20 |
[C++] 백준 15649번 N과 M (1) (0) | 2022.06.19 |
[C++] 백준 2468번 안전 영역 (0) | 2022.06.17 |
Comments