코딩테스트/백준

[백준]14891번 톱니바퀴 - Java

GAEBAL 2022. 4. 24. 13:14
728x90

문제

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

 

14891번: 톱니바퀴

총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

www.acmicpc.net

 

 

풀이

아 처음에 톱니바퀴의 위치에 따라서 옆 톱니바퀴가 회전하는지 안하는지를 생각을 안해줘서 너무 노가다로 풀었다,,,, 진짜 너무 빡침 ㅠ

이래서 설계가 중요하구나 느꼈음./...

 

중간에 코드 짜다가 아 이렇게 풀면 안되는구나 하고 알았는데 톱니바퀴 4개라서 노가다도 될 것 같고, 이미 돌아가기엔 너무 늦었으니까 이렇게 풀고 다음에  톱니바퀴2 문제나 이 문제 한번 더 풀어야 겠다 라고 결심했음...

 

진짜 나새끼 빡대가리,,, ㅠㅠㅠ

 

돌릴 수 있는지 판단하는 함수, 톱니바퀴 돌리는 함수, 그리고 메인함수 안에 스위치문(이게 노가다)으로 풀었는데 이렇게 하지 마세요 ㅠ 다음에 한번 더 풀어서 글 올려야겠음...!

 

자세한 건 주석으로!!

 

 

코드

// 14891번 톱니바퀴
// https://www.acmicpc.net/problem/14891

package BAEKJOON;

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

public class Num14891_톱니바퀴 {
    static int[][] wheel = new int[4][8];
    static int K;
    static int[][] arr;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;

        for (int i = 0; i < 4; i++) {
            String str = br.readLine();
            for (int j = 0; j < 8; j++) {
                wheel[i][j] = str.charAt(j) - '0';
            }
        }

        K = Integer.parseInt(br.readLine());
        arr = new int[K][2];
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            arr[i][0] = Integer.parseInt(st.nextToken()); // 몇번 톱니바퀴인지
            arr[i][1] = Integer.parseInt(st.nextToken()); // 어느 방향으로 돌릴건지
        }

        for (int i = 0; i < K; i++) {
            int realWheel = arr[i][0] - 1;
            int direction = arr[i][1];
            switch (realWheel) {
                case 0:
                    if (isRotate(wheel[realWheel][2], wheel[realWheel + 1][6])) {
                        if (isRotate(wheel[realWheel + 1][2], wheel[realWheel + 2][6])) {
                            if (isRotate(wheel[realWheel + 2][2], wheel[realWheel + 3][6])) {
                                rotate(realWheel + 1, -direction);
                                rotate(realWheel + 2, direction);
                                rotate(realWheel + 3, -direction);
                            } else {
                                rotate(realWheel + 1, -direction);
                                rotate(realWheel + 2, direction);
                            }
                        } else {
                            rotate(realWheel + 1, -direction);
                        }
                    }
                    rotate(realWheel, direction); // 해당 톱니바퀴는 어차피 회전해야됨
                    break;
                case 1:
                    if (isRotate(wheel[realWheel][2], wheel[realWheel + 1][6])) {
                        if (isRotate(wheel[realWheel + 1][2], wheel[realWheel + 2][6])) {
                            rotate(realWheel + 1, -direction);
                            rotate(realWheel + 2, direction);
                        } else {
                            rotate(realWheel + 1, -direction);
                        }
                    }
                    if (isRotate(wheel[realWheel][6], wheel[realWheel - 1][2])) {
                        rotate(realWheel - 1, -direction);
                    }
                    rotate(realWheel, direction);
                    break;
                case 2:
                    if (isRotate(wheel[realWheel][2], wheel[realWheel + 1][6])) {
                        rotate(realWheel + 1, -direction);
                    }
                    if (isRotate(wheel[realWheel][6], wheel[realWheel - 1][2])) {
                        if (isRotate(wheel[realWheel - 1][6], wheel[realWheel - 2][2])) {
                            rotate(realWheel - 1, -direction);
                            rotate(realWheel - 2, direction);
                        } else {
                            rotate(realWheel - 1, -direction);
                        }
                    }
                    rotate(realWheel, direction);
                    break;
                case 3:
                    if (isRotate(wheel[realWheel][6], wheel[realWheel - 1][2])) {
                        if (isRotate(wheel[realWheel - 1][6], wheel[realWheel - 2][2])) {
                            if (isRotate(wheel[realWheel - 2][6], wheel[realWheel - 3][2])) {
                                rotate(realWheel - 1, -direction);
                                rotate(realWheel - 2, direction);
                                rotate(realWheel - 3, -direction);
                            } else {
                                rotate(realWheel - 1, -direction);
                                rotate(realWheel - 2, direction);
                            }
                        } else {
                            rotate(realWheel - 1, -direction);
                        }
                    }
                    rotate(realWheel, direction);
                    break;
            }
        }

        int answer = 0;
        for (int i = 1; i <= 4; i++) {
            answer += wheel[i - 1][0] * Math.pow(2, i - 1);
        }

        System.out.println(answer);
    }

    private static boolean isRotate(int nowWheel, int anotherWheel) {
        if (nowWheel == anotherWheel) {
            return false;
        } else {
            return true;
        }
    }

    private static void rotate(int nowWheel, int direction) {
        int temp;
        if (direction == 1) { // 시계 방향이면
            temp = wheel[nowWheel][7];
            for (int i = 7; i > 0; i--) {
                wheel[nowWheel][i] = wheel[nowWheel][i - 1];
            }
            wheel[nowWheel][0] = temp;
        } else { // 반시계 방향이면
            temp = wheel[nowWheel][0];
            for (int i = 1; i <= 7; i++) {
                wheel[nowWheel][i - 1] = wheel[nowWheel][i];
            }
            wheel[nowWheel][7] = temp;
        }
    }
}
728x90