코딩공부/Softeer

[Softeer/C++] 장애물인식 프로그램

usingsystem 2024. 3. 9. 19:27
728x90

https://softeer.ai/practice/6282

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

소스코드

#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