본문 바로가기
Problem Solving/Baekjoon

[백준] 18323 Photoshoot - Brute Force / Java

by graycode 2023. 11. 29.

 문제 링크

 

18323번: Photoshoot

Farmer John is lining up his $N$ cows ($2\le N\le 10^3$), numbered $1\ldots N$, for a photoshoot. FJ initially planned for the $i$-th cow from the left to be the cow numbered $a_i,$ and wrote down the permutation $a_1,a_2,\ldots,a_N$ on a sheet of paper. U

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    static int n;
    static int[] arr;

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb;

        arr = new int[(n = read()) - 1];
        for (int i = 0; i < arr.length; i++) arr[i] = read();

        int i = 1;
        while (!generate(i, sb = new StringBuilder(), new boolean[n + 1]) && i < arr[0]) i++;

        bw.write(sb.toString());
        bw.flush();
    }

    private static boolean generate(int a, StringBuilder sb, boolean[] visit) {
        sb.append(a).append(" ");
        visit[a] = true;

        for (int b : arr) {
            if ((a = b - a) <= 0 || a > n || visit[a]) return false;
            sb.append(a).append(" ");
            visit[a] = true;
        }

        return true;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글