Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 15686. 치킨 배달 본문

acmicpc

[BAEKJOON] 15686. 치킨 배달

코드와이 2021. 5. 10. 20:09

 

문제링크

www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸

www.acmicpc.net

 

package acmicpc.Gold5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class 치킨배달 {

	static int n, m, map[][], dis[][], ans, numbers[];
	static List<int[]> list1, list2;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		map = new int[n][n];
		list1 = new ArrayList<>();		// 집 위치
		list2 = new ArrayList<>();		// 치킨집 위치
		for(int i = 0 ; i < n ; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0 ; j < n ; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
				if(map[i][j] == 1) list1.add(new int[] {i,j});
				else if(map[i][j] == 2) list2.add(new int[] {i,j});
			}
		}

		dis = new int[list1.size()][list2.size()];
		for(int i = 0 ; i < list1.size() ; i++) {
			int r = list1.get(i)[0];
			int c = list1.get(i)[1];
			for(int j = 0 ; j < list2.size() ; j++) {
				int r2 = list2.get(j)[0];
				int c2 = list2.get(j)[1];
				dis[i][j] = Math.abs(r-r2) + Math.abs(c-c2);
			}
		}
		ans = 987654321;
		numbers = new int[m];
		go(0,0);
		System.out.println(ans);
		
	}
	
	public static void go(int cnt, int idx) {
		if(cnt == m) {
			int sum = 0;
			for(int i = 0 ; i < list1.size() ; i++) {
				int min = 987654321;
				for(int j : numbers) {
					min = Math.min(dis[i][j], min);
				}
				sum += min;
			}
			ans = Math.min(ans, sum);
			return;
		}
		
		for(int i = idx ; i < list2.size() ; i++) {
			numbers[cnt] = i;
			go(cnt+1, i+1);
		}
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 1202. 보석 도둑  (0) 2021.05.13
[BAEKJOON] 17143. 낚시왕  (0) 2021.05.12
[BAEKJOON] 10026. 적록색약  (0) 2021.05.10
[BAEKJOON] 14500. 테트로미노  (0) 2021.05.09
[BAEKJOON] 14503. 로봇 청소기  (0) 2021.05.09