티스토리 뷰
모든 경우의 수를 모두 탐색하는 문제입니다.
DFS 탐색을 사용하였습니다.
[소스코드]
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 75 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static int N, minVal = Integer.MAX_VALUE, maxVal = Integer.MIN_VALUE; public static int[] temp, arr, expression; public static int[] dirX = new int[] { 0, 0, -1, 1 }; public static int[] dirY = new int[] { 1, -1, 0, 0 }; public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { StringTokenizer st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); arr = new int[N]; temp = new int[N - 1]; expression = new int[4]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for (int i = 0; i < 4; i++) expression[i] = Integer.parseInt(st.nextToken()); dfs(1, arr[0]); System.out.println(maxVal); System.out.println(minVal); } public static void dfs(int depth, int totalVal) { if (depth > N - 1) { minVal = Math.min(minVal, totalVal); maxVal = Math.max(maxVal, totalVal); return; } for (int i = 0; i < 4; i++) { if (expression[i] > 0) { expression[i] -= 1; switch (i) { case 0: dfs(depth + 1, totalVal + arr[depth]); break; case 1: dfs(depth + 1, totalVal - arr[depth]); break; case 2: dfs(depth + 1, totalVal * arr[depth]); break; case 3: dfs(depth + 1, totalVal / arr[depth]); break; } expression[i] += 1; } } } } | cs |
'알고리즘 문제 > 백준(BOJ)' 카테고리의 다른 글
[백준 3190번] 뱀 :: 늦깎이 IT (0) | 2019.03.18 |
---|---|
[백준 13460번] 구슬 탈출2 :: 늦깎이 IT (0) | 2019.03.12 |
[백준 14889번] 스타트와 링크 :: 늦깎이 IT (0) | 2019.03.09 |
[백준 15686번] 치킨 배달 :: 늦깎이 IT (0) | 2019.03.09 |
[백준 14499번] 주사위 굴리기 :: 늦깎이 IT (0) | 2019.03.08 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- DFS
- 구슬 탈출2
- 알고리즘
- 중간값
- 리스트
- 최소힙
- 시뮬레이션
- 힙
- BFS
- 구현
- 큐
- SWEA
- 연산자 끼워넣기
- 브루트포스
- 최대힙
- 자바
- 14888
- 삼성
- 배열
- 백준
- 영역 구하기
- 나무 재테크
- 트리
- 탈주범 검거
- 탐색
- 알고스팟
- 정렬
- 우선순위 큐
- 힙정렬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함