728x90
https://softeer.ai/practice/6282
소스코드
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
struct Pos
{
int y;
int x;
};
int _dx[4] = { 0,0,-1,1 };
int _dy[4] = { 1,-1,0,0 };
int _visit[26][26];
int _mask;
int _n;
string _list[26];
int BFS(Pos pos)
{
queue<Pos> q;
q.push(pos);
_visit[pos.y][pos.x] = _mask;
int count = 1;
while (q.empty() == false)
{
Pos now = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nextX = now.x + _dx[i];
int nextY = now.y + _dy[i];
if (nextX < 0 || nextX >= _n)
continue;
if (nextY < 0 || nextY >= _n)
continue;
if (_visit[nextY][nextX] > 0)
continue;
if (_list[nextY][nextX] == '0')
continue;
_visit[nextY][nextX] = _mask;
count++;
q.push({nextY, nextX});
}
}
return count;
}
int main(int argc, char** argv)
{
cin >> _n;
for (int i = 0; i < _n; i++) {
cin >> _list[i];
}
vector<int> result;
for (int i = 0; i < _n; i++)
{
for (int j = 0; j < _n; j++)
{
if (_list[i][j] == '0')
continue;
if (_visit[i][j] > 0)
continue;
_mask++;
result.push_back(BFS({i,j}));
}
}
sort(result.begin(), result.end());
cout << result.size() << endl;
for (auto item : result)
{
cout << item << endl;
}
return 0;
}
728x90
'코딩공부 > Softeer' 카테고리의 다른 글
[Softeer/C++] 나무 공격 (0) | 2024.11.04 |
---|---|
[Softeer/C++] [21년 재직자 대회 예선] 비밀 메뉴 (0) | 2024.03.11 |
[Softeer/C++] [21년 재직자 대회 예선] 전광판 (0) | 2024.03.09 |
[Softeer/C++] 진정한 효도 (0) | 2024.03.09 |
[Softeer/C++]Level2 8단 변속기 (0) | 2024.03.08 |