Skip to content

Commit a51d970

Browse files
authored
[20250319] BOJ / G5 / 비트 문자열 재배열하기 / 권혁준
1 parent 8ecbe11 commit a51d970

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
if(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int N, M;
26+
static int start = 0, end1 = 0, end2 = 0;
27+
28+
public static void main(String[] args) throws Exception {
29+
30+
ready();
31+
solve();
32+
33+
bwEnd();
34+
35+
}
36+
37+
static void ready() throws Exception{
38+
39+
N = nextInt();
40+
M = nextInt();
41+
for(int i=N-1;i>=0;i--) if(nextInt() == 1) start |= (1<<i);
42+
int[] temp = new int[M];
43+
for(int i=0;i<M;i++) temp[i] = nextInt();
44+
int x = 0, y = 0;
45+
for(int i=M-1;i>=0;i--) {
46+
if(i%2 == 0) {
47+
x += temp[i];
48+
for(int j=0;j<temp[i];j++) end1 |= (1<<y++);
49+
}
50+
else {
51+
y += temp[i];
52+
for(int j=0;j<temp[i];j++) end2 |= (1<<x++);
53+
}
54+
}
55+
56+
}
57+
58+
static void solve() throws Exception{
59+
60+
boolean[] vis = new boolean[32768];
61+
vis[start] = true;
62+
Queue<int[]> Q = new LinkedList<>();
63+
Q.offer(new int[] {start,0});
64+
while(!Q.isEmpty()) {
65+
int[] now = Q.poll();
66+
int n = now[0], t = now[1];
67+
if(n == end1 || n == end2) {
68+
bw.write(t+"\n");
69+
return;
70+
}
71+
for(int i=0;i<N-1;i++) {
72+
int x = (1<<i) * ((n&(1<<i)) == 0 ? 0 : 1);
73+
int y = (1<<(i+1)) * ((n&(1<<(i+1))) == 0 ? 0 : 1);
74+
int next = n^x^y;
75+
next |= (x<<1) | (y>>1);
76+
if(!vis[next]) {
77+
vis[next] = true;
78+
Q.offer(new int[] {next,t+1});
79+
}
80+
}
81+
}
82+
83+
}
84+
85+
}
86+
87+
```

0 commit comments

Comments
 (0)