[๋ฐฑ์ค(BOJ)] 15686_์นํจ ๋ฐฐ๋ฌ JAVA
๋ฌธ์ : BOJ_15686๋ฒ ์นํจ ๋ฐฐ๋ฌ
๋ฌธ์ ์ค๋ช
N*N ํฌ๊ธฐ์ map์์ ์ง๊ณผ ์นํจ์ง์ด ์ฃผ์ด์ง๋ค.
์ต๋ m ๊ฐ์ ์นํจ์ง์ ์ ์ธํ ๋๋จธ์ง ์นํจ์ง์ ๋ค ์์ ์ผ ํ๋ค.
์ด๋ ์ง๊ณผ ์นํจ์ง๊ณผ์ ์ต์ ๊ฑฐ๋ฆฌ(์์ง๊ฑฐ๋ฆฌ์ ์ฐจ + ์ํ๊ฑฐ๋ฆฌ์ ์ฐจ
)์ ํฉ์ ์ต์๊ฐ์ ๊ตฌํด์ผ ํ๋ค.
Solution
์์ ํ์(์กฐํฉ)
N์ ์ต๋ ํฌ๊ธฐ๋ 50, M์ ์ต๋ ํฌ๊ธฐ๋ 13์ด๋ค.
13๊ฐ์ ์นํจ์ง์์ 7๊ฐ์ ์นํจ์ง์ ์ ํํ๋ ์กฐํฉ์ ์๊ฐ ๊ฐ์ฅ ํฌ๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐํด๋ณด๋ฉด,
50 * 50(๋์์ ์ต๋ ํฌ๊ธฐ) * 13C7 * 7(๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ง๋ค 7๊ฐ์ ์นํจ์ง๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ํ์ธํด๋ด) == 30,030,000
์ด๋ฏ๋ก ์กฐํฉ์ ์ด์ฉํ ์์ ํ์์ผ๋ก ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
Description
- ์กฐํฉ์
recursion
์ด๋ ๋ฉ์๋์์ ์ฌ๊ท๋ก ์งํํ์๋ค. - ์ขํ๊ฐ์
Node
๋ผ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ x, y ๊ฐ์ ๋ฐ์๋ค.
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
class Node{
private int x;
private int y;
public Node(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Main {
public static int n;
public static int m;
public static int[] [] map;
public static ArrayList<Node> chicken = new ArrayList<>();
public static ArrayList<Node> house = new ArrayList<>();
public static ArrayList<ArrayList<Integer>> combs = new ArrayList<ArrayList<Integer>>();
public static boolean[] chk;
public static int getDis(Node a, Node b){
int result = 0;
result+= Math.abs(a.getX() - b.getX());
result+= Math.abs(a.getY() - b.getY());
return result;
}
public static void recursion(int idx, int cnt){
if(cnt == m){
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0; i<chk.length; i++){
if(chk[i]) arr.add(i);
}
combs.add(arr);
return;
}
for(int i = idx; i<chk.length; i++){
if(chk[i]) continue;
chk[i] = true;
recursion(i,cnt+1);
chk[i] = false;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
for(int i=1; i<=n; i++){
st = new StringTokenizer(br.readLine());
for(int j=1; j<=n; j++){
map[i][j] = Integer.parseInt(st.nextToken());
if(map[i][j]==1) house.add(new Node(i,j));
else if(map[i][j]==2) chicken.add(new Node(i,j));
}
}
chk = new boolean[chicken.size()];
recursion(0,0);
int res = (int)1e9;
for(int i =0; i<combs.size();i++){
int value = 0;
for(int j=0; j<house.size();j++){
Node houseNode = house.get(j);
int temp = 0;
for(int k=0;k<combs.get(i).size();k++){
Node chickenNode = chicken.get(combs.get(i).get(k));
int dist = getDis(houseNode, chickenNode);
if(k==0) temp = dist;
else temp = Math.min(dist,temp);
}
value += temp;
}
res = Math.min(res,value);
}
bw.write(String.valueOf(res)+"\n");
bw.flush();
br.close();
bw.close();
}
}
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] LCS 2(9252) (0) | 2022.03.16 |
---|---|
[BOJ] ๊ฒฝ์์ ์ ์ผ(18405) (0) | 2022.03.16 |
[BOJ] ์๊ธฐ ์์ด(16236) (0) | 2022.03.15 |
[BOJ] ํ์ฑ ํฐ๋(2887) (0) | 2022.03.15 |
[Programmers] ์๋ฌผ์ ์ ์ด์ (2020 KAKAO BLIND RECRUITMENT) (0) | 2022.03.15 |