๐Ÿ™‡โ€โ™€๏ธ[Gold V] ์ ๋ก์ƒ‰์•ฝ - 10026

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2200 KB, ์‹œ๊ฐ„: 0 ms

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

์ œ์ถœ ์ผ์ž

2024๋…„ 1์›” 5์ผ 00:08:32

๋ฌธ์ œ ์„ค๋ช…

์ ๋ก์ƒ‰์•ฝ์€ ๋นจ๊ฐ„์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋Š๋ผ์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ์€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ๊ณผ๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

ํฌ๊ธฐ๊ฐ€ Nร—N์ธ ๊ทธ๋ฆฌ๋“œ์˜ ๊ฐ ์นธ์— R(๋นจ๊ฐ•), G(์ดˆ๋ก), B(ํŒŒ๋ž‘) ์ค‘ ํ•˜๋‚˜๋ฅผ ์ƒ‰์น ํ•œ ๊ทธ๋ฆผ์ด ์žˆ๋‹ค. ๊ทธ๋ฆผ์€ ๋ช‡ ๊ฐœ์˜ ๊ตฌ์—ญ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋Š”๋ฐ, ๊ตฌ์—ญ์€ ๊ฐ™์€ ์ƒ‰์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๋˜, ๊ฐ™์€ ์ƒ‰์ƒ์ด ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋‘ ๊ธ€์ž๋Š” ๊ฐ™์€ ๊ตฌ์—ญ์— ์†ํ•œ๋‹ค. (์ƒ‰์ƒ์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋Š๋ผ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ฐ™์€ ์ƒ‰์ƒ์ด๋ผ ํ•œ๋‹ค)

์˜ˆ๋ฅผ ๋“ค์–ด, ๊ทธ๋ฆผ์ด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ดค์„ ๋•Œ ๊ตฌ์—ญ์˜ ์ˆ˜๋Š” ์ด 4๊ฐœ์ด๋‹ค. (๋นจ๊ฐ• 2, ํŒŒ๋ž‘ 1, ์ดˆ๋ก 1) ํ•˜์ง€๋งŒ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์€ ๊ตฌ์—ญ์„ 3๊ฐœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (๋นจ๊ฐ•-์ดˆ๋ก 2, ํŒŒ๋ž‘ 1)

๊ทธ๋ฆผ์ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ดค์„ ๋•Œ์™€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ดค์„ ๋•Œ ๊ตฌ์—ญ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 100)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ ์ค„์—๋Š” ๊ทธ๋ฆผ์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ดค์„ ๋•Œ์˜ ๊ตฌ์—ญ์˜ ๊ฐœ์ˆ˜์™€ ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ดค์„ ๋•Œ์˜ ๊ตฌ์—ญ์˜ ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

BFS๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜๋Š” R,G,B๋ฅผ ๊ตฌ๋ถ„ํ•œ BFS, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” R,G์„ธํŠธ B๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” BFS๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.

// R,G,B ๋‹ค ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฒ„์ „
queue<pair<int, int>> q;
int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
void BFS(int y, int x)
{
	if (discovered[y][x])
		return;
	char ch = board[y][x];
	if (ch == 'R')
		r++;
	else if (ch == 'G')
		g++;
	else
		b++;
	q.push({ y, x });
	discovered[y][x] = 1;

	while (q.empty() == false)
	{
		pair<int, int> here = q.front();
		q.pop();

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > n || nx > n)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (board[ny][nx] != ch) // ๋‹ค ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฒ„์ „์ด๋‹ˆ๊นŒ ๋‹ค๋ฅด๋ฉด ๋ฐ”๋กœ continue
				continue;

			discovered[ny][nx] = 1;
			q.push({ ny, nx });
		}
	}
}

// R,G๋ž‘ B๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฒ„์ „
void BFS2(int y, int x)
{
	if (discovered[y][x])
		return;
	char ch = board[y][x];
	if (ch == 'R')
		r++;
	else if (ch == 'B')
		b++;
	else
		g++;
	q.push({ y, x });
	discovered[y][x] = 1;

	while (q.empty() == false)
	{
		pair<int, int> here = q.front();
		q.pop();

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > n || nx > n)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (ch == 'B' && board[ny][nx] != ch) // B์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค๋ฅธ๊ฑฐ ๋งŒ๋‚˜๋ฉด continue
				continue;
			if (ch == 'R' && board[ny][nx] == 'B') // R, G๋Š” B๋งŒ๋‚˜๋ฉด continue;
				continue;
			if (ch == 'G' && board[ny][nx] == 'B')
				continue;

			discovered[ny][nx] = 1;
			q.push({ ny, nx });
		}
	}
}

์ด๋ ‡๊ฒŒ ํ•˜๊ณ  r,g,b์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์„œ ๋‹ต์„ ๊ตฌํ•œ๋‹ค.

void solve()
{
	setting();

	for (int y = 1; y <= n; ++y)
	{
		for (int x = 1; x <= n; ++x)
		{
			BFS(y, x);
		}
	}

	//cout << r << " " << g << " " << b << '\n';
	cout << r + g + b << " ";
	memset(discovered, 0, sizeof(discovered));
	r = g = b = 0;

	for (int y = 1; y <= n; ++y)
	{
		for (int x = 1; x <= n; ++x)
		{
			BFS2(y, x);
		}
	}
	//cout << r << " " << g << " " << b;
	cout << r + g + b;
}

๋ชจ๋“  ์ •์ ์„ ์ˆœํšŒํ•˜๋ฉด์„œ BFS๋ฅผ ๋Œ๋ ค์•ผํ•œ๋‹ค.

ํ•œ๋ฒˆ ๋‹ค ๋Œ๋ฆฌ๋ฉด ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค˜์•ผ ๊ฐ’์ด ์ค‘๋ณต์ด ์•ˆ๋œ๋‹ค.

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
vector<vector<char>> board;
int discovered[101][101];
int n, r = 0, g = 0, b = 0;

void setting()
{
	cin >> n;
	board = vector<vector<char>>(n + 1, vector<char>(n + 1));
	string str;
	for (int y = 1; y <= n; ++y)
	{
		cin >> str;
		for (int x = 1; x <= n; ++x)
		{
			board[y][x] = str[x - 1];
		}
	}
}

queue<pair<int, int>> q;
int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
void BFS(int y, int x)
{
	if (discovered[y][x])
		return;
	char ch = board[y][x];
	if (ch == 'R')
		r++;
	else if (ch == 'G')
		g++;
	else
		b++;
	q.push({ y, x });
	discovered[y][x] = 1;

	while (q.empty() == false)
	{
		pair<int, int> here = q.front();
		q.pop();

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > n || nx > n)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (board[ny][nx] != ch)
				continue;

			discovered[ny][nx] = 1;
			q.push({ ny, nx });
		}
	}
}

void BFS2(int y, int x)
{
	if (discovered[y][x])
		return;
	char ch = board[y][x];
	if (ch == 'R')
		r++;
	else if (ch == 'B')
		b++;
	else
		g++;
	q.push({ y, x });
	discovered[y][x] = 1;

	while (q.empty() == false)
	{
		pair<int, int> here = q.front();
		q.pop();

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > n || nx > n)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (ch == 'B' && board[ny][nx] != ch)
				continue;
			if (ch == 'R' && board[ny][nx] == 'B')
				continue;
			if (ch == 'G' && board[ny][nx] == 'B')
				continue;

			discovered[ny][nx] = 1;
			q.push({ ny, nx });
		}
	}
}

void solve()
{
	setting();

	for (int y = 1; y <= n; ++y)
	{
		for (int x = 1; x <= n; ++x)
		{
			BFS(y, x);
		}
	}

	//cout << r << " " << g << " " << b << '\n';
	cout << r + g + b << " ";
	memset(discovered, 0, sizeof(discovered));
	r = g = b = 0;

	for (int y = 1; y <= n; ++y)
	{
		for (int x = 1; x <= n; ++x)
		{
			BFS2(y, x);
		}
	}
	//cout << r << " " << g << " " << b;
	cout << r + g + b;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: