곽로그
[백준 15686, Java] 치킨배달 본문
반응형
1. 보완해야하는 것
1부터 N까지 M 개를 뽑는데, 중복허용, 중복없이, 오름차순, 내림차순!!
2. 이번주 역테가 이 정도 수준이였으면 좋겠다. ㅜㅜ
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
class Point{
int x;
int y;
int distance;
Point(int x, int y){
this.x =x;
this.y =y;
distance = 0;
}
Point(int x, int y, int distance){
this.x =x;
this.y =y;
this.distance =distance;
}
@Override
public String toString() {
return "("+x+","+y+")";
}
}
public class Main {
public static int N;
public static int M;
public static int[][] map;
public static boolean[] visited;
public static ArrayList<Point> chickenStore;
public static Point[] pickedChickenStore;
public static ArrayList<Point> homes;
public static int minChickenDistance = 987654321;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][N];
chickenStore = new ArrayList<>();
pickedChickenStore = new Point[M];
homes = new ArrayList<>();
for(int r=0;r<N;r++) {
st = new StringTokenizer(br.readLine());
for(int c=0;c<N;c++) {
int value = Integer.parseInt(st.nextToken());
map[r][c] = value;
if(value ==1) {
homes.add(new Point(r,c));
}
else if(value==2) {
chickenStore.add(new Point(r,c));
}
}
}
pickChickenStore(0,0);
bw.write(String.valueOf(minChickenDistance));
bw.flush();
}
public static void pickChickenStore(int index, int start) {
if(index>=M) {
calculateChkickenDistance(pickedChickenStore);
}
else if(start>=chickenStore.size()) {
return;
}
else {
for(int i = start; i<chickenStore.size(); i++) {
pickedChickenStore[index] = chickenStore.get(i);
pickChickenStore(index+1,i+1);
}
}
}
public static void calculateChkickenDistance(Point[] chikenStoreArray) {
int totalDistance= 0;
for(int h =0; h<homes.size();h++) {
Point home = homes.get(h);
int minDistance = 987654321;
for(int c = 0; c<chikenStoreArray.length;c++) {
Point chickenStore = chikenStoreArray[c];
int distance = Math.abs(home.x - chickenStore.x)+Math.abs(home.y-chickenStore.y);
minDistance = Math.min(minDistance,distance);
}
totalDistance+=minDistance;
}
minChickenDistance =Math.min(minChickenDistance, totalDistance);
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준1158, Java] 요세푸스 문제 (0) | 2020.10.20 |
---|---|
백준 19236 청소년 상어 (Java) (0) | 2020.10.17 |
[백준 14499, Java] 주사위 굴리기 (0) | 2020.10.15 |
[백준 12100, Java] 2046(easy) (0) | 2020.10.14 |
[백준 16236,Java] 아기상어 (0) | 2020.10.11 |
Comments