๐Ÿ™‡โ€โ™€๏ธ[Silver II] ๊ฝƒ๊ธธ - 14620

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

๋ฐฑํŠธ๋ž˜ํ‚น, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ œ์ถœ ์ผ์ž

2024๋…„ 11์›” 21์ผ 15:44:20

๋ฌธ์ œ ์„ค๋ช…

2017๋…„ 4์›” 5์ผ ์‹๋ชฉ์ผ์„ ๋งž์ดํ•œ ์ง„์•„๋Š” ๋‚˜๋ฌด๋ฅผ ์‹ฌ๋Š” ๋Œ€์‹  ํ•˜์ดํ…Œํฌ๊ด€ ์•ž ํ™”๋‹จ์— ๊ฝƒ์„ ์‹ฌ์–ด ๋“ฑ๊ตํ•  ๋•Œ ๋งˆ๋‹ค ๊ฝƒ๊ธธ์„ ๊ฑท๊ณ  ์‹ถ์—ˆ๋‹ค.

์ง„์•„๊ฐ€ ๊ฐ€์ง„ ๊ฝƒ์˜ ์”จ์•—์€ ๊ฝƒ์„ ์‹ฌ๊ณ ๋‚˜๋ฉด ์ •ํ™•ํžˆ 1๋…„ํ›„์— ๊ฝƒ์ด ํ”ผ๋ฏ€๋กœ ์ง„์•„๋Š” ๋‹ค์Œํ•ด ์‹๋ชฉ์ผ ๋ถ€ํ„ฐ ๊ฝƒ๊ธธ์„ ๊ฑธ์„ ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ง„์•„์—๊ฒŒ๋Š” ๊ฝƒ์˜ ์”จ์•—์ด ์„ธ๊ฐœ๋ฐ–์— ์—†์—ˆ์œผ๋ฏ€๋กœ ์„ธ ๊ฐœ์˜ ๊ฝƒ์ด ํ•˜๋‚˜๋„ ์ฃฝ์ง€ ์•Š๊ณ  1๋…„ํ›„์— ๊ฝƒ์žŽ์ด ๋งŒ๊ฐœํ•˜๊ธธ ์›ํ•œ๋‹ค.

๊ฝƒ๋ฐญ์€ N*N์˜ ๊ฒฉ์ž ๋ชจ์–‘์ด๊ณ  ์ง„์•„๋Š” ์”จ์•—์„ (1,1)~(N,N)์˜ ์ง€์  ์ค‘ ํ•œ๊ณณ์— ์‹ฌ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ฝƒ์˜ ์”จ์•—์€ ๊ทธ๋ฆผ (a)์ฒ˜๋Ÿผ ์‹ฌ์–ด์ง€๋ฉฐ 1๋…„ ํ›„ ๊ฝƒ์ด ํ”ผ๋ฉด ๊ทธ๋ฆผ (b)๋ชจ์–‘์ด ๋œ๋‹ค.

๊ฝƒ์„ ์‹ฌ์„ ๋•Œ๋Š” ์ฃผ์˜ํ•  ์ ์ด์žˆ๋‹ค. ์–ด๋–ค ์”จ์•—์ด ๊ฝƒ์ด ํ•€ ๋’ค ๋‹ค๋ฅธ ๊ฝƒ์žŽ(ํ˜น์€ ๊ฝƒ์ˆ )๊ณผ ๋‹ฟ๊ฒŒ ๋  ๊ฒฝ์šฐ ๋‘ ๊ฝƒ ๋ชจ๋‘ ์ฃฝ์–ด๋ฒ„๋ฆฐ๋‹ค. ๋˜ ํ™”๋‹จ ๋ฐ–์œผ๋กœ ๊ฝƒ์žŽ์ด ๋‚˜๊ฐ€๊ฒŒ ๋œ๋‹ค๋ฉด ๊ทธ ๊ฝƒ์€ ์ฃฝ์–ด๋ฒ„๋ฆฌ๊ณ  ๋งŒ๋‹ค.

๊ทธ๋ฆผ(c)๋Š” ์„ธ ๊ฝƒ์ด ์ •์ƒ์ ์œผ๋กœ ํ•€ ๋ชจ์–‘์ด๊ณ  ๊ทธ๋ฆผ(d)๋Š” ๋‘ ๊ฝƒ์ด ์ฃฝ์–ด๋ฒ„๋ฆฐ ๋ชจ์–‘์ด๋‹ค.

ํ•˜์ดํ…Œํฌ ์•ž ํ™”๋‹จ์˜ ๋Œ€์—ฌ ๊ฐ€๊ฒฉ์€ ๊ฒฉ์ž์˜ ํ•œ ์ ๋งˆ๋‹ค ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ง„์•„๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์„ธ ์”จ์•—์„ ๋ชจ๋‘ ๊ฝƒ์ด ํ”ผ๊ฒŒํ•˜๋ฉด์„œ ๊ฐ€์žฅ ์‹ผ ๊ฐ€๊ฒฉ์— ํ™”๋‹จ์„ ๋Œ€์—ฌํ•˜๊ณ  ์‹ถ๋‹ค.

๋‹จ ํ™”๋‹จ์„ ๋Œ€์—ฌํ•  ๋•Œ๋Š” ๊ฝƒ์žŽ์ด ํ•€ ๋ชจ์–‘์„ ๊ธฐ์ค€์œผ๋กœ ๋Œ€์—ฌ๋ฅผ ํ•ด์•ผํ•˜๋ฏ€๋กœ ๊ฝƒ ํ•˜๋‚˜๋‹น 5ํ‰์˜ ๋•…์„ ๋Œ€์—ฌํ•ด์•ผ๋งŒ ํ•œ๋‹ค.

๋ˆ์ด ๋งŽ์ง€ ์•Š์€ ์ง„์•„๋ฅผ ์œ„ํ•˜์—ฌ ์ง„์•„๊ฐ€ ๊ฝƒ์„ ์‹ฌ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ๋น„์šฉ์„ ๊ตฌํ•ด์ฃผ์ž!

์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์— ํ™”๋‹จ์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด N(6โ‰คNโ‰ค10)์ด ๋“ค์–ด์˜จ๋‹ค.

์ดํ›„ N๊ฐœ์˜ ์ค„์— N๊ฐœ์”ฉ ํ™”๋‹จ์˜ ์ง€์ ๋‹น ๊ฐ€๊ฒฉ(0โ‰คGโ‰ค200)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

๊ฝƒ์„ ์‹ฌ๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

3๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ฝƒ์žŽ์ด ๊ณ‚์น˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
๊ณ‚์น˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ฐ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ  ์ตœ์†Œ์˜ ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.

๋จผ์ € 3๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ 2์ฐจ์› ๋ฐฐ์—ด์„ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ์ƒ๊ฐํ–ˆ๋‹ค.
nn๋ฐฐ์—ด์ด๋ฏ€๋กœ ์ขŒํ‘œ๋ฅผ nn๋งŒํผ ์ˆœํšŒํ•˜๋ฉด ๋œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  3๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ์„ ํƒํ•ด์•ผํ•˜๋ฏ€๋กœ 3์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

// 3๊ฐœ ์„ ํƒ
for (int i = 0; i < n * n - 2; ++i)
{
	for (int j = i + 1; j < n * n - 1; ++j)
	{
		for (int k = j + 1; k < n * n; ++k)
		{
			// 1์ฐจ์› ์ธ๋ฑ์Šค๋ฅผ 2์ฐจ์› ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜
			int row1 = i / n;
			int col1 = i % n;
			int row2 = j / n;
			int col2 = j % n;
			int row3 = k / n;
			int col3 = k % n;
		}
	}
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด 3๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฝƒ์ด ๊ฐ€์žฅ ์ž๋ฆฌ์— ์žˆ์„ ๊ฒฝ์šฐ๋Š” ๊ฝƒ์žŽ์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด์คฌ๋‹ค.

if (row1 == 0 || col1 == 0 || row1 == n - 1 || col1 == n - 1)
	continue;
if (row2 == 0 || col2 == 0 || row2 == n - 1 || col2 == n - 1)
	continue;
if (row3 == 0 || col3 == 0 || row3 == n - 1 || col3 == n - 1)
	continue;

๊ฝƒ์žŽ์ด ๊ณ‚์น˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ check๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ set์„ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์„ ์ฒดํฌํ–ˆ๋‹ค.
๋™์‹œ์— temp์— ๋น„์šฉ์„ ๊ณ„์‚ฐ๋„ ํ•ด์คฌ๋‹ค.

// ์กฐํ•ฉ ์ €์žฅ
set<pair<int, int>> s;
int temp = 0;
s.insert({ row1, col1 }); temp += board[row1][col1];
s.insert({ row2, col2 }); temp += board[row2][col2];
s.insert({ row3, col3 }); temp += board[row3][col3];
for (int dir = 0; dir < 4; ++dir)
{
	pair<int, int> n1 = { row1 + dy[dir], col1 + dx[dir] };
	pair<int, int> n2 = { row2 + dy[dir], col2 + dx[dir] };
	pair<int, int> n3 = { row3 + dy[dir], col3 + dx[dir] };
	s.insert(n1);
	s.insert(n2);
	s.insert(n3);
	temp += board[n1.first][n1.second];
	temp += board[n2.first][n2.second];
	temp += board[n3.first][n3.second];
}
// ๊ฝƒ์žŽ์ด ๊ณ‚์น˜๋Š”๊ฐ€?
if (s.size() != 15)
	continue;

set์˜ ๊ฐœ์ˆ˜๊ฐ€ 15๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ค‘๋ณต๋œ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
์ตœ์ข…์ ์œผ๋กœ ์ตœ์†Œ์˜ ๋น„์šฉ์„ ํ™•์ธํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•œ๋‹ค.
res = min(res, temp);

3๊ฐœ์˜ ์ขŒํ‘œ๋ผ๊ณ  ๋ช…์‹œ๋œ ๋ฌธ์ œ์—ฌ์„œ 3์ค‘ for๋ฌธ๋„ ๋‚˜์˜์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ ๋งŒ์•ฝ์— ๊ฝƒ์˜ ๊ฐœ์ˆ˜๊นŒ์ง€ ๋ฌธ์ œ์—์„œ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด for๋ฌธ์ด ์•„๋‹ˆ๋ผ ์ˆœ์—ด์„ ์ง์ ‘ ๊ตฌํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์ƒ๊ธด๋‹ค.

์ˆœ์—ด์„ ๋งŒ๋“ค์–ด๋ณธ ๊ฒฝํ—˜์ด ์žˆ์–ด์„œ ๊ทธ๊ฑธ ๋‹ค์‹œ ๋ณต์Šตํ•ด๋ณด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์งฐ๋‹ค.

int n, r, ch[20];
void DFS(int s, int L)
{
	if (L == r)
	{
		for (int i = 0; i < L; ++i)
		{
			cout << ch[i] << " ";
		}
		cout << endl;
	}
	else
	{
		for (int i = s; i < n; ++i)
		{
			ch[L] = i;
			DFS(i + 1, L + 1);
		}
	}
}

2์ฐจ์› ๋ฐฐ์—ด์—์„œ 3๊ฐœ๋ฅผ ์„ ํƒํ•ด์•ผํ•˜๋Š”๋ฐ pair<int, int>๋ฅผ ๋‹ด๋Š” vector๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜์˜ ์ธ๋ฑ์Šค๋กœ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์„ ๋งŒ๋“ค๋ฉด ์ € ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ์„๊ฑฐ๊ฐ™๋‹ค.

image

๐Ÿš€์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

int n, res = INT_MAX;
vector<vector<int>> board;
pair<int, int> coordi;
int dy[] = { 0, 1, 0, -1 };
int dx[] = { 1, 0, -1, 0 };

void solve()
{
	cin >> n;
	board = vector<vector<int>>(n, vector<int>(n));
	for (int y = 0; y < n; ++y)
	{
		for (int x = 0; x < n; ++x)
		{
			cin >> board[y][x];
		}
	}

	// 3๊ฐœ ์„ ํƒ
	for (int i = 0; i < n * n - 2; ++i)
	{
		for (int j = i + 1; j < n * n - 1; ++j)
		{
			for (int k = j + 1; k < n * n; ++k)
			{
				// 1์ฐจ์› ์ธ๋ฑ์Šค๋ฅผ 2์ฐจ์› ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜
				int row1 = i / n;
				int col1 = i % n;
				int row2 = j / n;
				int col2 = j % n;
				int row3 = k / n;
				int col3 = k % n;
				if (row1 == 0 || col1 == 0 || row1 == n - 1 || col1 == n - 1)
					continue;
				if (row2 == 0 || col2 == 0 || row2 == n - 1 || col2 == n - 1)
					continue;
				if (row3 == 0 || col3 == 0 || row3 == n - 1 || col3 == n - 1)
					continue;

				// ์กฐํ•ฉ ์ €์žฅ
				set<pair<int, int>> s;
				int temp = 0;
				s.insert({ row1, col1 }); temp += board[row1][col1];
				s.insert({ row2, col2 }); temp += board[row2][col2];
				s.insert({ row3, col3 }); temp += board[row3][col3];
				for (int dir = 0; dir < 4; ++dir)
				{
					pair<int, int> n1 = { row1 + dy[dir], col1 + dx[dir] };
					pair<int, int> n2 = { row2 + dy[dir], col2 + dx[dir] };
					pair<int, int> n3 = { row3 + dy[dir], col3 + dx[dir] };
					s.insert(n1);
					s.insert(n2);
					s.insert(n3);
					temp += board[n1.first][n1.second];
					temp += board[n2.first][n2.second];
					temp += board[n3.first][n3.second];
				}
				// ๊ฝƒ์žŽ์ด ๊ณ‚์น˜๋Š”๊ฐ€?
				if (s.size() != 15)
					continue;

				// ๋น„์šฉ ์ฒดํฌ
				res = min(res, temp);
			}
		}
	}

	cout << res;
}

int main()
{
	FILE* stream;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen_s(&stream, "input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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