Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- github
- Algorithm
- 재귀
- String
- w3school
- W3Schools
- 시작해요 언리얼 2022
- 오류
- 프로그래밍
- Class
- dynamic
- c++
- Programming
- Unity
- 파이썬
- python
- Tutorial
- 백준
- Basic
- 문제풀이
- parameter
- Material
- 기초
- dfs
- Unreal Engine 5
- UE5
- C#
- guide
- loop
- DP
Archives
- Today
- Total
행복한 개구리
백준 22.01.30. 14888번 - 연산자 끼워넣기 본문
N = int(input())
ops = []
nums = []
result = []
nums = list(map(int, input().split()))
ops = list(map(int, input().split()))
m = -1e9
n = 1e9
def DFS(num, depth, add, sub, mul, div):
if depth == N:
if num not in result:
result.append(num)
return
if add > 0:
DFS(num + nums[depth], depth + 1, add - 1, sub, mul, div)
if sub > 0:
DFS(num - nums[depth], depth + 1, add, sub - 1, mul, div)
if mul > 0:
DFS(num * nums[depth], depth + 1, add, sub, mul - 1, div)
if div > 0:
DFS(int(num / nums[depth]), depth + 1,
add, sub, mul, div - 1)
DFS(nums[0], 1, ops[0], ops[1], ops[2], ops[3])
print(max(result))
print(min(result))
- 재귀를 사용하는데, 가장먼저 덧셈/뺄셈/곱셈/나눗셈이 시작할지 결정하기위해서 elif가 아닌 if문을 네가지로 적어서 네갈래로 나눠 탐색합니다.
- 모든 경우의 수를 검색하고 끝까지 검색했을 때 result리스트에 num이 없다면 추가해주고 있다면 return합니다.
- 마지막으로 result리스트에 저장된 값 중에서 max와 min을 출력합니다.
'Algorithm > BaekJoon' 카테고리의 다른 글
백준 22.02.01. 1003번 - 피보나치 함수 (0) | 2022.02.01 |
---|---|
백준 22.01.31. 14889번 - 스타트와 링크 (0) | 2022.01.31 |
백준 22.01.30. 2580번 - 스도쿠 (0) | 2022.01.30 |
백준 22.01.28. 9663번 - N-Queen (0) | 2022.01.28 |
백준 22.01.28. 15652번 - N과 M (4) (0) | 2022.01.28 |