๐Ÿฅ‡ [Baekjoon / Java] 15686. ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

์ดํ•˜์–€ยท2024๋…„ 6์›” 13์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
23/33

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

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

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

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

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

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๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.
//์˜ˆ1
5 3
0 0 1 0 0
0 0 2 0 1
0 1 2 0 0
0 0 1 0 0
0 0 0 0 2
//์˜ˆ2
5 2
0 2 0 1 0
1 0 1 0 0
0 0 0 0 0
2 0 0 1 1
2 2 0 1 2
//์˜ˆ3
5 1
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0
//์˜ˆ4
5 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

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


๋ฌธ์ œ ์ดํ•ด


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


์•Œ๊ณ ๋ฆฌ์ฆ˜


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 35๋ถ„

  • ์ž…๋ ฅ
    • N๊ณผ M ์ž…๋ ฅ๋ฐ›๊ธฐ(N = ์ž…๋ ฅ๋ฐ›์„ ์ค„ ์ˆ˜, M = ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜)
    • N๊ฐœ์˜ ์ค„ ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ
    • ๋„์‹œ ์ •๋ณด ์„ ์–ธํ•˜๊ธฐ : 0์€ ๋นˆ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘
    • ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
    • ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ
  • ์ถœ๋ ฅ
    • ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’, ์ฆ‰ ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ
import java.util.*;

public class Main {

  static int N = 0;
  static int M = 0;
  static ArrayList<int[]> house = new ArrayList<>(); //์ง‘
  static ArrayList<int[]> chicken = new ArrayList<>(); //์น˜ํ‚จ
  static int result = Integer.MAX_VALUE;
  static boolean[] chick;
  public static void main(String[] args){
        /*
        - ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’, ์ฆ‰ ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ
         */
    Scanner sc = new Scanner(System.in);

    // 1. N๊ณผ M ์ž…๋ ฅ๋ฐ›๊ธฐ(N = ์ž…๋ ฅ๋ฐ›์„ ์ค„ ์ˆ˜, M = ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜)
    int N = sc.nextInt();
    int M = sc.nextInt();

    // 2. N๊ฐœ์˜ ์ค„ ์ž…๋ ฅ๋ฐ›๊ธฐ -> ์ง‘, ์น˜ํ‚จ์ง‘ ์ •๋ณด
    ArrayList<int[]> house = new ArrayList<>(); //์ง‘
    ArrayList<int[]> chicken = new ArrayList<>(); //์น˜ํ‚จ
    for(int r=0; r<N; r++){
      for(int c=0; c<N; c++){
        switch(sc.nextInt()){
          case 1 : //1์€ ์ง‘
            house.add(new int[]{r,c});
            break;
          case 2 : //2๋Š” ์น˜ํ‚จ์ง‘
            chicken.add(new int[]{r,c});
            break;
        }
      }
    }
    combination(-1,0);
    System.out.println(result);
  }

  // 3. ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
  //    ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ
  public static void combination(int n, int r){
    if(r == M){
      int dist = 0;

      for (int i = 0; i < house.size(); i++) {
        int tmp = Integer.MAX_VALUE;
        for (int j = 0; j < chicken.size(); j++) {
          if (chick[j]) {
            int[] h = house.get(i);
            int[] c = chicken.get(j);
            tmp = Math.min(tmp, Math.abs(h[0] - c[0]) + Math.abs(h[1] - c[1]));
          }
        }
        dist += tmp;
      }
      result = Math.min(result, dist);
      return;

    }
  }
}


์˜ค๋‹ต์ฒดํฌ


  • ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ณ  0์ด ๋‚˜์˜ด.
  • combination ์—ฐ์‚ฐ์„ ์œ„ํ•ด ์ „์—ญ๋ณ€์ˆ˜๋กœ N, M, house, chicken ๋ฐ ๊ทธ ๋ฐฐ์—ด, result๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผ ๋ฉ”์ธ ํด๋ž˜์Šค ์•ˆ์—์„œ์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Œ!!!
    static int N;
    static int M;
    static ArrayList<int[]> house; //์ง‘
    static ArrayList<int[]> chicken; //์น˜ํ‚จ
    static int result = Integer.MAX_VALUE;
    static boolean[] chick;


์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 67๋ถ„(์ฒซ ํ’€์ด ์‹œ๊ฐ„ ํฌํ•จ)

  • ์ž…๋ ฅ

    • N๊ณผ M ์ž…๋ ฅ๋ฐ›๊ธฐ(N = ์ž…๋ ฅ๋ฐ›์„ ์ค„ ์ˆ˜, M = ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜)
    • N๊ฐœ์˜ ์ค„ ์ž…๋ ฅ๋ฐ›๊ธฐ -> ์ง‘, ์น˜ํ‚จ์ง‘ ์ •๋ณด
      • 1์€ ์ง‘
      • 2๋Š” ์น˜ํ‚จ์ง‘
  • ์ถœ๋ ฅ

    • combination ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ

    • ๋ณ„๋„ ๋ฉ”์„œ๋“œ๋กœ combination ์—ฐ์‚ฐ ๋ถ„๋ฆฌ
    • ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
    • ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ
import java.util.*;

public class Main {

    static int N;
    static int M;
    static ArrayList<int[]> house; //์ง‘
    static ArrayList<int[]> chicken; //์น˜ํ‚จ
    static int result = Integer.MAX_VALUE;
    static boolean[] chick;
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        // 1. N๊ณผ M ์ž…๋ ฅ๋ฐ›๊ธฐ(N = ์ž…๋ ฅ๋ฐ›์„ ์ค„ ์ˆ˜, M = ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜)
        N = sc.nextInt();
        M = sc.nextInt();

        // 2. N๊ฐœ์˜ ์ค„ ์ž…๋ ฅ๋ฐ›๊ธฐ -> ์ง‘, ์น˜ํ‚จ์ง‘ ์ •๋ณด
        house = new ArrayList<>(); //์ง‘
        chicken = new ArrayList<>(); //์น˜ํ‚จ
        for(int r=0; r<N; r++){
            for(int c=0; c<N; c++){
                switch(sc.nextInt()){
                    case 1 : //1์€ ์ง‘
                        house.add(new int[]{r,c});
                        break;
                    case 2 : //2๋Š” ์น˜ํ‚จ์ง‘
                        chicken.add(new int[]{r,c});
                        break;
                }
            }
        }
        chick = new boolean[chicken.size()];
        combination(0,0);
        System.out.println(result);
    }

    // 3. ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
    //    ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ : ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ
    public static void combination(int n, int r){
        if(r == M){
            int dist = 0;

            for (int i = 0; i < house.size(); i++) {
                int tmp = Integer.MAX_VALUE;
                for (int j = 0; j < chicken.size(); j++) {
                    if (chick[j]) {
                        int[] h = house.get(i);
                        int[] c = chicken.get(j);
                        tmp = Math.min(tmp, Math.abs(h[0] - c[0]) + Math.abs(h[1] - c[1]));
                    }
                }
                dist += tmp;
            }
            result = Math.min(result, dist);
            return;
        }
        for (int i = n; i < chick.length; i++) {
            chick[i] = true;
            combination(i+1, r + 1);
            chick[i] = false;
        }

    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€