본문 바로가기

코딩테스트

[알고리즘 문제풀이] 백준14891번 톱니바퀴 ( 구현 )

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

 

 

 



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

import java.util.ArrayList;
import java.util.List;


public class Main {


    static int[][] wheal;
    static int N;
    static int K,D;
    static int ans;

    public static void main(String[] args) throws IOException {

        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));


        wheal = new int[4][8];

        for(int i=0;i<4;i++){
            String st = br.readLine();

            String[] temp = st.split("");
            for(int j=0;j<8;j++){
                wheal[i][j] = Integer.parseInt(temp[j]);
            }
        }

        StringTokenizer st= new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());

        for(int i=0;i<N;i++){
            st =new StringTokenizer(br.readLine());
            K=Integer.parseInt(st.nextToken()) -1 ;
            D=Integer.parseInt(st.nextToken());


            boolean[] rotate = new boolean[4];
            int[] rotateDirection = new int[4];

            rotate[K] = true;
            rotateDirection[K] = D;


            // 오른쪽 톱니바퀴 체크
            for(int s=K;s<3;s++){
                // s는 Wheal 번호
                if(wheal[s][2]== wheal[s+1][6]) break;
                else if( wheal[s][2] != wheal[s+1][6]){
                    rotate[s+1]=true;
                    rotateDirection[s+1] = -rotateDirection[s];

                }
            }

            // 왼쪽 톱니바퀴 체크
            for(int s=K;s>0;s--) {
                // s는 Wheal 번호
                if(wheal[s][6] == wheal[s-1][2]) break;
                else if (wheal[s][6] != wheal[s-1][2]) {
                    rotate[s-1]=true;
                    rotateDirection[s-1] = -rotateDirection[s];
                }
            }

            for(int j=0;j<4;j++){
                if(rotate[j]){
                    if(rotateDirection[j]==1) clockwise(j);
                    else AntiClockwise(j);
                }
            }

        }


        ans=0;
        if( wheal[0][0] == 1) ans+=1;
        if( wheal[1][0] == 1) ans+=2;
        if( wheal[2][0] == 1) ans+=4;
        if( wheal[3][0] == 1) ans+=8;

        System.out.println(ans);


    }

    // 시계 방향 회전 => 모든 배열을 오른쪽으로 1칸씩 이동
    static void clockwise(int whealNum){
        int last = wheal[whealNum][7];
        for(int i=7;i>=1;i--){
            wheal[whealNum][i]=wheal[whealNum][i-1];
        }
        wheal[whealNum][0]=last;
    }

    // 반시계 방향 => 모든 배열을 왼쪽으로 1칸씩 이동
    static void AntiClockwise(int whealNum){
        int first = wheal[whealNum][0];

        for(int i=1;i<=7;i++){
            wheal[whealNum][i-1] = wheal[whealNum][i];
        }
        wheal[whealNum][7]=first;
    }

    static int execute(int nextD,int s){
        if (nextD == 1) {
            clockwise(s);  // 시계방향 회전
            return -1;
        } else {
            AntiClockwise(s); // 반시계방향 회전
            return 1;
        }

    }

    static int setNextD(int D){
        if(D==1) return -1;
        else return 1;
    }


}