-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
101 lines (86 loc) · 3.03 KB
/
Main.java
File metadata and controls
101 lines (86 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main {
static int[][] gears;
static boolean[] turnFlag, checked;
static int[] dirFlag;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
gears = new int[5][8];
for(int i=1; i<5; i++){
char[] c = br.readLine().toCharArray();
for(int j=0; j<c.length; j++) {
gears[i][j] = c[j] - '0';
}
}
turnFlag = new boolean[5]; // #1~4 톱니바퀴 돌릴지 말지 체크
dirFlag = new int[5]; // #1~4 톱니바퀴 돌릴 경우 방향 체크
checked = new boolean[5]; // #인접 톱니바퀴 중복체크 방지
int K = Integer.parseInt(br.readLine());
for(int i=0; i<K; i++){
String[] cmd = br.readLine().split(" "); // [0]: gear# , [1]: direction
int gear = Integer.parseInt(cmd[0]);
int direction = Integer.parseInt(cmd[1]);
initState(); // 상태값 초기화
// 인접 톱니바퀴 체크하면서 상태값 업데이트
turnFlag[gear] = true;
dirFlag[gear] = direction;
checked[gear] = true;
checkAdjGear(gear, direction);
// 돌리는 것으로 체크된 톱니바퀴들 돌리기
for(int j=1; j<5; j++){
if(turnFlag[j]){
turnGear(j, dirFlag[j]);
}
}
}
int sum = 0;
for(int i=1, num=0; i<5; i++, num++){
if(gears[i][0] == 0) continue;
sum += Math.pow(2, num);
}
System.out.println(sum);
br.close();
}
public static void initState() {
Arrays.fill(turnFlag, false);
Arrays.fill(dirFlag, 0);
Arrays.fill(checked, false);
}
public static void checkAdjGear(int n, int dir){
if(n < 1 || n >=5) return;
if(n-1 >= 1 && !checked[n-1]){
checked[n-1] = true;
if(gears[n-1][2] != gears[n][6]){
turnFlag[n-1] = true;
dirFlag[n-1] = (-1)*dir;
checkAdjGear(n-1, (-1)*dir);
}
}
if(n+1 < 5 && !checked[n+1]) {
checked[n+1] = true;
if(gears[n+1][6] != gears[n][2]) {
turnFlag[n+1] = true;
dirFlag[n+1] = (-1)*dir;
checkAdjGear(n+1, (-1)*dir);
}
}
}
public static void turnGear(int n, int dir) {
int[] tmp = Arrays.copyOf(gears[n], gears[n].length);
switch(dir) {
case 1: // 시계
for(int i=0; i<8; i++) {
gears[n][i] = tmp[(7+i)%8];
}
break;
case -1: // 반시계
for(int i=0; i<8; i++) {
gears[n][i] = tmp[(i+1)%8];
}
break;
}
}
}