[BOJ 14888] ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ
1. ๋ฌธ์ : Link
์ซ์์ ์ฐ์ฐ์๊ฐ ์ฃผ์์ก์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํด์ max๊ฐ๊ณผ min๊ฐ์ return
2. ํ์ด
dfs์ ๋ฐฑํธ๋ํน์ ์ด์ฉํด์ ํธ๋ ๋ฌธ์ ์๋ค.
3. ์ฝ๋
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static int N;
static int[] num;
static int maxVal = -Integer.MAX_VALUE;
static int minVal = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(bf.readLine());
StringTokenizer stk = new StringTokenizer(bf.readLine(), " ");
num = new int[N];
int[] calc = new int[4];
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(stk.nextToken());
}
stk = new StringTokenizer(bf.readLine(), " ");
for(int i=0; i<4; i++){
calc[i] = Integer.parseInt(stk.nextToken());
}
calculator(1, num[0], calc);
System.out.println(maxVal);
System.out.println(minVal);
}
public static void calculator(int k, int total, int[] arr){
if(k == N){
maxVal = Math.max(maxVal, total);
minVal = Math.min(minVal, total);
return;
}
// +
if(arr[0] > 0){
arr[0] -= 1;
calculator(k+1, total + num[k], arr);
arr[0] += 1;
}
// -
if(arr[1] > 0){
arr[1] -= 1;
calculator(k+1, total - num[k], arr);
arr[1] += 1;
}
// *
if(arr[2] > 0){
arr[2] -= 1;
calculator(k+1, total * num[k], arr);
arr[2] += 1;
}
// /
if(arr[3] > 0){
arr[3] -= 1;
calculator(k+1, total / num[k], arr);
arr[3] += 1;
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 1303] ์ ์-์ ํฌ (0) | 2022.07.04 |
---|---|
[BOJ 1260] DFS์ BFS (0) | 2022.07.01 |
[BOJ 17070] ํ์ดํ ์ฎ๊ธฐ๊ธฐ1 (java) (0) | 2022.07.01 |
[BOJ 2667] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2022.06.29 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๊ฒ ๋๋ฒ (0) | 2022.04.08 |
์ต๊ทผ๋๊ธ