๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ์—ฐ๊ตฌ์†Œ - 14502

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰, ๊ตฌํ˜„

์ œ์ถœ ์ผ์ž

2023๋…„ 12์›” 21์ผ 23:49:24

๋ฌธ์ œ ์„ค๋ช…

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค.

์—ฐ๊ตฌ์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ Nร—M์ธ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ง์‚ฌ๊ฐํ˜•์€ 1ร—1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ๋นˆ ์นธ, ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ฒฝ์€ ์นธ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋“ ์ฐจ์ง€ํ•œ๋‹ค.

์ผ๋ถ€ ์นธ์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉฐ, ์ด ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ๋นˆ ์นธ์œผ๋กœ ๋ชจ๋‘ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฉฐ, ๊ผญ 3๊ฐœ๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ๊ตฌ์†Œ๊ฐ€ ์ƒ๊ธด ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

์ด๋•Œ, 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ณณ์ด๋‹ค. ์•„๋ฌด๋Ÿฐ ๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ๋ชจ๋“  ๋นˆ ์นธ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

2ํ–‰ 1์—ด, 1ํ–‰ 2์—ด, 4ํ–‰ 6์—ด์— ๋ฒฝ์„ ์„ธ์šด๋‹ค๋ฉด ์ง€๋„์˜ ๋ชจ์–‘์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง€๊ฒŒ ๋œ๋‹ค.

2 1 0 0 1 1 0
1 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 1 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ๋ชจ์Šต์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง„๋‹ค.

2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

๋ฒฝ์„ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์œ„์˜ ์ง€๋„์—์„œ ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋Š” 27์ด๋‹ค.

์—ฐ๊ตฌ์†Œ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (3 โ‰ค N, M โ‰ค 8)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„์˜ ๋ชจ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜์ด๋‹ค. 2์˜ ๊ฐœ์ˆ˜๋Š” 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ ์ด์ƒ์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋กœ์ง์˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šด๋‹ค.
  2. ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„๋‹ค.
  3. ์•ˆ์ „ํ•œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

๊ตฌํ˜„์„ ํ•˜๊ธฐ ์ „ ์„ธํŒ…๋ถ€ํ„ฐํ•œ๋‹ค.

// 0 : ๋นˆ ์นธ
// 1 : ๋ฒฝ
// 2 : ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜
void setting()
{
	cin >> n >> m;
	board = vector<vector<int>>(n, vector<int>(m));

	for (int y = 0; y < n; ++y)
	{
		for (int x = 0; x < m; ++x)
		{
			cin >> board[y][x];
			if (board[y][x] == 0) // ๋ฒฝ์„ ์„ธ์šธ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด๋ž€ ๋œป.
				wall.push_back({ y, x });
			if (board[y][x] == 2)
				virus.push_back({ y, x });
		}
	}
}

์ €๋ ‡๊ฒŒ ์„ธํŒ…์„ ํ•˜๊ณ  ์ด์ œ ๊ตฌํ˜„์„ ํ•˜๋Š”๋ฐ 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฑด combination์ด๋‹ค.
combinationํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  3๊ฐœ๋Š” 3์ค‘ for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

void solve()
{
	int res = 0;

	// ๋ฒฝ์„ ๋งŒ๋“ ๋‹ค.
	// ์กฐํ•ฉ
	int wallSize = wall.size();
	for (int i = 0; i < wallSize; ++i)
	{
		for (int j = 0; j < i; ++j)
		{
			for (int k = 0; k < j; ++k)
			{
                // 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šด๋‹ค.
				board[wall[i].y][wall[i].x] = 1;
				board[wall[j].y][wall[j].x] = 1;
				board[wall[k].y][wall[k].x] = 1;

				// ์—ฌ๊ธฐ์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฌ๊ณ  ์•ˆ์ „ํ•œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
				res = max(res, spread());

                // ๋‹ค์Œ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•˜์—ฌ ์›์ƒ ๋ณต๊ตฌ ์‹œํ‚จ๋‹ค.
				board[wall[i].y][wall[i].x] = 0;
				board[wall[j].y][wall[j].x] = 0;
				board[wall[k].y][wall[k].x] = 0;
			}
		}
	}

	cout << res << '\n';
}

spreadํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง€๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•œ๋‹ค.

int spread()
{
	int res = 0;
	memset(visited, 0, sizeof(visited));
	for (Pos pos : virus)
	{
		visited[pos.y][pos.x] = 1;
		dfs(pos.y, pos.x);
	}
	for (int y = 0; y < n; ++y)
		for (int x = 0; x < m; ++x)
			if (board[y][x] == 0 && visited[y][x] == 0)
				res++;

	return res;
}

spread๋ฅผ ํ•  ๋•Œ ๋งˆ๋‹ค visited๋Š” ์ดˆ๊ธฐํ™” ์‹œ์ผœ์•ผํ•œ๋‹ค.
์ฒซ ๋ฒˆ์งธ for๋ฌธ์„ ๋ณด๋ฉด virus ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ dfs๋ฅผ ์‹œํ‚จ๋‹ค.
์—ฌ๊ธฐ์„œ ํผ์ง€๋Š” ๋กœ์ง์ด ๋œ๋‹ค.

๊ทธ ๋กœ์ง์ด ๋๋‚˜๋ฉด ์•„๋ž˜ ์ด์ค‘for๋ฌธ์—์„œ ์•ˆ์ „ํ•œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
board[y][x] == 0 ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ง€๋‚˜๊ฐ„ ์ž๋ฆฌ๊ฐ€ ์•„๋‹ˆ ๊ณณ์ธ visited[y][x] == 0์—ญ์‹œ ํ™•์ธํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.

dfs๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
void dfs(int y, int x)
{
	for (int i = 0; i < 4; ++i)
	{
		int nextY = y + dy[i];
		int nextX = x + dx[i];
		if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m)
			continue;
		if (visited[nextY][nextX])
			continue;
		if (board[nextY][nextX]) // ๋ฒฝ์ธ ๊ฒฝ์šฐ
			continue;
		visited[nextY][nextX] = true;
		dfs(nextY, nextX);
	}
}

๋ฐฑ์ค€์—์„œ memset์„ ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ์•ˆ๋‚˜๊ฒŒ ํ•˜๋ ค๋ฉด <memory>ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ <cstring>ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.

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

#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

struct Pos
{
	int y;
	int x;
};

int visited[10][10];
int n, m;
vector<vector<int>> board;
vector<Pos> wall;
vector<Pos> virus;

// 0 : ๋นˆ ์นธ
// 1 : ๋ฒฝ
// 2 : ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜
void setting()
{
	cin >> n >> m;
	board = vector<vector<int>>(n, vector<int>(m));

	for (int y = 0; y < n; ++y)
	{
		for (int x = 0; x < m; ++x)
		{
			cin >> board[y][x];
			if (board[y][x] == 0)
				wall.push_back({ y, x });
			if (board[y][x] == 2)
				virus.push_back({ y, x });
		}
	}
}

// ํผ์ง€๋Š” ํ•จ์ˆ˜

void print_board()
{
	for (int y = 0; y < n; ++y)
	{
		for (int x = 0; x < m; ++x)
		{
			cout << board[y][x] << " ";
		}
		cout << endl;
	}

	cout << endl;
}

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
void dfs(int y, int x)
{
	for (int i = 0; i < 4; ++i)
	{
		int nextY = y + dy[i];
		int nextX = x + dx[i];
		if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m)
			continue;
		if (visited[nextY][nextX])
			continue;
		if (board[nextY][nextX])
			continue;
		visited[nextY][nextX] = true;
		dfs(nextY, nextX);
	}
}

int spread()
{
	int res = 0;
	memset(visited, 0, sizeof(visited));
	for (Pos pos : virus)
	{
		visited[pos.y][pos.x] = 1;
		dfs(pos.y, pos.x);
	}
	for (int y = 0; y < n; ++y)
		for (int x = 0; x < m; ++x)
			if (board[y][x] == 0 && visited[y][x] == 0)
				res++;

	return res;
}

// 1. ๋ฒฝ์„ 3๊ฐœ ์„ธ์šด๋‹ค
// 2. ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„๋‹ค.
// 3. ์•ˆ์ „ํ•œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
void solve()
{
	int res = 0;

	// ๋ฒฝ์„ ๋งŒ๋“ ๋‹ค.
	// ์กฐํ•ฉ
	int wallSize = wall.size();
	for (int i = 0; i < wallSize; ++i)
	{
		for (int j = 0; j < i; ++j)
		{
			for (int k = 0; k < j; ++k)
			{
				board[wall[i].y][wall[i].x] = 1;
				board[wall[j].y][wall[j].x] = 1;
				board[wall[k].y][wall[k].x] = 1;

				//print_board();
				res = max(res, spread());

				board[wall[i].y][wall[i].x] = 0;
				board[wall[j].y][wall[j].x] = 0;
				board[wall[k].y][wall[k].x] = 0;
			}
		}
	}

	cout << res << '\n';
}

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

	setting();
	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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