티스토리 뷰

문제풀이/백준

14891. 톱니바퀴

BiteSnail 2024. 1. 12. 23:12

문제 개요

문제가 요구하는대로 구현할 수 있는지를 묻는 문제입니다.

문제 접근

구현을 더 연습해야겠다고 느끼게 해준 문제입니다. 간단한 문제였는데, 로직을 잘못 설정해서 이리 꼬이고 저리 꼬이고 생각보다 많은 시간이 걸렸습니다. 핵심은 N번째 톱니바퀴가 회전할 때 주위의 톱니바퀴의 회전을 재귀함수를 통해 구현한 것이라고 생각합니다. 바뀌기 전 값으로 톱니바퀴가 움직일지 움직이지 않을지 결정해야 하기 때문입니다.

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class Main {
    static List<List<Character>> gears;
    static int N;
    static int[][] opers;
    static int[] polars;

    public static void input() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        gears = new ArrayList<>();
        polars = new int[4];
        for (int i = 0; i < 4; i++) {
            gears.add(bf.readLine()
                    .chars()
                    .mapToObj(c -> (char) c)
                    .collect(Collectors.toCollection(ArrayList::new)));
            polars[i] = 2;
        }
        N = Integer.parseInt(bf.readLine());

        opers = new int[N][2];

        for (int i = 0; i < N; i++) {
            String[] temp = bf.readLine().split(" ");
            opers[i][0] = Integer.parseInt(temp[0]) - 1;
            opers[i][1] = Integer.parseInt(temp[1]);
        }
    }

    static boolean isDifferent(int left, int right){
        if(left > right){
            int temp = left;
            left = right;
            right = temp;
        }
        if(left < 0 || right >= 4){
            return false;
        }
        return gears.get(left).get(polars[left]) != gears.get(right).get((polars[right]+4)%8);
    }

    static void rotate(int idx, boolean isClock){
        if(isClock){
            clockwise(idx);
            return;
        }
        counterclockwise(idx);
    }

    static void spread(int n, boolean isClock, boolean isLeft){
        if(isLeft){
            // leftSpread;
            if(isDifferent(n+1, n)){
                spread(n-1, !isClock, isLeft);
                rotate(n, isClock);
            }
            return;
        }
        // rightSpread;
        if(isDifferent(n-1, n)){
            spread(n+1, !isClock, isLeft);
            rotate(n, isClock);
        }
    }

    static void clockwise(int n) {
        polars[n] = (polars[n] + 7) % 8;
    }

    static void counterclockwise(int n) {
        polars[n] = (polars[n] + 1) % 8;
    }

    public static void process() {
        for (int i = 0; i < N; i++) {
            boolean isClock = opers[i][1] == 1;
            if(isDifferent(opers[i][0]-1, opers[i][0])){
                spread(opers[i][0]-1, !isClock, true);
            }
            if(isDifferent(opers[i][0], opers[i][0]+1)){
                spread(opers[i][0]+1, !isClock, false);
            }
            rotate(opers[i][0], isClock);
        }
        int result = 0;
        int multi = 1;
        for (int i = 0; i < 4; i++) {
            if (gears.get(i).get((polars[i]+6)%8) == '1') {
                result += multi;
            }
            multi *= 2;
        }
        System.out.println(result);
    }

    public static void main(String[] args) throws IOException {
        input();
        process();
    }
}

문제

https://www.acmicpc.net/problem/14891

'문제풀이 > 백준' 카테고리의 다른 글

14502. 연구소  (0) 2024.01.15
16234. 인구 이동  (1) 2024.01.14
15686. 치킨 배달  (0) 2024.01.10
14501. 퇴사  (0) 2024.01.09
2668. 숫자고르기  (0) 2024.01.07
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
글 보관함