본문 바로가기
Problem Solving/Baekjoon

[백준] 18788 Swapity Swap - Graph Theory / Java

by graycode 2023. 12. 14.

 문제 링크

 

18788번: Swapity Swap

Initially, the order of the cows is $[1,2,3,4,5,6,7]$ from left to right. After the first step of the process, the order is $[1,5,4,3,2,6,7].$ After the second step of the process, the order is $[1,5,7,6,2,3,4]$. Repeating both steps a second time yields t

www.acmicpc.net

 

 풀이 코드

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

public class Main {

    static int a1, a2, b1, b2;

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

        int n = read(), k = read();
        a1 = read();
        a2 = read();
        b1 = read();
        b2 = read();

        int[] arr = new int[n + 2];
        for (int i = 1; i <= n; i++) {
            int nxt = i, cnt = 1;
            while ((nxt = swap(nxt)) != i) cnt++;

            int rtn = k % cnt;
            while (rtn-- > 0) nxt = swap(nxt);

            arr[nxt] = i;
        }

        for (int i = 1; i <= n; i++) sb.append(arr[i]).append("\n");

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

    private static int swap(int i) {
        if (a1 <= i && i <= a2) i = a1 + a2 - i;
        if (b1 <= i && i <= b2) i = b1 + b2 - i;

        return i;
    }

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

}

댓글