📌 고정 게시글

📢 공지합니다

이 게시글은 메인 페이지에 항상 고정되어 표시됩니다.

최코딩의 개발

[백준 14891번] 톱니바퀴 본문

코딩테스트/백준

[백준 14891번] 톱니바퀴

seung_ho_choi.s 2025. 5. 20. 23:07
728x90

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

 

package implement;

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 Implement14891 {
    static List<List<Integer>> config;

    static int K;

    static int answer;

    static boolean[] visit;



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

        // 0번 무시 해도됨 그냥 편하게 할려고
        config.add(new ArrayList<>());


        for (int i = 1; i < 5; i++) {
            config.add(new ArrayList<>());
            String input = br.readLine();
            List<Integer> setting = config.get(i);
            for (int j = 0; j < input.length(); j++) {
                setting.add(input.charAt(j) - '0');
            }
        }

        K = Integer.parseInt(br.readLine());
        answer = 0;
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            int line = Integer.parseInt(st.nextToken());
            int dir = Integer.parseInt(st.nextToken());
            visit = new boolean[5]; // 0 제외
            solution(line, dir);
        }

        int score = 1;
        for (int cnt = 1; cnt < 5; cnt++) {
            List<Integer> setting = config.get(cnt);
            if (setting.get(0) == 1) {
                answer += score;
            }
            score *= 2;
        }
        System.out.println(answer);

    }

    static void solution(int line, int dir) {
        List<Integer> lineSetting = config.get(line);

        // 다른 시계방향과 극이 맞는지 봐야됨
        // 1이 S극, 0이 N극
        if (line == 1) {
            back(line, dir, lineSetting);

        } else if (line == 4) {
            front(line, dir, lineSetting);

        } else {
            front(line, dir, lineSetting);
            back(line, dir, lineSetting);

        }


        // 시계(1) 방향이면 뒷 요소 제거해서 앞으로 오기
        // 반 시계(-1) 방향이면 앞요소 제거해서 뒤로 배치
        if (dir == 1) {
            int last = lineSetting.remove(7);
            lineSetting.add(0, last);
        } else if (dir == -1) {
            int first = lineSetting.remove(0);
            lineSetting.add(first);
        }

    }

    private static void front(int line, int dir, List<Integer> lineSetting) {
        int compareA = lineSetting.get(6);
        int compareB = config.get(line - 1).get(2);
        visit[line] = true;
        if (compareA != compareB && !visit[line - 1]) {
            int nextDir;
            if (dir == 1) {
                nextDir = -1;
            } else {
                nextDir = 1;
            }
            solution(line - 1, nextDir);
        }
    }

    private static void back(int line, int dir, List<Integer> lineSetting) {
        int compareA = lineSetting.get(2);
        int compareB = config.get(line + 1).get(6);
        visit[line] = true;
        if (compareA != compareB && !visit[line + 1]) {
            int nextDir;
            if (dir == 1) {
                nextDir = -1;
            } else {
                nextDir = 1;
            }
            solution(line + 1, nextDir);
        }
    }
}

 

문제 보고 기겁했는데 풀다보니 그냥 리스트 그리고 백트래킹으로 푼 간단한 문제이다. 

 

문제의 핵심은 돌기전에 먼저 극이 다른지 파악해야된다. 돌고 나서 극이 다른지 판단하면 안된다. 따라서 필자는 톱니바퀴가 도는 코드를 극이 다른지 비교한 다음에 코드를 실행하도록 했다. 문제를 잘 못읽어서 앞뒤로 코드 바꿨더니 오류가 났다.. ㅎㅎ 

728x90

'코딩테스트 > 백준' 카테고리의 다른 글

[백준 15683번] 감시  (0) 2025.05.21
[백준 14890번] 경사로  (0) 2025.05.19
[백준 14500번] 테트로미노  (0) 2025.05.18
[백준 12100번] 2048(Easy)  (0) 2025.05.16
[백준 14719번] 빗물  (0) 2025.05.15