Skip to content

Commit 2200205

Browse files
authored
Merge pull request #111 from AlgorithmWithGod/Seol-JY
[20250213] BOJ / 골드5 / 톱니바퀴(2) / 설진영
2 parents 88ca675 + 81b69f6 commit 2200205

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
public class Main {
5+
private static int count = 0;
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
int T = Integer.parseInt(br.readLine());
10+
int[] arr = new int[T];
11+
12+
for (int i = 0; i < T; i++) {
13+
arr[i] = Integer.parseInt(br.readLine(), 2);
14+
}
15+
16+
int K = Integer.parseInt(br.readLine());
17+
18+
for (int i = 0; i < K; i++) {
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
int index = Integer.parseInt(st.nextToken()) - 1;
21+
int direction = Integer.parseInt(st.nextToken());
22+
23+
int[] turns = new int[T];
24+
turns[index] = direction;
25+
26+
for (int j = index - 1; j >= 0; j--) {
27+
if (needTurn(arr[j], arr[j+1])) {
28+
turns[j] = -turns[j+1];
29+
} else {
30+
break;
31+
}
32+
}
33+
for (int j = index + 1; j < T; j++) {
34+
if (needTurn(arr[j-1], arr[j])) {
35+
turns[j] = -turns[j-1];
36+
} else {
37+
break;
38+
}
39+
}
40+
41+
for (int j = 0; j < T; j++) {
42+
if (turns[j] == 1) {
43+
arr[j] = clockwise(arr[j]);
44+
} else if (turns[j] == -1) {
45+
arr[j] = antiClockwise(arr[j]);
46+
}
47+
}
48+
}
49+
int answer = 0;
50+
for (int i = 0; i < T; i++) {
51+
if ((arr[i] & 128) == 128) {
52+
answer++;
53+
}
54+
}
55+
System.out.println(answer);
56+
}
57+
58+
59+
// 회전 여부 결정
60+
private static boolean needTurn(Integer left, Integer right) {
61+
return ((left & 32) == 32) != ((right & 2) == 2);
62+
}
63+
64+
// 회전시키기
65+
private static Integer clockwise(Integer target) {
66+
int adjust = ((target & 1) == 1) ? 128 : 0;
67+
return (target >> 1) + adjust;
68+
}
69+
70+
private static Integer antiClockwise(Integer target) {
71+
int adjust = ((target & 128) == 128) ? -255 : 0;
72+
return (target << 1) + adjust;
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)