Skip to content

Commit 453264b

Browse files
authored
Merge pull request #398 from AlgorithmWithGod/suyeun84
[20250629] BOJ / G4 / 물통 / 김수연
2 parents 5e5d749 + 6a9fafd commit 453264b

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

suyeun84/202506/29 BOJ G4 물통

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj2251 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
static int A, B, C;
12+
static Set<Integer> answer;
13+
static boolean[][][] visited;
14+
static int[] capacity;
15+
public static void main(String[] args) throws Exception {
16+
nextLine();
17+
A = nextInt();
18+
B = nextInt();
19+
C = nextInt();
20+
answer = new TreeSet<>();
21+
capacity = new int[]{A, B, C};
22+
visited = new boolean[A + 1][B + 1][C + 1];
23+
24+
dfs(0, 0, C);
25+
26+
for (int a : answer) System.out.print(a + " ");
27+
}
28+
static void dfs(int a, int b, int c) {
29+
if (visited[a][b][c]) return;
30+
31+
visited[a][b][c] = true;
32+
33+
if (a == 0) answer.add(c);
34+
35+
for (int i = 0; i < 3; i++) {
36+
for (int j = 0; j < 3; j++) {
37+
if (i != j) {
38+
int[] next = pour(new int[]{a, b, c}, i, j);
39+
dfs(next[0], next[1], next[2]);
40+
}
41+
}
42+
}
43+
}
44+
static int[] pour(int[] state, int from, int to) {
45+
int[] next = state.clone();
46+
int amount = Math.min(state[from], capacity[to] - state[to]);
47+
next[from] -= amount;
48+
next[to] += amount;
49+
return next;
50+
}
51+
}
52+
```

0 commit comments

Comments
 (0)