Algorithm/백준
[백준] 10819번 차이를 최대로
sirius
2021. 3. 4. 17:25
https://www.acmicpc.net/problem/10819
10819번: 차이를 최대로
첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main {
public static int N;
public static int[] arr;
public static int[] num;
public static boolean[] visit;
public static int max = Integer.MIN_VALUE;
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());
arr = new int[N];
num = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
num[i] = Integer.parseInt(st.nextToken());
}
visit = new boolean[N];
dfs(0);
System.out.println(max);
}
public static void dfs(int depth) {
if(depth == N) {
int sum = 0;
for(int i= 0; i < N-1; i++) {
sum += Math.abs(arr[i] - arr[i+1]);
}
max = Math.max(sum, max);
return;
}
for(int i = 0 ; i < N ; i ++ ) {
if(!visit[i]) {
visit[i] = true;
arr[depth] = num[i];
dfs(depth + 1);
visit[i] = false;
}
}
}
}