티스토리 뷰

알고리즘/백준

[JAVA] 백준 15686 치킨배달

코딩가딩 2022. 1. 5. 17:23

https://www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

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

www.acmicpc.net

 

💡 조합 코드를 작성할 수 있는지, 좌표 거리를 코드로 구현할 수 있는지

 

문제 해결 과정

1. 집과 치킨집의 좌표 list로 저장

2. 치킨집 M개 조합 경우 구하기

3. M개의 치킨집과 집 최소 치킨거리 계산

 

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class Main {
    
    static int N, M, min = Integer.MAX_VALUE;
    static ArrayList<Node> houses = new ArrayList<>();
    static ArrayList<Node> chickens = new ArrayList<>();
    
    static int[] tgt;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
 
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        tgt = new int[M];
        
        for(int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<N; j++) {
                int tmp = Integer.parseInt(st.nextToken());
                
                if(tmp == 1) houses.add(new Node(i, j));
                else if(tmp == 2) chickens.add(new Node(i, j));
            }
        }
        
        // 치킨 집 comb
        comb(00);
        
        System.out.println(min);
    }
    
    static void comb(int tgtIdx, int srcIdx) {
        if(tgtIdx == M) {
            
            // 최소 치킨거리 합
            int sum = 0;
            
            for(int i=0; i<houses.size(); i++) {
                int cDis = Integer.MAX_VALUE;
                
                for(int j=0; j<M; j++) {
                    int tmp = calDis(i, tgt[j]);
                    cDis = Math.min(cDis, tmp);
                }
                
                sum += cDis;
            }
            
            min = Math.min(min, sum);
            return;
        }
        
        for(int i=srcIdx; i<chickens.size(); i++) {
            tgt[tgtIdx] = i;
            comb(tgtIdx+1, i+1);
        }
    }
    
    // 치킨거리
    static int calDis(int houseIdx, int chickenIdx) {
        Node h = houses.get(houseIdx);
        Node c = chickens.get(chickenIdx);
        
        return Math.abs(h.x-c.x) + Math.abs(h.y-c.y);
    }
    
    static class Node {
        int x, y;
        
        public Node(int y, int x) {
            this.y = y;
            this.x = x;
        }
    }
 
}
 
cs

'알고리즘 > 백준' 카테고리의 다른 글

[JAVA] 백준 2529번 부등호  (0) 2021.12.19
[JAVA] 백준 11729번  (0) 2021.12.16
[JAVA] 백준 11659번  (0) 2021.12.13
[JAVA] 백준 17471번  (0) 2021.03.24
[JAVA] 백준 14891번  (0) 2021.03.16
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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 29 30 31
글 보관함