School/Algorithm
[알고리즘]SYNCHRONZING CLOCK
0lynny
2021. 11. 25. 22:07
import java.util.*;
public class Student20191023 {
static int switches[][] = new int[10][]; //스위치
static int clock[] = new int[16]; //시계
final static int MIN = 99999;
public static void main(String[] args) {
// TODO Auto-generated method stub
scan();
int result = solve(clock, 0);
if(result >= MIN) {
result = -1;
}
System.out.println(result);
}
public static void scan() {
Scanner scan = new Scanner(System.in);
String input;
int i, j, k, h;
int value = 0;
i = 0;
input = scan.nextLine();
while(!(input.equals(""))) {
String[] line = input.split("\\s+"); //한줄을 읽어서 공백별로 분할하여 배열에 저장
if(i > 9) {
for(h = 0, k = 0; h < clock.length && k < line.length; k++, h++) {
clock[h] = Integer.parseInt(line[k]);
}
break;
}
switches[i] = new int[Integer.parseInt(line[1])]; //switches[0] = new int[3] 이런 형태로 생성
for(k = 2, j = 0; j < switches[i].length && k < line.length; k++, j++) {
value = (Integer.parseInt(line[k]));
switches[i][j] = value;
}
i++;
input = scan.nextLine();
}
scan.close();
}
public static void push(int[] clocks, int swt) {
for(int i = 0; i < switches[swt].length; i++) {
int n = switches[swt][i];
if(clocks[n] == 12) { //4번 돌아가면 원점
clocks[n] = 0;
}
clocks[n] += 3;
}
}
public static boolean is_twelve(int[] clocks) {
for(int i = 0; i < switches.length; i++) {
if(clocks[i] != 12) {
return false;
}
}
return true;
}
public static int solve(int[] clocks, int swt) {
int result = MIN;
if(swt == 10) {
return is_twelve(clocks) ? 0 : result;
}
for(int i = 0; i < 4; i++) {
result = Math.min(result, i + solve(clocks, swt + 1));
push(clocks, swt);
}
return result;
}
}