Skip to content

Commit 221c0e4

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 2479 java solution (#92)
* [ADD] baekjoon 2479 java solution * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent 7b55a61 commit 221c0e4

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

solutions/baekjoon/2479/Main.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Authored by: beberiche
2+
// Co-authored by: -
3+
// Link: http://boj.kr/92ba32984b594dd68c8353a90fb75743
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int N = rd.nextInt();
12+
int K = rd.nextInt();
13+
14+
int[] a = new int[N + 1];
15+
for (int i = 1; i <= N; i++) {
16+
int num = 1;
17+
String str = rd.next();
18+
int total = 0;
19+
for (int j = K - 1; j >= 0; j--) { // 이진코드 -> int화
20+
int val = str.charAt(j) - '0';
21+
if (val == 1) {
22+
total += val * num;
23+
}
24+
num *= 2;
25+
}
26+
a[i] = total;
27+
}
28+
29+
int A = rd.nextInt();
30+
int B = rd.nextInt();
31+
Queue<Node> q = new LinkedList<>();
32+
q.add(new Node(a[A], A+""));
33+
boolean visited[] = new boolean[N + 1];
34+
visited[A] = true;
35+
36+
String ans = "-1";
37+
while (!q.isEmpty()) {
38+
Node curr = q.poll();
39+
if (curr.num == a[B]) {
40+
ans = curr.path;
41+
break;
42+
}
43+
44+
for (int i = 1; i <= N; i++) {
45+
if (visited[i] || a[i] == curr.num) continue;
46+
47+
if (Integer.bitCount(curr.num ^ a[i]) == 1) {
48+
visited[i] = true;
49+
q.add(new Node(a[i], curr.path + " " + i));
50+
}
51+
}
52+
}
53+
54+
System.out.println(ans);
55+
}
56+
57+
private static class Node {
58+
int num;
59+
String path;
60+
61+
Node(int num, String path) {
62+
this.num = num;
63+
this.path = path;
64+
}
65+
}
66+
67+
static class FastReader {
68+
BufferedReader br;
69+
StringTokenizer st;
70+
71+
public FastReader() {
72+
br = new BufferedReader(new InputStreamReader(System.in));
73+
}
74+
75+
String next() {
76+
while (st == null || !st.hasMoreElements()) {
77+
try {
78+
st = new StringTokenizer(br.readLine());
79+
} catch (IOException e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
return st.nextToken();
84+
}
85+
86+
int nextInt() {
87+
return Integer.parseInt(next());
88+
}
89+
90+
long nextLong() {
91+
return Long.parseLong(next());
92+
}
93+
94+
double nextDouble() {
95+
return Double.parseDouble(next());
96+
}
97+
98+
String nextLine() {
99+
String str = "";
100+
try {
101+
str = br.readLine();
102+
} catch (IOException e) {
103+
e.printStackTrace();
104+
}
105+
return str;
106+
}
107+
}
108+
}
109+
110+
/* Solution Description
111+
112+
1. 비트마스킹 응용 문제로, 해밍비트가 1인 경우를 코드로 구현할 수 있다면 쉽게 문제를 해결할 수 있다.
113+
114+
2. 더불어, `A->B` 까지의 가장 짧은 해밍 비트 경로를 찾아야하므로, `BFS` 를 적용했다.
115+
116+
3. 임의의 코드에 대해, 동일한 코드가 아니며 아직 방문하지 않은 코드 가운데,
117+
두 코드를 `XOR` 연산을 했을 때, 참인 비트의 수가 `1` 인 경우에 대해 탐색을 이어가는 방식으로 문제를 해결할 수 있다.
118+
119+
*/

0 commit comments

Comments
 (0)