PS/BaekJoon
10819 JAVA ์ฐจ์ด๋ฅผ ์ต๋๋ก
chaerlo127
2022. 10. 26. 23:47
728x90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// ์์ด์ด ๋์ฌ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๊ณ ๊ทธ ํ์ ์ ๋ ๊ฐ์ผ๋ก ๋ํด์ค๋ค.
public class Main {
static int[] array;
static int[] array2;
static boolean[] check;
static int max;
static int index;
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
index = Integer.parseInt(br.readLine());
array = new int[index];
array2 = new int[index];
String a = br.readLine();
StringTokenizer st = new StringTokenizer(a);
int i = 0;
while(st.hasMoreTokens()) {
array[i++] = Integer.parseInt(st.nextToken());
}
check = new boolean[index];
dfs(0);
System.out.println(max);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void dfs(int count) {
if(count == index) {
int sum = 0;
for(int i = 0; i<index-1; i++) {
sum += Math.abs(array2[i] - array2[i+1]);
}
max = Math.max(sum, max);
}else {
// ์์ด๋ก ๋์ฌ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์
// ์ธ ๊ฐ์ ์์๋ผ๋ฉด
// 1 - 2 - 3, 1 - 3 - 2, 2 - 1- 3, 2 - 3 - 1, 3 - 1 - 2, 3 - 2 - 1
for(int i = 0; i<index; i++) {
if(check[i]) continue;
check[i] = true;
array2[count] = array[i];
dfs(count+1);
check[i] = false;
}
}
}
}
728x90