Skip to content

Commit 910ad9a

Browse files
authored
Merge pull request #1035 from AlgorithmWithGod/lkhyun
[20251004] BOJ / G4 / 전구와 스위치 / 이강현
2 parents e452033 + 2cb48be commit 910ad9a

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.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static StringBuilder sb = new StringBuilder();
10+
static int N;
11+
static int[] input;
12+
static int[] target;
13+
static int cnt1 = 0;
14+
static int cnt2 = 0;
15+
16+
public static void main(String[] args) throws Exception {
17+
N = Integer.parseInt(br.readLine());
18+
input = new int[N];
19+
target = new int[N];
20+
21+
String temp1 = br.readLine();
22+
String temp2 = br.readLine();
23+
24+
for(int i = 0;i<N;i++){
25+
input[i] = Character.getNumericValue(temp1.charAt(i));
26+
target[i] = Character.getNumericValue(temp2.charAt(i));
27+
}
28+
int[] input1 = input.clone();
29+
int[] input2 = input.clone();
30+
31+
for(int i = 1; i<N; i++){
32+
if(input1[i-1] != target[i-1]){
33+
input1[i-1] ^= 1;
34+
input1[i] ^= 1;
35+
if(i+1 < N) {
36+
input1[i + 1] ^= 1;
37+
}
38+
cnt1++;
39+
}
40+
}
41+
for (int i = 0; i < N; i++) {
42+
if(input1[i] != target[i]){
43+
cnt1 = Integer.MAX_VALUE;
44+
break;
45+
}
46+
}
47+
48+
input2[0] ^= 1;
49+
input2[1] ^= 1;
50+
cnt2++;
51+
for (int i = 1; i < N; i++) {
52+
if(input2[i-1] != target[i-1]){
53+
input2[i-1] ^= 1;
54+
input2[i] ^= 1;
55+
if(i+1 < N) {
56+
input2[i + 1] ^= 1;
57+
}
58+
cnt2++;
59+
}
60+
}
61+
for (int i = 0; i < N; i++) {
62+
if(input2[i] != target[i]){
63+
cnt2 = Integer.MAX_VALUE;
64+
break;
65+
}
66+
}
67+
int cnt = Math.min(cnt1, cnt2);
68+
if(cnt == Integer.MAX_VALUE){
69+
cnt = -1;
70+
}
71+
bw.write(cnt+"");
72+
bw.close();
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)