[백준] 14888번 연산자 (python, dfs)
728x90
반응형
14888번: 연산자 끼워넣기
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, ��
www.acmicpc.net
n=int(input())
a=list(map(int,input().split()))
oper=list(map(int,input().split()))
result=[]
def dfs(cnt,p,minus,mul,d,now):
if cnt==n-1:
result.append(now)
return
if p<oper[0]:
dfs(cnt+1,p+1,minus,mul,d,now+a[cnt+1])
if minus<oper[1]:
dfs(cnt+1,p,minus+1,mul,d,now-a[cnt+1])
if mul<oper[2]:
dfs(cnt+1,p,minus,mul+1,d,now*a[cnt+1])
if d<oper[3]:
if now<0:
temp=(-now)//a[cnt+1]
now=-temp
else: now=now//a[cnt+1]
dfs(cnt+1,p,minus,mul,d+1,now)
dfs(0,0,0,0,0,a[0])
print(max(result))
print(min(result))
728x90
반응형
'백준' 카테고리의 다른 글
[백준] 15686번 치킨배달 (시뮬, bfs, 파이썬, java) (0) | 2020.10.17 |
---|---|
[백준] 14503번 로봇 청소기 (bfs, 시뮬레이션, python) (0) | 2020.10.17 |
[백준] 16930번 달리기 (python, bfs) (0) | 2020.10.16 |
[백준] 17086번 아기 상어 2 (bfs, python) (0) | 2020.10.16 |
[백준] 14226번 이모티콘 (bfs, 파이썬) (0) | 2020.10.16 |
TAGS.