알고리즘

bj14891(톱니바퀴)-구현

쥐4 2025. 5. 22. 23:38

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

 

구현 문제는 시간 줄이기가 어려운 것 같다. 젠장

톱니바퀴의 상태를 클래스를 분리해 관리해주니, 쉽게 풀렸다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    static CogWheel[] cogWheels;
    static int K;
    static List<int[]> commands = new ArrayList<>();
    static StringTokenizer stringTokenizer;
    static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        initialize();
        System.out.println(solve());
    }

    private static int solve() {
        for(int[] command : commands){
            int wheelNum = command[0];
            int dir = command[1];
            processWheel(wheelNum, dir, new boolean[4]);
        }

        int answer = 0;
        for(int i = 0; i < 4; i++){
            if(cogWheels[i].status[0] == 1){
                answer += (int) Math.pow(2,i);
            }
        }
        return answer;
    }

    private static void processWheel(int wheelNum, int dir, boolean[] isTurned) {
        if(wheelNum < 0 || wheelNum > 3)return;
        if(isTurned[wheelNum])return;
        isTurned[wheelNum] = true;

        if (wheelNum + 1 <= 3) {
            int rightMatchStatus = cogWheels[wheelNum + 1].status[6];
            if(rightMatchStatus != cogWheels[wheelNum].status[2]) {   //맞닿아있는것이 다르다면 회전
                processWheel(wheelNum + 1, (-dir), isTurned);
            }
        }


        if(wheelNum - 1 >= 0){
            int leftMatchStatus = cogWheels[wheelNum-1].status[2];
            if(leftMatchStatus != cogWheels[wheelNum].status[6]) {   //맞닿아있는것이 다르다면 회전
                processWheel(wheelNum - 1, (-dir), isTurned);
            }
        }


        //현재 톱니 회전
        cogWheels[wheelNum].turn(dir);
    }

    private static void initialize() throws IOException {
        cogWheels = new CogWheel[4];
        for(int i = 0; i < 4; i++){
            String line = bufferedReader.readLine();
            cogWheels[i] = new CogWheel();
            for(int j = 0; j < 8; j++){
                cogWheels[i].status[j] = Character.getNumericValue(line.charAt(j));
            }
        }
        K = Integer.parseInt(bufferedReader.readLine());
        for(int i = 0; i < K; i++){
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
            int num = Integer.parseInt(stringTokenizer.nextToken());
            int dir = Integer.parseInt(stringTokenizer.nextToken());
            commands.add(new int[]{num-1, dir});
        }
    }
}

class CogWheel{
    int[] status;

    public CogWheel() {
        this.status = new int[8];
    }

    public void turn(int dir) {
        if(dir == 1){
            int curr = status[7];
            for(int i = 7; i > 0; i--){
                status[i] = status[i-1];
            }
            status[0] = curr;
        }else{
            int curr = status[0];
            for(int i = 0; i < 7; i++){
                status[i] = status[i+1];
            }
            status[7] = curr;
        }
    }
}