๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

15686๋ฒˆ: ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

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

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

๋ณด์ž๋งˆ์ž ์กฐํ•ฉ(Combination)์ด ๋– ์˜ค๋ฅด๊ธด ํ–ˆ๋Š”๋ฐ, ์ด๊ฒŒ ์ •๋ง ๋‹จ์ˆœํžˆ ์กฐํ•ฉ๋งŒ์œผ๋กœ ํ’€๋ฆด๊นŒ?... ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฌ์ง„ ์•Š์„๊นŒ?, ์ค‘๋ณต ๊ณ„์‚ฐ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ? ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ, ์“ธ ๋ฐ ์—†๋Š” ๊ณ ๋ฏผ์ด์—ˆ๋‹จ ๊ฑธ ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค.

์•„๋ฌด๋ž˜๋„ ์ง‘์˜ ๊ฐœ์ˆ˜๊ฐ€ 2N๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๊ณ , N๊ณผ M์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ํฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ฒŒ๋‹ค๊ฐ€, ์ง‘๊ณผ ์น˜ํ‚จ ์ง‘๋งŒ ์‚ฌ์šฉํ•˜๋‹ˆ \( N \times N \) ํฌ๊ธฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์ ๋„ ํ•œ ๋ชซํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

์„œ๋ก ์ด ๊ธธ์—ˆ๋„ค์š”. ์•„๋ฌดํŠผ ์ œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ๋„์‹œ ๋‚ด์˜ ์ง‘ ์ขŒํ‘œ์™€ ์น˜ํ‚จ ์ง‘ ์ขŒํ‘œ๋ฅผ ์–ป์–ด์™€, ๊ฐ๊ฐ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  2. ์กฐํ•ฉ์„ ํ†ตํ•ด, ์น˜ํ‚จ ์ง‘๋“ค ์ค‘ M๊ฐœ๋ฅผ ์„ ํƒํ•œ๋‹ค.
  3. M๊ฐœ๋ฅผ ์„ ํƒํ–ˆ๋‹ค๋ฉด, ๊ฐ๊ฐ์˜ ์ง‘์— ๋Œ€ํ•ด, M๊ฐœ์˜ ์น˜ํ‚จ ์ง‘๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋ฅผ ์„ ํƒํ•˜์—ฌ ํ•ฉ์‚ฐํ•œ๋‹ค. (์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ)
    • ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ ํ•ฉ์‚ฐ ๊ฐ’์ด ์ตœ์†Œ ๋„์‹œ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ปค์ง„๋‹ค๋ฉด, ๋” ์ด์ƒ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒ
    • ์ข…๋ฃŒ ํ›„, ๋‹ค์Œ ์กฐํ•ฉ์œผ๋กœ ๋„˜์–ด๊ฐ€์„œ ๋‹ค์‹œ 3๋ฒˆ ๊ณผ์ •์„ ์ง„ํ–‰ํ•œ๋‹ค. 
  4. ๋ชจ๋“  ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค ํ•ฉ์‚ฐํ–ˆ๋‹ค๋ฉด, ์ตœ์†Œ ๋„์‹œ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์™€ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
  5. ๋ชจ๋“  ๋น„๊ต๋ฅผ ๋‹ค ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
using namespace std;
using Position = pair<int, int>;

class City
{
public:
	City(int N, int M);
	int GetMinChickenStreet();

private:
	int GetDistance(const Position& p1, const Position& p2) { return abs(p1.first - p2.first) + abs(p1.second - p2.second); }
	void GetMinChickenStreet(vector<int>& selectedChickens, int currentIdx);

private:
	const int HOUSE = 1;
	const int CHICKEN = 2;
	const int INIT_VALUE = 1000000000;

	vector<Position> _houses;
	vector<Position> _chickens;

	int _minChickenStreet = INIT_VALUE;
	int _M;
};

City::City(int N, int M)
{
	_M = M;

	for (int row = 0; row < N; row++)
	{
		for (int col = 0; col < N; col++)
		{
			int information;
			cin >> information;

			if (information == HOUSE)
				_houses.emplace_back(row, col);
			else if (information == CHICKEN)
				_chickens.emplace_back(row, col);
		}
	}
}

void City::GetMinChickenStreet(vector<int>& selectedChickens, int currentIdx)
{
	if (selectedChickens.size() == _M)
	{
		int sum = 0;
		for (int hi = 0; hi < _houses.size(); hi++)
		{
			int minDistance = INIT_VALUE;
			for (const int& ci : selectedChickens)
				minDistance = min(minDistance, GetDistance(_houses[hi], _chickens[ci]));
			
			sum += minDistance;
			if (sum > _minChickenStreet)
				return;
		}

		_minChickenStreet = min(_minChickenStreet, sum);
		return;
	}

	for (int i = currentIdx; i < _chickens.size(); i++)
	{
		selectedChickens.emplace_back(i);
		GetMinChickenStreet(selectedChickens, i + 1);
		selectedChickens.pop_back();
	}
}

int City::GetMinChickenStreet()
{
	vector<int> selectedChickens;
	GetMinChickenStreet(selectedChickens, 0);
	return _minChickenStreet;
}

int main()
{
	ios::sync_with_stdio(false);
	cout.tie(NULL); cin.tie(NULL);

	int N, M;
	cin >> N >> M;

	City city(N, M);
	cout << city.GetMinChickenStreet();
	return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•