N개의 고객을 중복없이 고르는 순열
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Route {//static클래스는 원래 클래스 안에 있어야함
static class Point{
public int r;
public int c;
Point(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public String toString() {
return "Point [r=" + r + ", c=" + c + "]";
}
}
static int res;
static int N;
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("input/swea_1247.txt"));
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= 10; tc++) {
res=Integer.MAX_VALUE;//최솟값을 구하는거니까 MAX값을 초기값으로
N =sc.nextInt();
Point company = new Point(sc.nextInt(), sc.nextInt());
Point home = new Point(sc.nextInt(), sc.nextInt());
Point [] cus= new Point[N];
for (int i = 0; i < N; i++) {
cus[i]= new Point(sc.nextInt(), sc.nextInt());
}
// System.out.println(Arrays.toString(cus));
move(cus,new Point[N],new boolean[N],0,company,home);
System.out.printf("#%d %d",tc, res);
System.out.println();
//System.out.println(cnt);
}
}
//static int cnt;
private static void move(Point[] cus, Point[] sel, boolean[] v, int k,Point company, Point home) {
if(k==sel.length) {
//여기서 최단거리를 비교
//cnt++;
int ans=0;
ans = Math.abs(company.r - sel[0].r)+ Math.abs(company.c - sel[0].c);
for (int i = 0; i < N-1 ; i++) {
ans += Math.abs(sel[i].r-sel[i+1].r) + Math.abs(sel[i].c-sel[i+1].c);
}
ans += Math.abs(sel[N-1].r-home.r)+ Math.abs(sel[N-1].c-home.c);
// System.out.println(ans);
// System.out.println(ans);
res = Math.min(ans,res);
return;
}
for (int i = 0; i < cus.length; i++) {
if(!v[i]) {
v[i]=true;
sel[k] = cus[i];
move(cus, sel, v, k+1,company, home);
v[i]= false;
}
}
}
}
res값에 0을 집어넣어버려서 값이 계속0으로 찍히는 오류를 겪었다..
최솟값을 구하는경우엔 무조건 max value를 주자!
'Problem Solving > SWEA' 카테고리의 다른 글
SWEA- 5215 햄버거 다이어트 (0) | 2021.02.06 |
---|---|
SWEA-1218 괄호 짝짓기 (0) | 2021.02.06 |
SWEA-1954 달팽이 숫자 (0) | 2021.02.06 |
SWEA-1210 Ladder 1 (0) | 2021.02.06 |
[SWEA] 1289 원재의 메모리 복구하기 (0) | 2021.02.06 |
댓글