BOJ::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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>
using namespace std;
 
int n, max_res, min_res;
int a[11];
int oper[4];
 
void dfs(int idx,int M) {
    if (idx == n) {
        max_res = max(max_res, M);
        min_res = min(min_res, M);
    }
    if (oper[0!= 0) {
        oper[0]--;
        dfs(idx + 1, M + a[idx]);
        oper[0]++;
    }
    if (oper[1!= 0) {
        oper[1]--;
        dfs(idx + 1, M - a[idx]);
        oper[1]++;
    }
    if (oper[2!= 0) {
        oper[2]--;
        dfs(idx + 1, M * a[idx]);
        oper[2]++;
    }
    if (oper[3!= 0) {
        oper[3]--;
        dfs(idx + 1, M / a[idx]);
        oper[3]++;
    }
}
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
 
    for (int i = 0; i < 4; i++) {
        cin >> oper[i];
    }
    max_res = -1e9;
    min_res = 1e9;
 
    dfs(1, a[0]);
 
    cout << max_res << "\n" << min_res << endl;
}
cs

 

'BOJ::문제풀이' 카테고리의 다른 글

2302 극장좌석  (0) 2018.01.27
1941 소문난칠공주  (0) 2018.01.26
1958 LCS3  (0) 2018.01.14
9252 LCS 2  (1) 2018.01.14
9251 LCS  (0) 2018.01.14

+ Recent posts