Skip to content

Commit 2a0069e

Browse files
authored
Merge pull request #1459 from AlgorithmWithGod/Ukj0ng
[20251120] BOJ / G4 / 물통
2 parents a5699e7 + b276a53 commit 2a0069e

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

Ukj0ng/202511/20 BOJ G4 물통.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static Set<String> visited;
9+
private static TreeSet<Integer> set;
10+
private static int A, B, C;
11+
public static void main(String[] args) throws IOException {
12+
init();
13+
BFS();
14+
15+
for (Integer e : set) {
16+
bw.write(e + " ");
17+
}
18+
19+
bw.flush();
20+
bw.close();
21+
br.close();
22+
}
23+
24+
private static void init() throws IOException {
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
A = Integer.parseInt(st.nextToken());
27+
B = Integer.parseInt(st.nextToken());
28+
C = Integer.parseInt(st.nextToken());
29+
30+
visited = new HashSet<>();
31+
set = new TreeSet<>();
32+
33+
34+
}
35+
36+
private static void BFS() {
37+
Queue<int[]> q = new ArrayDeque<>();
38+
visited.add(0 + "," + 0 + "," + C);
39+
q.add(new int[]{0, 0, C});
40+
41+
while (!q.isEmpty()) {
42+
int[] current = q.poll();
43+
44+
if (current[0] == 0) {
45+
set.add(current[2]);
46+
}
47+
48+
for (int i = 0; i < 3; i++) {
49+
if (i == 0 && current[0] > 0) {
50+
int nB = Math.min(B, current[0] + current[1]);
51+
int nA = current[0] - (nB - current[1]);
52+
53+
if (!visited.contains(nA + "," + nB + "," + current[2])) {
54+
visited.add(nA + "," + nB + "," + current[2]);
55+
q.add(new int[]{nA, nB, current[2]});
56+
}
57+
58+
int nC = Math.min(C, current[0] + current[2]);
59+
nA = current[0] - (nC - current[2]);
60+
if (!visited.contains(nA + "," + current[1] + "," + nC)) {
61+
visited.add(nA + "," + current[1] + "," + nC);
62+
q.add(new int[]{nA, current[1], nC});
63+
}
64+
} else if (i == 1 && current[1] > 0) {
65+
int nA = Math.min(A, current[0] + current[1]);
66+
int nB = current[1] - (nA - current[0]);
67+
68+
if (!visited.contains(nA + "," + nB + "," + current[2])) {
69+
visited.add(nA + "," + nB + "," + current[2]);
70+
q.add(new int[]{nA, nB, current[2]});
71+
}
72+
73+
int nC = Math.min(C, current[1] + current[2]);
74+
nB = current[1] - (nC - current[2]);
75+
if (!visited.contains(current[0] + "," + nB + "," + nC)) {
76+
visited.add(current[0] + "," + nB + "," + nC);
77+
q.add(new int[]{current[0], nB, nC});
78+
}
79+
} else if (i == 2 && current[2] > 0) {
80+
int nA = Math.min(A, current[0] + current[2]);
81+
int nC = current[2] - (nA - current[0]);
82+
83+
if (!visited.contains(nA + "," + current[1] + "," + nC)) {
84+
visited.add(nA + "," + current[1] + "," + nC);
85+
q.add(new int[]{nA, current[1], nC});
86+
}
87+
88+
int nB = Math.min(B, current[1] + current[2]);
89+
nC = current[2] - (nB - current[1]);
90+
if (!visited.contains(current[0] + "," + nB + "," + nC)) {
91+
visited.add(current[0] + "," + nB + "," + nC);
92+
q.add(new int[]{current[0], nB, nC});
93+
}
94+
}
95+
}
96+
}
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)