๐Ÿ™‡โ€โ™€๏ธ[Gold V] ์น˜ํ‚จ ๋ฐฐ๋‹ฌ - 15686

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

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

์ œ์ถœ ์ผ์ž

2023๋…„ 12์›” 25์ผ 15:10:44

๋ฌธ์ œ ์„ค๋ช…

ํฌ๊ธฐ๊ฐ€ Nร—N์ธ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๋„์‹œ๋Š” 1ร—1ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๋„์‹œ์˜ ๊ฐ ์นธ์€ ๋นˆ ์นธ, ์น˜ํ‚จ์ง‘, ์ง‘ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๋„์‹œ์˜ ์นธ์€ (r, c)์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , rํ–‰ c์—ด ๋˜๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ r๋ฒˆ์งธ ์นธ, ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ c๋ฒˆ์งธ ์นธ์„ ์˜๋ฏธํ•œ๋‹ค. r๊ณผ c๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

์ด ๋„์‹œ์— ์‚ฌ๋Š” ์‚ฌ๋žŒ๋“ค์€ ์น˜ํ‚จ์„ ๋งค์šฐ ์ข‹์•„ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์‚ฌ๋žŒ๋“ค์€ "์น˜ํ‚จ ๊ฑฐ๋ฆฌ"๋ผ๋Š” ๋ง์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์ด๋‹ค. ์ฆ‰, ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ์ง‘์„ ๊ธฐ์ค€์œผ๋กœ ์ •ํ•ด์ง€๋ฉฐ, ๊ฐ๊ฐ์˜ ์ง‘์€ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์ด๋‹ค.

์ž„์˜์˜ ๋‘ ์นธ (r1, c1)๊ณผ (r2, c2) ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” |r1-r2| + |c1-c2|๋กœ ๊ตฌํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ ์ง€๋„๋ฅผ ๊ฐ–๋Š” ๋„์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž.

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

0์€ ๋นˆ ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘์ด๋‹ค.

(2, 1)์— ์žˆ๋Š” ์ง‘๊ณผ (1, 2)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |2-1| + |1-2| = 2, (5, 5)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |2-5| + |1-5| = 7์ด๋‹ค. ๋”ฐ๋ผ์„œ, (2, 1)์— ์žˆ๋Š” ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” 2์ด๋‹ค.

(5, 4)์— ์žˆ๋Š” ์ง‘๊ณผ (1, 2)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |5-1| + |4-2| = 6, (5, 5)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |5-5| + |4-5| = 1์ด๋‹ค. ๋”ฐ๋ผ์„œ, (5, 4)์— ์žˆ๋Š” ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” 1์ด๋‹ค.

์ด ๋„์‹œ์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘์€ ๋ชจ๋‘ ๊ฐ™์€ ํ”„๋žœ์ฐจ์ด์ฆˆ์ด๋‹ค. ํ”„๋ Œ์ฐจ์ด์ฆˆ ๋ณธ์‚ฌ์—์„œ๋Š” ์ˆ˜์ต์„ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ผ๋ถ€ ์น˜ํ‚จ์ง‘์„ ํ์—…์‹œํ‚ค๋ ค๊ณ  ํ•œ๋‹ค. ์˜ค๋žœ ์—ฐ๊ตฌ ๋์— ์ด ๋„์‹œ์—์„œ ๊ฐ€์žฅ ์ˆ˜์ต์„ ๋งŽ์ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ M๊ฐœ๋ผ๋Š” ์‚ฌ์‹ค์„ ์•Œ์•„๋‚ด์—ˆ๋‹ค.

๋„์‹œ์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘ ์ค‘์—์„œ ์ตœ๋Œ€ M๊ฐœ๋ฅผ ๊ณ ๋ฅด๊ณ , ๋‚˜๋จธ์ง€ ์น˜ํ‚จ์ง‘์€ ๋ชจ๋‘ ํ์—…์‹œ์ผœ์•ผ ํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ๊ณ ๋ฅด๋ฉด, ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋ ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(2 โ‰ค N โ‰ค 50)๊ณผ M(1 โ‰ค M โ‰ค 13)์ด ์ฃผ์–ด์ง„๋‹ค.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋„์‹œ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๋„์‹œ์˜ ์ •๋ณด๋Š” 0, 1, 2๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , 0์€ ๋นˆ ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘์„ ์˜๋ฏธํ•œ๋‹ค. ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” 2N๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์ ์–ด๋„ 1๊ฐœ๋Š” ์กด์žฌํ•œ๋‹ค. ์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” M๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 13๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์„ ์ตœ๋Œ€ M๊ฐœ๋ฅผ ๊ณจ๋ž์„ ๋•Œ, ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

  1. ์—ฐ๊ตฌ์†Œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์˜€๋‹ค.

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

  1. m๊ฐœ์˜ ์น˜ํ‚จ ์ง‘ ๊ณ ๋ฅด๊ธฐ.
  2. ๋ณด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์ฐพ๊ธฐ.
  3. ์œ„์˜ ํ–‰๋™์„ ๋ชจ๋“  ๊ฒฝ์šฐ์—์„œ ์ฒ˜๋ฆฌ.
  4. ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๊ธฐ.

์ด ๋กœ์ง์˜ ๋ฉ”์ธ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.


int cal = 0;
void pick(int num, vector<int>& picked, int toPick)
{
	if (toPick == 0)
	{
		// ์น˜ํ‚จ ์ง‘ ์„ ํƒํ•˜๊ธฐ
		for (int i = 0; i < picked.size(); ++i)
		{
			board[chick[picked[i]].first][chick[picked[i]].second] = 3;
		}
		
		// ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ
		cal = 0;
		for (int i = 0; i < house.size(); ++i)
		{
			pair<int, int> h = { house[i].first, house[i].second };

			int temp = 12345678;
			for (int y = 1; y <= n; ++y)
			{
				for (int x = 1; x <= n; ++x)
				{
					if (board[y][x] == 3)
					{
						temp = min(temp,abs(h.first - y) + abs(h.second - x));
					}
				}
			}
			cal += temp;
		}

		res = min(res, cal);

		// ์น˜ํ‚จ ์ง‘ ์›์ƒ๋ณต๊ตฌ
		for (int i = 0; i < picked.size(); ++i)
		{
			board[chick[picked[i]].first][chick[picked[i]].second] = 2;
		}

		return;
	}

	int smallest = picked.empty() ? 0 : picked.back() + 1;

	for (int next = smallest; next < num; ++next)
	{
		picked.push_back(next);
		pick(num, picked, toPick - 1);
		picked.pop_back();
	}
}

์ข…๋งŒ๋ถ์—์„œ ๋ฐฐ์› ๋˜ ์žฌ๊ท€์ ์œผ๋กœ ์ˆœ์—ด๋งŒ๋“ค๊ธฐ๋ฅผ ์‘์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

์น˜ํ‚จ์ง‘์„ ์„ ํƒํ•  ๋•Œ ์ž„์‹œ๋กœ ๊ฐ’์„ 3์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์„ ํƒํ•œ ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ณ„์‚ฐ์ด ๋๋‚ฌ์œผ๋ฉด ๋‹ค์‹œ ์›์ƒ ๋ณต๊ตฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

์‚ฌ๋žŒ๋“ค์ด ๋งŽ์ด ํ’€์–ด ๋ณธ ๋ฌธ์ œ์ค‘ ํ•˜๋‚˜์ธ๋ฐ ์Šค์Šค๋กœ ํ’€๊ณ ์‹ถ์–ด์„œ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ์•ˆ๋ณด๊ณ  ํ’€์—ˆ๋‹ค..!!

๋งค์šฐ ๋ฟŒ๋“ฏ

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

// ์น˜ํ‚จ ๋ฐฐ๋‹ฌ
#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

// ๋นˆ ์นธ : 0
// ์ง‘ : 1
// ์น˜ํ‚จ์ง‘ : 2

int n, m, res = 123456789, houseCnt, chickCnt;
vector<vector<int>> board;
vector<pair<int, int>> chick;
vector<pair<int, int>> house;

void setting()
{
	cin >> n >> m;
	board = vector<vector<int>>(n + 2, vector<int>(n + 2));

	for (int y = 1; y <= n; ++y)
	{
		for (int x = 1; x <= n; ++x)
		{
			cin >> board[y][x];
			if (board[y][x] == 1)
				house.push_back({ y, x });
			if (board[y][x] == 2)
				chick.push_back({ y, x });
		}
	}

	houseCnt = house.size();
	chickCnt = chick.size();
}

int cal = 0;
void pick(int num, vector<int>& picked, int toPick)
{
	if (toPick == 0)
	{
		// ์น˜ํ‚จ ์ง‘ ์„ ํƒํ•˜๊ธฐ
		for (int i = 0; i < picked.size(); ++i)
		{
			board[chick[picked[i]].first][chick[picked[i]].second] = 3;
		}
		
		// ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ
		cal = 0;
		for (int i = 0; i < house.size(); ++i)
		{
			pair<int, int> h = { house[i].first, house[i].second };

			int temp = 12345678;
			for (int y = 1; y <= n; ++y)
			{
				for (int x = 1; x <= n; ++x)
				{
					if (board[y][x] == 3)
					{
						temp = min(temp,abs(h.first - y) + abs(h.second - x));
					}
				}
			}
			cal += temp;
		}

		res = min(res, cal);

		// ์น˜ํ‚จ ์ง‘ ์›์ƒ๋ณต๊ตฌ
		for (int i = 0; i < picked.size(); ++i)
		{
			board[chick[picked[i]].first][chick[picked[i]].second] = 2;
		}

		return;
	}

	int smallest = picked.empty() ? 0 : picked.back() + 1;

	for (int next = smallest; next < num; ++next)
	{
		picked.push_back(next);
		pick(num, picked, toPick - 1);
		picked.pop_back();
	}
}

void solve()
{
	// ์น˜ํ‚จ ์ง‘ m๊ฐœ๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‹ค ์—†์•ค๋‹ค
	// ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค
	// ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค

	vector<int> test;
	pick(chickCnt, test, m);
	
	cout << res;
}

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

	setting();
	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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