๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ์ธ๊ตฌ ์ด๋™ - 16234

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰, ๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 2์ผ 12:35:23

๋ฌธ์ œ ์„ค๋ช…

Nร—Nํฌ๊ธฐ์˜ ๋•…์ด ์žˆ๊ณ , ๋•…์€ 1ร—1๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋•…์—๋Š” ๋‚˜๋ผ๊ฐ€ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•˜๋ฉฐ, rํ–‰ c์—ด์— ์žˆ๋Š” ๋‚˜๋ผ์—๋Š” A[r][c]๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์‚ฌ์ด์—๋Š” ๊ตญ๊ฒฝ์„ ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ๋“  ๋‚˜๋ผ๋Š” 1ร—1 ํฌ๊ธฐ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ๊ตญ๊ฒฝ์„ ์€ ์ •์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์ด๋‹ค.

์˜ค๋Š˜๋ถ€ํ„ฐ ์ธ๊ตฌ ์ด๋™์ด ์‹œ์ž‘๋˜๋Š” ๋‚ ์ด๋‹ค.

์ธ๊ตฌ ์ด๋™์€ ํ•˜๋ฃจ ๋™์•ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋˜๊ณ , ๋” ์ด์ƒ ์•„๋ž˜ ๋ฐฉ๋ฒ•์— ์˜ํ•ด ์ธ๊ตฌ ์ด๋™์ด ์—†์„ ๋•Œ๊นŒ์ง€ ์ง€์†๋œ๋‹ค.

  • ๊ตญ๊ฒฝ์„ ์„ ๊ณต์œ ํ•˜๋Š” ๋‘ ๋‚˜๋ผ์˜ ์ธ๊ตฌ ์ฐจ์ด๊ฐ€ L๋ช… ์ด์ƒ, R๋ช… ์ดํ•˜๋ผ๋ฉด, ๋‘ ๋‚˜๋ผ๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๊ตญ๊ฒฝ์„ ์„ ์˜ค๋Š˜ ํ•˜๋ฃจ ๋™์•ˆ ์—ฐ๋‹ค.
  • ์œ„์˜ ์กฐ๊ฑด์— ์˜ํ•ด ์—ด์–ด์•ผํ•˜๋Š” ๊ตญ๊ฒฝ์„ ์ด ๋ชจ๋‘ ์—ด๋ ธ๋‹ค๋ฉด, ์ธ๊ตฌ ์ด๋™์„ ์‹œ์ž‘ํ•œ๋‹ค.
  • ๊ตญ๊ฒฝ์„ ์ด ์—ด๋ ค์žˆ์–ด ์ธ์ ‘ํ•œ ์นธ๋งŒ์„ ์ด์šฉํ•ด ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด, ๊ทธ ๋‚˜๋ผ๋ฅผ ์˜ค๋Š˜ ํ•˜๋ฃจ ๋™์•ˆ์€ ์—ฐํ•ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • ์—ฐํ•ฉ์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ๊ฐ ์นธ์˜ ์ธ๊ตฌ์ˆ˜๋Š” (์—ฐํ•ฉ์˜ ์ธ๊ตฌ์ˆ˜) / (์—ฐํ•ฉ์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ์นธ์˜ ๊ฐœ์ˆ˜)๊ฐ€ ๋œ๋‹ค. ํŽธ์˜์ƒ ์†Œ์ˆ˜์ ์€ ๋ฒ„๋ฆฐ๋‹ค.
  • ์—ฐํ•ฉ์„ ํ•ด์ฒดํ•˜๊ณ , ๋ชจ๋“  ๊ตญ๊ฒฝ์„ ์„ ๋‹ซ๋Š”๋‹ค.

๊ฐ ๋‚˜๋ผ์˜ ์ธ๊ตฌ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ธ๊ตฌ ์ด๋™์ด ๋ฉฐ์น  ๋™์•ˆ ๋ฐœ์ƒํ•˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

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

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฐ ๋‚˜๋ผ์˜ ์ธ๊ตฌ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. rํ–‰ c์—ด์— ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” A[r][c]์˜ ๊ฐ’์ด๋‹ค. (0 โ‰ค A[r][c] โ‰ค 100)

์ธ๊ตฌ ์ด๋™์ด ๋ฐœ์ƒํ•˜๋Š” ์ผ์ˆ˜๊ฐ€ 2,000๋ฒˆ ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ธ๊ตฌ ์ด๋™์ด ๋ฉฐ์น  ๋™์•ˆ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์ฒซ์งธ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋กœ์ง์€ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋๋‹ค.

  1. ๊ตญ๊ฒฝ์„ ์„ ์—ฐ๋‹ค.
  2. ๋ชจ๋“  ๊ตญ๊ฒฝ์„ ์„ ์—ด์—ˆ์œผ๋ฉด ์—ฐํ•ฉ์„ ๋งŒ๋“ ๋‹ค.
  3. ์—ฐํ•ฉ์„ ํ•ด์ฒดํ•˜๊ณ  ๊ตญ๊ฒฝ์„ ์„ ๋‹ซ๋Š”๋‹ค.

์—ฌ๊ธฐ์„œ ๊ตญ๊ฒฝ์„ ์„ ์—ฌ๋Š” ๋ฐฉ๋ฒ•์„ BFS๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

๋จผ์ € ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์„ ์„ธํŒ…์„ํ•œ๋‹ค.

cin >> N >> L >> R;
board = vector<vector<int>>(N + 1, vector<int>(N + 1));
discovered = vector<vector<int>>(N + 1, vector<int>(N + 1));

for (int i = 1; i <= N; ++i)
{
	for (int j = 1; j <= N; ++j)
	{
		cin >> board[i][j];
	}
}

BFS๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

void BFS(pair<int, int> here)
{
	q.push(here);
	discovered[here.first][here.second] = 1;

	// ๊ตญ๊ฒฝ์„  ์—ด๊ธฐ
	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

		if (flag != true)
			flag = false;
		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;

            // ๊ตญ๊ฒฝ์„ ์„ ์—ด ์กฐ๊ฑด์ด ๋˜๋Š”์ง€ ํ™•์ธ.    
			int diff = abs(board[here.first][here.second] - board[ny][nx]);
			if (diff < L || diff > R)
				continue;

			q.push({ ny, nx });
			v.push_back({ ny, nx });
			discovered[ny][nx] = 1;
			sum += board[ny][nx]; // ๋‚˜์ค‘์— ํ‰๊ท ๋‚ด๊ธฐ ์œ„ํ•œ ํ•ฉ
		}
	}
}

๋”์ด์ƒ ์—ฐํ•ฉ์ด ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์•ผํ•œ๋‹ค.

while (true)
{
	if (flag == false)
		break;

    // ํ”Œ๋ž˜๊ทธ ๊บผ์ฃผ๊ณ  ์—ฐํ•ฉ์ƒ๊ธฐ๋ฉด true
	flag = false;

	for (int i = 1; i <= N; ++i)
	{
		for (int j = 1; j <= N; ++j)
		{
			if (discovered[i][j] == 0)
			{
                // ๋ฒกํ„ฐ ๋น„์›Œ์ฃผ๊ณ  ํ˜„์žฌ ์œ„์น˜๋Š” ์ฑ„์›Œ์ฃผ๊ธฐ
				v.clear();
				v.push_back({ i, j });
                // ํ•ฉ๋„ ์ฒ˜์Œ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ
				sum = board[i][j];
				BFS({ i, j });
			}

            // ์—ฐํ•ฉ์ด ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ๋œป
			if (v.size() >= 2)
			{
				flag = true;
				int vSize = v.size();

                // ํ‰๊ท ๊ฐ’ ์ ์šฉํ•˜๊ธฐ.
				for (int i = 0; i < vSize; ++i)
				{
					board[v[i].first][v[i].second] = sum / vSize;
				}
			}
		}
	}

    // ๋ฌธ์ œ๊ฐ€ ๋ฉฐ์น ์ด ์ง€๋‚ฌ๋Š”์ง€ ๋ฌผ์—ˆ์œผ๋ฏ€๋กœ ํ•œ๋ฐ”ํ€ด ๋‹ค ๋Œ๊ณ  res๋ฅผ ์ฆ๊ฐ€ํ•ด์•ผ๋จ.
	if (flag)
		res++;

	// ์—ฐํ•ฉ ํ•ด์ฒดํ•˜๊ณ  ๊ตญ๊ฒฝ์„  ๋‹ซ๊ธฐ
	for (int i = 1; i <= N; ++i)
	{
		for (int j = 1; j <= N; ++j)
		{
			discovered[i][j] = 0;
		}
	}
}

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

#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;

int N, L, R, res = 0;
vector<vector<int>> board;
vector<vector<int>> discovered;
vector<pair<int, int>> v;
queue<pair<int, int>> q;

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
bool flag = true;
int sum = 0;

void BFS(pair<int, int> here)
{
	q.push(here);
	discovered[here.first][here.second] = 1;

	// ๊ตญ๊ฒฝ์„  ์—ด๊ธฐ
	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

		if (flag != true)
			flag = false;
		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;

			int diff = abs(board[here.first][here.second] - board[ny][nx]);
			if (diff < L || diff > R)
				continue;

			q.push({ ny, nx });
			v.push_back({ ny, nx });
			discovered[ny][nx] = 1;
			sum += board[ny][nx];
		}
	}
}

void solve()
{
	cin >> N >> L >> R;
	board = vector<vector<int>>(N + 1, vector<int>(N + 1));
	discovered = vector<vector<int>>(N + 1, vector<int>(N + 1));

	for (int i = 1; i <= N; ++i)
	{
		for (int j = 1; j <= N; ++j)
		{
			cin >> board[i][j];
		}
	}

	// ๋กœ์ง
	// 1. ๊ตญ๊ฒฝ์„ ์„ ์—ฐ๋‹ค.
	// 2. ๋ชจ๋“  ๊ตญ๊ฒฝ์„ ์„ ์—ด์—ˆ์œผ๋ฉด ์—ฐํ•ฉ์„ ๋งŒ๋“ ๋‹ค.
	// 3. ์—ฐํ•ฉ์„ ํ•ด์ฒดํ•˜๊ณ  ๊ตญ๊ฒฝ์„ ์„ ๋‹ซ๋Š”๋‹ค.
	// BFS?

	while (true)
	{
		if (flag == false)
			break;

		flag = false;

		for (int i = 1; i <= N; ++i)
		{
			for (int j = 1; j <= N; ++j)
			{
				if (discovered[i][j] == 0)
				{
					v.clear();
					v.push_back({ i, j });
					sum = board[i][j];
					BFS({ i, j });
				}

				if (v.size() >= 2)
				{
					flag = true;
					int vSize = v.size();

					for (int i = 0; i < vSize; ++i)
					{
						board[v[i].first][v[i].second] = sum / vSize;
					}
				}
			}
		}

		if (flag)
			res++;

		// ์—ฐํ•ฉ ํ•ด์ฒดํ•˜๊ณ  ๊ตญ๊ฒฝ์„  ๋‹ซ๊ธฐ
		for (int i = 1; i <= N; ++i)
		{
			for (int j = 1; j <= N; ++j)
			{
				discovered[i][j] = 0;
			}
		}
	}

	cout << res;
}

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

	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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