본문 바로가기

코딩 테스트

백준 - 1926 그림

#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };

struct Pos
{
	Pos() = default;
	Pos(int a, int b) : x(a), y(b)
	{

	}

	int x;
	int y;


	bool operator!= (const Pos& other)const
	{
		if (x == other.x)
		{
			if (y == other.y)
			{
				return false;
			}
		}
		return true;
	}

	bool operator== (const Pos& other)const
	{
		return !(*this != other);
	}

	bool operator<(const Pos& other)const
	{
		if (x != other.x)
		{
			return x < other.x;
		}
		return y < other.y;
	}

};


int main(void)
{
	int n, m;

	cin >> n >> m;

	vector<vector<int>> vec(n, vector<int>(m, -1));

	for (int i = 0; i < vec.size(); ++i)
	{
		for (int j = 0; j < vec[i].size(); ++j)
		{
			cin >> vec[i][j];
		}
	}
	
	int answer = 0;

	vector<vector<bool>> visited(n, vector<bool>(m, false));

	queue<Pos> q;

	map<Pos, int> Map;
	int mNum = 0;

	for (int i = 0; i < vec.size(); ++i)
	{
		for (int j = 0; j < vec[i].size(); ++j)
		{
			if (vec[i][j] == 1 && visited[i][j] == false)
			{
				q.push(Pos(i, j));
				visited[i][j] = true;

				Pos mNum(i,j);

				int fakeAnswer = 1;

				Map[mNum] = 1;

				while (!q.empty())
				{
					Pos recv = q.front();
					q.pop();

					for (int i = 0; i < 4; ++i)
					{
						int x = recv.x + dx[i];
						int y = recv.y + dy[i];

						Pos next(x, y);

						if (x < 0 || x >= n || y < 0 || y >= m)
						{
							continue;
						}

						if (visited[x][y] == true)
						{
							continue;
						}

						if (vec[x][y] == 0)
						{
							continue;
						}

						q.push(next);
						visited[x][y] = true;

						fakeAnswer++;
						Map[mNum]++;
					}
				}

				if (answer < fakeAnswer)
				{
					answer = fakeAnswer;
				}
			}
		}
	}

	cout << Map.size() << "\n" << answer;

	return 0;
}

'코딩 테스트' 카테고리의 다른 글

백준 - 14502 연구소  (0) 2024.08.27
백준 - 7576 토마토  (0) 2024.08.26
백준 : 5014 스타트링크  (0) 2024.08.26
백준 7562 나이트의 이동  (0) 2024.08.25
백준 1260 Bfs, Dfs  (0) 2024.08.25