취미처럼

[백준] 10819번 차이를 최대로 본문

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;
            }
        }
    }
}

'Algorithm > 백준' 카테고리의 다른 글

[백준] 6603번 로또  (0) 2021.03.04
[백준] 10971번 외판원 순회 2  (0) 2021.03.04
[백준] 10974번 모든 순열  (0) 2021.03.04
[백준] 10973번 이전 순열  (0) 2021.03.04
[백준] 10972번 다음 순열  (0) 2021.03.04
Comments