Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.05.29 - [BOJ] 15661. 링크와 스타트 #75

Closed
suhyunsim opened this issue May 30, 2021 · 0 comments
Closed

21.05.29 - [BOJ] 15661. 링크와 스타트 #75

suhyunsim opened this issue May 30, 2021 · 0 comments
Assignees
Labels
백트래킹 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented May 30, 2021

문제

핵심 아이디어

  • 스타트와 링크에서 인원수 제한 조간이 빠진 문제

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q15661;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static int n;
    static int[][] s;

    private static int go(int index, ArrayList<Integer> first, ArrayList<Integer> second) {
        if (index == n) {
            if (first.size() == 0) return -1;
            if (second.size() == 0) return -1;
            int t1 = 0;
            int t2 = 0;
            for (int i = 0; i < first.size(); i++) {
                for (int j = 0; j < first.size(); j++) {
                    if (i == j) continue;
                    t1 += s[first.get(i)][first.get(j)];
                }
            }
            for (int i = 0; i < second.size(); i++) {
                for (int j = 0; j < second.size(); j++) {
                    if (i == j) continue;
                    t2 += s[second.get(i)][second.get(j)];
                }
            }
            return Math.abs(t1 - t2);
        }
        int ans = -1;
        first.add(index);
        int t1 = go(index + 1, first, second);
        if (ans == -1 || (t1 != -1 && ans > t1)) {
            ans = t1;
        }
        first.remove(first.size() - 1);
        second.add(index);
        int t2 = go(index + 1, first, second);
        if (ans == -1 || (t2 != -1 && ans > t2)) {
            ans = t2;
        }
        second.remove(second.size() - 1);
        return ans;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        s = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                s[i][j] = sc.nextInt();
            }
        }
        ArrayList<Integer> first = new ArrayList<>();
        ArrayList<Integer> second = new ArrayList<>();
        System.out.println(go(0, first, second));
    }
}
@suhyunsim suhyunsim added this to the 5월 4주 차 milestone May 30, 2021
@suhyunsim suhyunsim self-assigned this May 30, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
백트래킹 성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant