Skip to content

Commit

Permalink
Merge pull request #216 from AlgoLeadMe/12-mjj111
Browse files Browse the repository at this point in the history
12-mjj111
  • Loading branch information
9kyo-hwang authored Sep 24, 2024
2 parents 4a1f93b + f8848d7 commit 302e01a
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions mjj111/์น˜ํ‚จ๋ฐฐ๋‹ฌ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import java.io.*;
import java.util.*;


public class ์น˜ํ‚จ๋ฐฐ๋‹ฌ {

static int N, M;
static int[][] graph;
static ArrayList<Point> person;
static ArrayList<Point> chicken;
static int answer;
static boolean[] open;
private static int CHICKEN = 2;
private static int HOME = 1;

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());

graph = new int[N][N];
person = new ArrayList<>();
chicken = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
graph[i][j] = Integer.parseInt(st.nextToken());

if (graph[i][j] == HOME) {
person.add(new Point(i, j));
}
if (graph[i][j] == CHICKEN) {
chicken.add(new Point(i, j));
}
}
}

answer = Integer.MAX_VALUE;
open = new boolean[chicken.size()];

DFS(0, 0);
}

public static void DFS(int start, int cnt) {
if(cnt != M) {
for (int i = start; i < chicken.size(); i++) {
open[i] = true;
DFS(i + 1, cnt + 1);
open[i] = false;
}
return;
}

int res = 0;
for (int i = 0; i < person.size(); i++) {
int minimum = Integer.MAX_VALUE;

for (int j = 0; j < chicken.size(); j++) {
if (open[j]) {
int distance = getDistance(i, j);
minimum = Math.min(minimum, distance);
}
}

res += minimum;
}
answer = Math.min(answer, res);
}

private static int getDistance(final int i, final int j) {
return Math.abs(person.get(i).x - chicken.get(j).x)
+ Math.abs(person.get(i).y - chicken.get(j).y);
}

private static class Point {
int x;
int y;

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

0 comments on commit 302e01a

Please sign in to comment.