본문 바로가기
알고리즘

ALGOSPOT - Synchronizing Clocks(JAVA)

by 장중앙 2021. 12. 16.

https://www.algospot.com/judge/problem/read/CLOCKSYNC

 

algospot.com :: CLOCKSYNC

Synchronizing Clocks 문제 정보 문제 그림과 같이 4 x 4 개의 격자 형태로 배치된 16개의 시계가 있다. 이 시계들은 모두 12시, 3시, 6시, 혹은 9시를 가리키고 있다. 이 시계들이 모두 12시를 가리키도록

www.algospot.com

 

풀이

스위치를 누르는 모든 경우의 수를 탐색(브루트 포스)

 

3, 6, 9, 12를 1, 2, 3, 0으로 변환

for (int j = 0; j < LEN; j++) {
	int temp = Integer.parseInt(st.nextToken());
	watches[j] = (temp / 3) % 4;
}

같은 스위치를 4번 누를 경우 연결된 시계가 다시 원래 시간으로 돌아감 -> 클릭 수 = 0 ~ 3

0 ~ 3번 클릭에 따라 재귀문으로 모든 경우의 수를 확인

private static void calcMin(int switchIdx, int cnt) {
	if (isAllPass()) {
		if (cnt < MIN)
			MIN = cnt;
		return;
	}
	if (switchIdx > 9) return;

	for (int i = 0; i < 4; i++) {
		calcMin(switchIdx + 1, cnt + i);
		for (int watchIdx : switchLink[switchIdx]) {
			watches[watchIdx] = (watches[watchIdx] + 1) % 4;
		}
	}
}

 

 

전체 코드

import java.io.*;
import java.util.*;

public class Main {
    static int LEN = 16;
    static final int INF = 987654321;
    static int MIN;
    static int[] watches = new int[LEN];
    static int[][] switchLink =
            {
                    {0, 1, 2},
                    {3, 7, 9, 11},
                    {4, 10, 14, 15},
                    {0, 4, 5, 6, 7},
                    {6, 7, 8, 10, 12},
                    {0, 2, 14, 15},
                    {3, 14, 15},
                    {4, 5, 7, 14, 15},
                    {1, 2, 3, 4, 5},
                    {3, 4, 5, 9, 13}
            };

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

        StringTokenizer st;
        st = new StringTokenizer(br.readLine());

        int testCase = Integer.parseInt(st.nextToken());


        for (int i = 0; i < testCase; i++) {
            st = new StringTokenizer(br.readLine());

            for (int j = 0; j < LEN; j++) {
                int temp = Integer.parseInt(st.nextToken());
                watches[j] = (temp / 3) % 4; // 1,2,3,0 으로 구분
            }
            MIN=INF;
            calcMin(0, 0);
            if (MIN == INF)
                System.out.println(-1);
            else
                System.out.println(MIN);
        }
    }

    private static void calcMin(int switchIdx, int cnt) {

        if (isAllPass()) {
            if (cnt < MIN)
                MIN = cnt;
            return;
        }
        if (switchIdx > 9) return;

        // 0 ~ 3번 클릭
        for (int i = 0; i < 4; i++) {
            calcMin(switchIdx + 1, cnt + i);
            for (int watchIdx : switchLink[switchIdx]) {
                watches[watchIdx] = (watches[watchIdx] + 1) % 4;
            }
        }
    }
    // 모든 시간이 12시(0)인지 확인 
    private static boolean isAllPass() {
        for (int i = 0; i < LEN; i++) {
            if (watches[i] != 0)
                return false;
        }
        return true;
    }
}

'알고리즘' 카테고리의 다른 글

BOJ 23291 - 어항 정리(Java)  (0) 2021.12.22
BOJ 2638 - 치즈(Java)  (1) 2021.12.20
Programmers - 문자열 압축(Java)  (0) 2021.12.16
BOJ 16234 - 인구 이동(Java)  (0) 2021.12.14
BOJ 11000 - 강의실 배정(Java)  (0) 2021.12.07

댓글