생각할 것
1. 1개씩 받는 입력법
2. 면적 측정
<정답 코드>
#include <bits/stdc++.h>
using namespace std;
int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> v (n, (vector<int> (n, -1)));
vector<vector<int>> vis (n, (vector<int> (n, 0)));
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
char ch;
cin >> ch;
v[y][x] = ch - '0';
}
}
vector<int> areas;
int cnt = 0;
queue<pair<int, int>> q;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
if (v[y][x] == 1 && vis[y][x] == 0) {
cnt++;
q.push({y, x});
vis[y][x] = 1;
int area = 1;
while(!q.empty()) {
auto cur = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int yy = cur.first + dy[i];
int xx = cur.second + dx[i];
if (yy > -1 && yy < n && xx > -1 && xx < n && v[yy][xx] == 1 && vis[yy][xx] == 0) {
q.push({yy, xx});
vis[yy][xx] = 1;
area++;
}
}
}
areas.push_back(area);
}
}
}
sort(areas.begin(), areas.end());
cout << cnt << "\n";
for(int a : areas) {
cout << a << "\n";
}
return 0;
}'알고리즘' 카테고리의 다른 글
| [백준] C++ 2468번:안전영역! (1) | 2025.12.16 |
|---|---|
| [백준] C++ 5014번:스타트링크! (0) | 2025.12.16 |
| [백준] C++ 2583번:영역 구하기! (0) | 2025.12.15 |
| [백준] C++ 5427번: 불! (0) | 2025.12.15 |
| [백준] C++ 7562번:나이트의 이동! (0) | 2025.12.15 |