๐Ÿ™‡โ€โ™€๏ธ[Gold III] ์น˜์ฆˆ - 2638

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

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

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 15์ผ 16:48:52

๋ฌธ์ œ ์„ค๋ช…

Nร—M์˜ ๋ชจ๋ˆˆ์ข…์ด ์œ„์— ์•„์ฃผ ์–‡์€ ์น˜์ฆˆ๊ฐ€ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ๋‹จ, N ์€ ์„ธ๋กœ ๊ฒฉ์ž์˜ ์ˆ˜์ด๊ณ , M ์€ ๊ฐ€๋กœ ๊ฒฉ์ž์˜ ์ˆ˜์ด๋‹ค. ์ด ์น˜์ฆˆ๋Š” ๋ƒ‰๋™ ๋ณด๊ด€์„ ํ•ด์•ผ๋งŒ ํ•˜๋Š”๋ฐ ์‹ค๋‚ด์˜จ๋„์— ๋‚ด์–ด๋†“์œผ๋ฉด ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•˜์—ฌ ์ฒœ์ฒœํžˆ ๋…น๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌํ•œ ๋ชจ๋ˆˆ์ข…์ด ๋ชจ์–‘์˜ ์น˜์ฆˆ์—์„œ ๊ฐ ์น˜์ฆˆ ๊ฒฉ์ž(์ž‘ ์€ ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘)์˜ 4๋ณ€ ์ค‘์—์„œ ์ ์–ด๋„ 2๋ณ€ ์ด์ƒ์ด ์‹ค๋‚ด์˜จ๋„์˜ ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•œ ๊ฒƒ์€ ์ •ํ™•ํžˆ ํ•œ์‹œ๊ฐ„๋งŒ์— ๋…น์•„ ์—†์–ด์ ธ ๋ฒ„๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜ <๊ทธ๋ฆผ 1> ๋ชจ์–‘๊ณผ ๊ฐ™์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๋ผ๋ฉด C๋กœ ํ‘œ์‹œ๋œ ๋ชจ๋“  ์น˜์ฆˆ ๊ฒฉ์ž๋Š” ํ•œ ์‹œ๊ฐ„ ํ›„์— ์‚ฌ๋ผ์ง„๋‹ค.

<๊ทธ๋ฆผ 1>

<๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด ์น˜์ฆˆ ๋‚ด๋ถ€์— ์žˆ๋Š” ๊ณต๊ฐ„์€ ์น˜์ฆˆ ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€ ๋กœ ์ด ๊ณต๊ฐ„์— ์ ‘์ด‰ํ•œ ์น˜์ฆˆ ๊ฒฉ์ž๋Š” ๋…น์ง€ ์•Š๊ณ  C๋กœ ํ‘œ์‹œ๋œ ์น˜์ฆˆ ๊ฒฉ์ž๋งŒ ์‚ฌ๋ผ์ง„๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•œ ์‹œ๊ฐ„ ํ›„, ์ด ๊ณต๊ฐ„์œผ๋กœ ์™ธ๋ถ€๊ณต๊ธฐ๊ฐ€ ์œ ์ž…๋˜๋ฉด <๊ทธ๋ฆผ 3>์—์„œ์™€ ๊ฐ™์ด C๋กœ ํ‘œ์‹œ๋œ ์น˜์ฆˆ ๊ฒฉ์ž๋“ค์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.

<๊ทธ๋ฆผ 2>

<๊ทธ๋ฆผ 3>

๋ชจ๋ˆˆ์ข…์ด์˜ ๋งจ ๊ฐ€์žฅ์ž๋ฆฌ์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“์ด์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ •ํ™•ํ•œ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๋ชจ๋ˆˆ์ข…์ด์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ N, M (5 โ‰ค N, M โ‰ค 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋ชจ๋ˆˆ์ข…์ด ์œ„์˜ ๊ฒฉ์ž์— ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์€ 1๋กœ ํ‘œ์‹œ๋˜๊ณ , ์น˜์ฆˆ๊ฐ€ ์—†๋Š” ๋ถ€๋ถ„์€ 0์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ๋˜ํ•œ, ๊ฐ 0๊ณผ 1์€ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋‹ค.

์ถœ๋ ฅ

์ถœ๋ ฅ์œผ๋กœ๋Š” ์ฃผ์–ด์ง„ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ •ํ™•ํ•œ ์‹œ๊ฐ„์„ ์ •์ˆ˜๋กœ ์ฒซ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์™ธ๋ถ€ ๊ณต๊ธฐ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๋กœ BFS๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

// ๋‚ด๋ถ€ ๊ณต๊ธฐ์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ
void BFS2(int y, int x)
{
	queue<pair<int, int>> q;
	q.push({ y, x });
	isInAir[y][x] = 1;
	pair<int, int> here;

	while (q.empty() == false)
	{
		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 > m)
				continue;
			if (isInAir[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 1)
				continue;

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

์น˜์ฆˆ ๊ทธ๋ฃน์„ ์•Œ์•„์•ผํ•˜๋ฏ€๋กœ ์น˜์ฆˆ๋ฅผ ์•Œ BFS๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ๊ตณ์ด ์•Œ์ง€ ์•Š์•„๋„ ๋ ๊ฒƒ๊ฐ™๋‹ค.

// ์น˜์ฆˆ ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ
void BFS(int y, int x)
{
	queue<pair<int, int>> q;
	if (board[y][x] == 1 && visited[y][x] == 0)
	{
		q.push({ y, x });
		cheese.push_back({ y, x });
		visited[y][x] = 1;
	}
	pair<int, int> here;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

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

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				continue;
			if (visited[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 0)
				continue;

			q.push({ ny, nx });
			cheese.push_back({ ny, nx });
			visited[ny][nx] = 1;
		}
	}
}

๋‚˜๋Š” ์ด๋ ‡๊ฒŒ BFS๋ฅผ ๋งŒ๋“ค์–ด์„œ ์น˜์ฆˆ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌ๋ฅผ ํ–ˆ๋Š”๋ฐ board์˜ ๊ฐ’์ด 1์ผ ๋•Œ ๋งˆ๋‹ค ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ์–ผ๋งˆํผ ๋‹ฟ์•„์žˆ๋Š”์ง€ ํ™•์ธํ•ด์„œ ํ’€์–ด๋„ ๋ ๊ฑฐ๊ฐ™๋‹ค.

์น˜์ฆˆ๋ฅผ ๋…น์ด๋Š” ํ•จ์ˆ˜๋Š” ์™ธ๋ถ€๊ณต๊ธฐ๋ฅผ ์ด๋ฏธ ํŒŒ์•…ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์น˜์ฆˆ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ์–ผ๋งŒํผ ๋‹ฟ์•„์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

void meltCheese()
{
	for (int i = 0; i < cheese.size(); ++i)
	{
		int cnt = 0;

		pair<int, int> ch = cheese[i];
		for (int j = 0; j < 4; ++j)
		{
			int ny = ch.first + dy[j];
			int nx = ch.second + dx[j];

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				continue;
			if (isInAir[ny][nx] == 1 && board[ny][nx] == 0)
				cnt++;

			if (cnt >= 2)
			{
				board[ch.first][ch.second] = 0;
				totalCheese--;
				break;
			}
		}
	}
}

์ตœ์ข…์ ์œผ๋กœ solve๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

void solve()
{
	// ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
	cin >> n >> m;
	board = vector<vector<int>>(n + 1, vector<int>(m + 1));
	visited = vector<vector<int>>(n + 1, vector<int>(m + 1));
	isInAir = vector<vector<int>>(n + 1, vector<int>(m + 1));

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] == 1)
				totalCheese++;
		}
	}

	//printBoard();
	BFS2(1, 1); // ์™ธ๋ถ€๊ณต๊ธฐ ํŒ๋‹จ

	while (true)
	{
		if (totalCheese == 0)
		{
			cout << res;
			break;
		}

		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				BFS(i, j);
				if (cheese.size() != 0)
				{
					meltCheese();
					cheese.clear();
				}
			}
		}

		Clear();
		BFS2(1, 1);
		res++;
		//printBoard();
	}
}

image

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

#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

void solve();
void BFS(int y, int x);
void BFS2(int y, int x);
void printBoard();
void Clear();

int n, m, res = 0, totalCheese = 0;
int dy[] = { -1, 0, 1, 0, -1, 1, 1, -1 };
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
vector<vector<int>> board;
vector<vector<int>> isInAir; // 1์ด๋ฉด ์™ธ๋ถ€ ๊ณต๊ธฐ 0์ด๋ฉด ๋‚ด๋ถ€ ๊ณต๊ธฐ
vector<vector<int>> visited;
vector<pair<int, int>> cheese;

// ์™ธ๋ถ€๊ณต๊ธฐ์ธ์ง€ ๋‚ด๋ถ€๊ณต๊ธฐ์ธ์ง€
// ์ ‘์ด‰๋ฉด์„ ํ™•์ธํ•˜๋Š”๊ฒƒ.
// ์น˜์ฆˆ ๊ทธ๋ฃน๋งŒ๋“ค๊ธฐ

// ์น˜์ฆˆ ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ
void BFS(int y, int x)
{
	queue<pair<int, int>> q;
	if (board[y][x] == 1 && visited[y][x] == 0)
	{
		q.push({ y, x });
		cheese.push_back({ y, x });
		visited[y][x] = 1;
	}
	pair<int, int> here;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

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

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				continue;
			if (visited[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 0)
				continue;

			q.push({ ny, nx });
			cheese.push_back({ ny, nx });
			visited[ny][nx] = 1;
		}
	}
}

// ๋‚ด๋ถ€ ๊ณต๊ธฐ์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ
void BFS2(int y, int x)
{
	queue<pair<int, int>> q;
	q.push({ y, x });
	isInAir[y][x] = 1;
	pair<int, int> here;

	while (q.empty() == false)
	{
		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 > m)
				continue;
			if (isInAir[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 1)
				continue;

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

void Clear()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			isInAir[i][j] = 0;
			visited[i][j] = 0;
		}
	}
}

void meltCheese()
{
	for (int i = 0; i < cheese.size(); ++i)
	{
		int cnt = 0;

		pair<int, int> ch = cheese[i];
		for (int j = 0; j < 4; ++j)
		{
			int ny = ch.first + dy[j];
			int nx = ch.second + dx[j];

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				continue;
			if (isInAir[ny][nx] == 1 && board[ny][nx] == 0)
				cnt++;

			if (cnt >= 2)
			{
				board[ch.first][ch.second] = 0;
				totalCheese--;
				break;
			}
		}
	}
}

void printBoard()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}

	cout << endl;
}

void solve()
{
	// ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
	cin >> n >> m;
	board = vector<vector<int>>(n + 1, vector<int>(m + 1));
	visited = vector<vector<int>>(n + 1, vector<int>(m + 1));
	isInAir = vector<vector<int>>(n + 1, vector<int>(m + 1));

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] == 1)
				totalCheese++;
		}
	}

	//printBoard();
	BFS2(1, 1); // ์™ธ๋ถ€๊ณต๊ธฐ ํŒ๋‹จ

	while (true)
	{
		if (totalCheese == 0)
		{
			cout << res;
			break;
		}

		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				BFS(i, j);
				if (cheese.size() != 0)
				{
					meltCheese();
					cheese.clear();
				}
			}
		}

		Clear();
		BFS2(1, 1);
		res++;
		//printBoard();
	}
}

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

	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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