문제) 백준 - 백트래킹 - 연산자 끼워넣기(2)
https://www.acmicpc.net/problem/15658
15658번: 연산자 끼워넣기 (2)
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1보다 크거나 같고, 4N보다 작거나 같은 4개의 정수가 주어지는데, 차례대
www.acmicpc.net
처음에 permutations를 활용하기 위해 파이썬으로 시도했다가 TLE로 틀려, C++을 통한 백트래킹으로 해결했습니다. 부분합, 인덱스, 4가지 연산자의 개수를 매개변수로 백트래킹을 적용합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfs(int total,int idx, int pp, int mi, int mul, int div) { | |
if (idx == N) { | |
res_min = min(res_min, total); | |
res_max = max(res_max, total); | |
return; | |
} | |
if (pp < oper[0]) | |
dfs(total + arr[idx], idx + 1, pp + 1, mi, mul, div); | |
if (mi < oper[1]) | |
dfs(total - arr[idx ], idx + 1, pp , mi + 1, mul, div); | |
if (mul < oper[2]) | |
dfs(total * arr[idx], idx + 1, pp , mi, mul + 1, div); | |
if (div < oper[3]) | |
dfs(total / arr[idx], idx + 1, pp, mi, mul, div + 1); | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 20365번 - 블로그2 (C++) 문제 및 풀이 (0) | 2022.01.14 |
---|---|
[백준] 16432번 - 떡장수와 호랑이 (C++) 문제 및 풀이 (0) | 2022.01.13 |
[백준] 10798번 - 세로읽기 (C++) 문제 및 풀이 (0) | 2022.01.13 |
[백준] 7662번 - 이중 우선순위 큐 (C++) 문제 및 풀이 (0) | 2022.01.12 |
[백준] 20208번 - 진우의 민트초코우유 (C++) 문제 및 풀이 (0) | 2022.01.12 |
댓글