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

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

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

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 13์ผ 14:40:27

๋ฌธ์ œ ์„ค๋ช…

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํŒ์ด ์žˆ๊ณ , ๊ทธ ์œ„์— ์–‡์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ(<๊ทธ๋ฆผ 1>์—์„œ ๋„ค๋ชจ ์นธ์— X์นœ ๋ถ€๋ถ„)์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“์—ฌ ์žˆ์ง€ ์•Š์œผ๋ฉฐ ์น˜์ฆˆ์—๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ตฌ๋ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ด ์น˜์ฆˆ๋ฅผ ๊ณต๊ธฐ ์ค‘์— ๋†“์œผ๋ฉด ๋…น๊ฒŒ ๋˜๋Š”๋ฐ ๊ณต๊ธฐ์™€ ์ ‘์ด‰๋œ ์นธ์€ ํ•œ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ๋…น์•„ ์—†์–ด์ง„๋‹ค. ์น˜์ฆˆ์˜ ๊ตฌ๋ฉ ์†์—๋Š” ๊ณต๊ธฐ๊ฐ€ ์—†์ง€๋งŒ ๊ตฌ๋ฉ์„ ๋‘˜๋Ÿฌ์‹ผ ์น˜์ฆˆ๊ฐ€ ๋…น์•„์„œ ๊ตฌ๋ฉ์ด ์—ด๋ฆฌ๋ฉด ๊ตฌ๋ฉ ์†์œผ๋กœ ๊ณต๊ธฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. <๊ทธ๋ฆผ 1>์˜ ๊ฒฝ์šฐ, ์น˜์ฆˆ์˜ ๊ตฌ๋ฉ์„ ๋‘˜๋Ÿฌ์‹ผ ์น˜์ฆˆ๋Š” ๋…น์ง€ ์•Š๊ณ  โ€˜cโ€™๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„๋งŒ ํ•œ ์‹œ๊ฐ„ ํ›„์— ๋…น์•„ ์—†์–ด์ ธ์„œ <๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด ๋œ๋‹ค.

<๊ทธ๋ฆผ 1> ์›๋ž˜ ์น˜์ฆˆ ๋ชจ์–‘

๋‹ค์‹œ ํ•œ ์‹œ๊ฐ„ ํ›„์—๋Š” <๊ทธ๋ฆผ 2>์—์„œ โ€˜cโ€™๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„์ด ๋…น์•„ ์—†์–ด์ ธ์„œ <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ™์ด ๋œ๋‹ค.

<๊ทธ๋ฆผ 2> ํ•œ ์‹œ๊ฐ„ ํ›„์˜ ์น˜์ฆˆ ๋ชจ์–‘

<๊ทธ๋ฆผ 3> ๋‘ ์‹œ๊ฐ„ ํ›„์˜ ์น˜์ฆˆ ๋ชจ์–‘

<๊ทธ๋ฆผ 3>์€ ์›๋ž˜ ์น˜์ฆˆ์˜ ๋‘ ์‹œ๊ฐ„ ํ›„ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์œผ๋ฉฐ, ๋‚จ์€ ์กฐ๊ฐ๋“ค์€ ํ•œ ์‹œ๊ฐ„์ด ๋” ์ง€๋‚˜๋ฉด ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฒ˜์Œ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š” ๋ฐ๋Š” ์„ธ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ™์ด ์น˜์ฆˆ๊ฐ€ ๋…น๋Š” ๊ณผ์ •์—์„œ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค.

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

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์‚ฌ๊ฐํ˜• ๋ชจ์–‘ ํŒ์˜ ์„ธ๋กœ์™€ ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ ์–‘์˜ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ์„ธ๋กœ์™€ ๊ฐ€๋กœ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100์ด๋‹ค. ํŒ์˜ ๊ฐ ๊ฐ€๋กœ์ค„์˜ ๋ชจ์–‘์ด ์œ— ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ์ฃผ์–ด์ง„๋‹ค. ์น˜์ฆˆ๊ฐ€ ์—†๋Š” ์นธ์€ 0, ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ์นธ์€ 1๋กœ ์ฃผ์–ด์ง€๋ฉฐ ๊ฐ ์ˆซ์ž ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ ์žˆ๋‹ค.

์ถœ๋ ฅ

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

๐Ÿš€ํ’€์ด

์•„ ์ด๊ฑฐ ํ‘ธ๋Š”๋ฐ 2์ผ ๊ฑธ๋ฆผ

์น˜์ฆˆ์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ํŒ๋ณ„ํ•˜๋Š”๊ฒŒ ์•ˆ๋– ์˜ฌ๋ž๋‹ค.

BFS๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.

์ „์—ญ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋“คโ€ฆ

void solve();
void BFS(int y, int x);
void removeCheese(vector<pair<int, int>> cheese);
void printBoard();
void visitedClear();
void visited2Clear();

int n, m;
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>> visited;
vector<vector<int>> visited2;
vector<pair<int, int>> cheese;

์ค‘์š”ํ•œ ์ ์€ ์น˜์ฆˆ ๊ทธ๋ฃน์ด ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์ƒ๊ธธ์ˆ˜์žˆ๊ณ  ๊ฐ๊ฐ ํ•œ ์‹œ๊ฐ„๋งˆ๋‹ค ๊ฒ‰์„ ์ง€์›Œ์•ผํ•œ๋‹ค.

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));
	visited2 = 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];
		}
	}

    // ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋ณ€์ˆ˜๋“ค
	int totalTime = 0;
	int lastCheese = 0;

    // ํ•œ์‹œ๊ฐ„๋งŒ์— ์น˜์ฆˆ๊ฐ€ ์—†์–ด์งˆ์ˆ˜ ์žˆ์–ด์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (board[i][j] == 1)
			{
				lastCheese++;
			}
		}
	}

    // ๋‚จ์€ ์น˜์ฆˆ๊ฐ€ ์—†์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
	bool flag = true;

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

		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
                // ์น˜์ฆˆ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ
				BFS(i, j);
                // ์น˜์ฆˆ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฒ‰๋ฉด ์ง€์›Œ์ฃผ๊ธฐ
				if (cheese.size() != 0)
				{
					flag = true;
                    // ์น˜์ฆˆ ์ง€์›Œ์ฃผ๋Š” ํ•จ์ˆ˜
					removeCheese(cheese);
					cheese.clear();
				}
			}
		}

        // ๋‚จ์€ ์น˜์ฆˆ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
		int cheeseCnt = 0;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				if (board[i][j] == 1)
				{
					cheeseCnt++;
				}
			}
		}

        // ์‹œ๊ฐ„ ์ฆ๊ฐ€
		totalTime++;
		if (cheeseCnt != 0)
			lastCheese = cheeseCnt;
		else if (cheeseCnt == 0)
			flag = false;

        // ๋ฐฉ๋ฌธ ์ดˆ๊ธฐ
		visitedClear();
	}

	cout << totalTime << '\n';
	cout << lastCheese;
}

์น˜์ฆˆ ๊ทธ๋ฃน์„ ๋งŒ๋“œ๋Š” 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 });

	pair<int, int> here;

	while (q.empty() == false)
	{
		here = q.front();
		cheese.push_back(here);
		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 (board[ny][nx] == 0)
				continue;
			if (visited[ny][nx] == 1)
				continue;

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

๊ฐ€์žฅ์ž๋ฆฌ์ธ์ง€ ํ™•์ธํ•˜๋Š” BFS

bool BFS2(int y, int x)
{
	queue<pair<int, int>> q;
	q.push({ y, x });

	pair<int, int> here;
	bool flag = false;

	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)
			{
				flag = true;
				return flag;
			}
			if (board[ny][nx] == 1)
				continue;
			if (visited2[ny][nx] == 1)
				continue;

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

	return flag;
}

์ง„์งœ ์น˜์ฆˆ ๊ฒ‰๋ฉด์„ ์ง€์›Œ์ฃผ๋Š” ํ•จ์ˆ˜

void removeCheese(vector<pair<int, int>> cheese)
{
	vector<pair<int, int>> clearList;

	int up = 100, down = 0, left = 100, right = 0;
	for (int i = 0; i < cheese.size(); ++i)
	{
		up = min(up, cheese[i].first);
		down = max(down, cheese[i].first);
		left = min(left, cheese[i].second);
		right = max(right, cheese[i].second);
	}

	for (int i = 0; i < cheese.size(); ++i)
	{
		visited2Clear();

		// ์œ„์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first - 1][cheese[i].second] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first - 1, cheese[i].second);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์™ผ์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first][cheese[i].second - 1] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first, cheese[i].second - 1);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์˜ค๋ฅธ์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first][cheese[i].second + 1] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first, cheese[i].second + 1);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์•„๋ž˜์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first + 1][cheese[i].second] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first + 1, cheese[i].second);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

	}


	for (int i = 0; i < clearList.size(); ++i)
	{
		board[clearList[i].first][clearList[i].second] = 0;
	}
}

๋ฌธ์ œ ํ•ด๊ฒฐ

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

#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 removeCheese(vector<pair<int, int>> cheese);
void printBoard();
void visitedClear();
void visited2Clear();

int n, m;
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>> visited;
vector<vector<int>> visited2;
vector<pair<int, int>> cheese;

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));
	visited2 = 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];
		}
	}

	// ๊ทธ๋ฃน์„ ์ง“๋Š”๋‹ค.
	// ๊ทธ๋ฃน์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.
	// ๊ทธ๋ฃน์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ์—†์•ค๋‹ค.
	// ๋‹ค์‹œ ๊ทธ๋ฃน์„ ์ง“๋Š”๋‹ค.
	// ...

	// ๊ฐ€์žฅ์ž๋ฆฌ ๊ฐœ๋…์„ ์–ด๋–ป๊ฒŒ ์•Œ์ง€
	// ์œ„๊ฐ€ 0์ด๋ฉด์„œ ๊ทธ๋ฃน ๋‚ด์— ๋งจ์œ„ y๊ฐ’์„ ์•Œ์•ผํ•œ๋‹ค.

	// ๊ทธ๋ฃน์˜ ๊ฐœ๋…์€ bfs๋กœ ๋งŒ๋“ ๋‹ค?
	// 
	//

	//printBoard();
	//cout << '\n';

	int totalTime = 0;
	int lastCheese = 0;

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (board[i][j] == 1)
			{
				lastCheese++;
			}
		}
	}
	bool flag = true;

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

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

		//printBoard();
		//cout << endl;

		int cheeseCnt = 0;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				if (board[i][j] == 1)
				{
					cheeseCnt++;
				}
			}
		}

		totalTime++;
		if (cheeseCnt != 0)
			lastCheese = cheeseCnt;
		else if (cheeseCnt == 0)
			flag = false;

		visitedClear();
	}

	cout << totalTime << '\n';
	cout << lastCheese;
}

void BFS(int y, int x)
{
	queue<pair<int, int>> q;
	if (board[y][x] == 1 && visited[y][x] == 0)
		q.push({ y, x });

	pair<int, int> here;

	while (q.empty() == false)
	{
		here = q.front();
		cheese.push_back(here);
		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 (board[ny][nx] == 0)
				continue;
			if (visited[ny][nx] == 1)
				continue;

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

bool BFS2(int y, int x)
{
	queue<pair<int, int>> q;
	q.push({ y, x });

	pair<int, int> here;
	bool flag = false;

	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)
			{
				flag = true;
				return flag;
			}
			if (board[ny][nx] == 1)
				continue;
			if (visited2[ny][nx] == 1)
				continue;

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

	return flag;
}

// ์ด ํ•จ์ˆ˜๊ฐ€ ์ž˜๋ชป๋จ
// ์น˜์ฆˆ ๊ฒ‰๋งŒ ์ง€์›Œ์•ผํ•˜๋Š”๋ฐ ๋กœ์ง์ด ์ž˜๋ชป๋๋‹ค
// ์น˜์ฆˆ ๊ฒ‰์„ ์•Œ์ˆ˜์žˆ๋Š” ๋ฐฉ๋ฒ•์€?
void removeCheese(vector<pair<int, int>> cheese)
{
	vector<pair<int, int>> clearList;

	int up = 100, down = 0, left = 100, right = 0;
	for (int i = 0; i < cheese.size(); ++i)
	{
		up = min(up, cheese[i].first);
		down = max(down, cheese[i].first);
		left = min(left, cheese[i].second);
		right = max(right, cheese[i].second);
	}

	for (int i = 0; i < cheese.size(); ++i)
	{
		visited2Clear();

		// ์œ„์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first - 1][cheese[i].second] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first - 1, cheese[i].second);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์™ผ์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first][cheese[i].second - 1] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first, cheese[i].second - 1);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์˜ค๋ฅธ์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first][cheese[i].second + 1] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first, cheese[i].second + 1);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

		// ์•„๋ž˜์ชฝ ๊ฐ€์žฅ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ
		if (board[cheese[i].first + 1][cheese[i].second] == 0)
		{
			// bfs2 ๋Œ๋ฆฌ๊ธฐ
			bool flag = BFS2(cheese[i].first + 1, cheese[i].second);
			if (flag == true) // ๊ฐ€์žฅ์ž๋ฆฌ๋‹ค.
			{
				clearList.push_back({ cheese[i].first, cheese[i].second });
				continue;
			}

			visited2Clear();
		}

	}


	for (int i = 0; i < clearList.size(); ++i)
	{
		board[clearList[i].first][clearList[i].second] = 0;
	}
}

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

		cout << '\n';
	}
}

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

void visited2Clear()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			visited2[i][j] = 0;
		}
	}
}

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

	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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