728x90
https://www.acmicpc.net/problem/2251
// 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.util.*; import java.io.*; public class Main { // main public static void main(String[] args) throws Exception{ // Please Enter Your Code Here BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); // 물통의 최대 수용치 int A = Integer.parseInt(st.nextToken()); int B = Integer.parseInt(st.nextToken()); int C = Integer.parseInt(st.nextToken()); List<Integer> ans = new ArrayList<>(); Deque<int[]> deq = new ArrayDeque<>(); boolean[][][] visited = new boolean[A + 1][B + 1][C + 1]; deq.offer( new int[] { 0, 0, C } ); while ( !deq.isEmpty() ) { int[] temp = deq.poll(); int a = temp[0], b = temp[1], c = temp[2]; // 필터링 if( visited[a][b][c] ) continue; // 방문처리 visited[a][b][c] = true; // a가 0인 경우, c를 정답 리스트에 넣어준다. if ( a == 0 ) ans.add(c); // a -> b if ( a + b > B ) deq.offer( new int[] { a + b - B, B, c } ); else deq.offer( new int[] { 0, a + b, c } ); // a -> c if ( a + c > C ) deq.offer( new int[] { a + c - C, b, C } ); else deq.offer( new int[] { 0, b, a + c } ); // b -> a if ( b + a > A ) deq.offer( new int[] { A, b + a - A, c } ); else deq.offer( new int[] { b + a, 0, c } ); // b -> c if ( b + c > C ) deq.offer( new int[] { a, b + c - C, C } ); else deq.offer( new int[] { a, 0, b + c } ); // c -> a if ( c + a > A ) deq.offer( new int[] { A, b, c + a - A } ); else deq.offer( new int[] { c + a, b, 0 } ); // c -> b if ( c + b > B) deq.offer( new int[] { a, B, c + b - B } ); else deq.offer( new int[] { a, c + b, 0 } ); } StringBuilder sb = new StringBuilder(); Collections.sort(ans); for( int i=0; i<ans.size(); ++i) sb.append(ans.get(i) + " "); bw.write(sb.toString()); bw.flush(); bw.close(); br.close(); } } | cs |
반응형
'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글
1, 2, 3 더하기 4 (0) | 2021.11.19 |
---|---|
팰린드롬? (0) | 2021.11.18 |
퍼즐 (0) | 2021.11.17 |
부등호 (0) | 2021.11.08 |
수 이어 쓰기 1 (0) | 2021.11.08 |