Algorithm/백준

[BOJ] - 14888. 연산자 끼워넣기

benguin 2019. 4. 9. 17:20

[URL]

https://www.acmicpc.net/problem/14888

 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 곱셈(×)의 개수, 나눗셈(÷)의 개수이다. 

www.acmicpc.net

 

[풀이 과정]

* 재귀 DFS

 

1. 재귀 DFS를 활용하여 가능한 연산자 조합을 구한다.

ex) 

[+] [-] [*] [%]

[2] [0] [1] [0] 일때 

 

(1) + + *    (2) + * +    (3) * + + 

   3가지 조합 가능

 

2. 각각의 조합에 따라 연산 실행

3. 후보자 배열에 넣어 오름차순 정렬

4. 최소값, 최대값 구한다.

 

 

[소스 코드]

 

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <algorithm>
using namespace std;
int N;
int numArr[11];
int operArr[10= { -1, };
int oper[4];
void init();
int calc();
void dfs(int cnt);
int cand[987654];
int candCnt = 0;
int main() {
    init();
    dfs(0);
    int Min, Max;
    sort(cand, cand + candCnt);
    Min = cand[0];
    Max = cand[candCnt - 1];
    printf("%d\n%d\n", Max, Min);
    return 0;
}
void init()
{
    scanf("%d"&N);
    for (int i = 0; i < N; i++)
    {
        scanf("%d"&numArr[i]);
    }
    for (int i = 0; i < 4; i++)
    {
        scanf("%d"&oper[i]);
    }
}
int calc()
{
    int res = 0;
    for (int i = 0; i < N - 1; i++)
    {
        int temp = 0;
        if (i == 0)
        {
            if (operArr[i] == 0)        //덧셈
            {
                temp = numArr[i] + numArr[i + 1];
            }
            if (operArr[i] == 1)        //뺄셈
            {
                temp = numArr[i] - numArr[i + 1];
            }
            if (operArr[i] == 2)        //곱셈
            {
                temp = numArr[i] * numArr[i + 1];
            }
            if (operArr[i] == 3)        //나눗셈
            {
                temp = numArr[i] / numArr[i + 1];
            }
            res += temp;
        }
        else
        {
            if (operArr[i] == 0)        //덧셈
            {
                res = res + numArr[i + 1];
            }
            if (operArr[i] == 1)        //뺄셈
            {
                res = res - numArr[i + 1];
            }
            if (operArr[i] == 2)        //곱셈
            {
                res = res * numArr[i + 1];
            }
            if (operArr[i] == 3)        //나눗셈
            {
                res = res / numArr[i + 1];
            }
        }
    }
    return res;
}
void dfs(int cnt)
{
    if (cnt == N - 1)
    {
        cand[candCnt] = calc(); 
        candCnt++;
        return;
    }
    for (int i = 0; i < 4; i++)
    {
        if (oper[i] > 0)
        {
            oper[i]--;
            operArr[cnt] = i;
            cnt++;
            dfs(cnt);
            cnt--;
            operArr[cnt] = -1;
            oper[i]++;
        }
    }
}
cs

'Algorithm > 백준' 카테고리의 다른 글

[BOJ] 14502. 연구소  (0) 2019.04.10
[BOJ] - 2667. 단지번호붙이기  (0) 2019.04.09
[BOJ] - 14889. 스타트와 링크  (0) 2019.04.09
[BOJ] - 17136. 색종이 붙이기  (0) 2019.04.09
[BOJ] - 16235. 나무 재테크  (0) 2019.04.08