๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] 15686. ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

by Seongpyo Hong 2021. 2. 19.

 


ํ’€์ด

์–ผํ• ๋ณด๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„๋ณด์ด์ง€๋งŒ ๊ฒฐ๊ตญ ๋”ฐ์ ธ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜ ์ค‘์— M๊ฐœ์˜ ์กฐํ•ฉ์„ ๋ฝ‘๋Š” ๊ฒฝ์šฐ๋‹ค. ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ณ  ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜์˜€๋‹ค. Next Permutation ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ์กฐํ•ฉ์„ ์ƒ์„ฑํ–ˆ๊ณ  ์ด์ „ ์ตœ์†Œ๊ฐ’์„ ๋„˜์–ด์„œ๋Š” ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์„ ์ค‘์ง€ํ•˜๊ณ  ๋ฆฌํ„ด๋˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Solution15686 {
	static class Pair {
		int x = 0;
		int y = 0;

		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

	}

	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static ArrayList<Pair> home = new ArrayList<>(), shop = new ArrayList<>();
	static int[] flag;

	public static void main(String[] args) throws NumberFormatException, IOException {
		String[] line = br.readLine().split(" ");
		int N = Integer.parseInt(line[0]);
		int M = Integer.parseInt(line[1]);
		for (int i = 0; i < N; i++) {
			String[] input = br.readLine().split(" ");
			for (int j = 0; j < input.length; j++) {
				if (input[j].equals("1"))
					home.add(new Pair(i, j));
				else if (input[j].equals("2"))
					shop.add(new Pair(i, j));
			}
		}

		flag = new int[shop.size()];
		int cnt = M;
		int idx = flag.length;
		while (cnt-- > 0)
			flag[--idx] = 1;
	
		int ret = Integer.MAX_VALUE;
		do {
			ret = Math.min(ret, getDistance());
		} while (nextPermutation());

		System.out.println(ret);
	}

	private static int getDistance() {
		int totalDist = 0;
		for (int i = 0; i < home.size(); i++) {
			int min = Integer.MAX_VALUE;
			Pair curHome = home.get(i);
			for (int j = 0; j < flag.length; j++) {
				if (flag[j] == 0)
					continue;
				Pair current = shop.get(j);
				min = Math.min(min, Math.abs(current.x - curHome.x) + Math.abs(current.y - curHome.y));
			}
			totalDist += min;
		}
		return totalDist;
	}

	static boolean nextPermutation() {
		int srcIdx = flag.length;
		while (--srcIdx > 0) {
			if (flag[srcIdx] > flag[srcIdx - 1])
				break;
		}
		srcIdx--;
		if (srcIdx == -1)
			return false;

		int destIdx = flag.length;
		while (--destIdx >= 0) {
			if (flag[destIdx] < flag[srcIdx])
				continue;
			swap(srcIdx, destIdx);
		}

		destIdx = flag.length;
		while (++srcIdx <= --destIdx) {
			swap(srcIdx, destIdx);
		}

		return true;
	}

	static void swap(int src, int dest) {
		int temp = flag[src];
		flag[src] = flag[dest];
		flag[dest] = temp;
	}
}

๋Œ“๊ธ€