일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- SW ExpertAcademy
- 감소하는 수
- 12869번
- 키패드 누르기
- 거울 설치
- 16234번
- QueryDSL 기초
- 2020 카카오 인턴십
- 스타트 택시
- 1038번
- 보석 쇼핑
- 15686번
- 프로그래머스
- 17144번
- 19238번
- 1789번
- 빛의 경로 사이클
- 어른 상어
- 12865번
- 미세먼지 안녕!
- 베스트엘범
- 9095번
- SW Expert Academy
- 파이썬
- 14499번
- 경주로 건설
- HTML 기초
- 수식 최대화
- python
- 백준 알고리즘
Archives
- Today
- Total
보물창고 블로그
백준 알고리즘 14888번 연산자 끼워넣기 풀이 with Python 본문
728x90
문제 링크: https://www.acmicpc.net/problem/14888
14888번: 연산자 끼워넣기
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 곱셈(×)의 개수, 나눗셈(÷)의 개수이다.
www.acmicpc.net
문제는 아래와 같습니다.
이 문제는 최솟값과 최댓값을 다 확인해야 하므로 완전탐색을 해야합니다. 저는 너비우선탐색(bfs)를 이용하여 문제를 해결하였습니다. 풀이는 다음과 같습니다.
from collections import deque
from copy import deepcopy
def solution(n, numbers, tools):
maxi = -float('inf')
mini = float('inf')
queue = deque()
# 더하기 빼기 곱하기 나누기
queue.append([0, numbers[0], tools])
while queue:
count, sub, tool = queue.popleft()
if count == n - 1:
if maxi < sub:
maxi = sub
if mini > sub:
mini = sub
else:
if tool[0] > 0:
tool2 = deepcopy(tool)
tool2[0] -= 1
queue.append([count + 1, sub + numbers[count + 1], tool2])
if tool[1] > 0:
tool2 = deepcopy(tool)
tool2[1] -= 1
queue.append([count + 1, sub - numbers[count + 1], tool2])
if tool[2] > 0:
tool2 = deepcopy(tool)
tool2[2] -= 1
queue.append([count + 1, sub * numbers[count + 1], tool2])
if tool[3] > 0:
tool2 = deepcopy(tool)
tool2[3] -= 1
if (sub > 0 and numbers[count + 1] < 0) or (sub < 0 and numbers[count + 1] > 0):
sub2 = -(abs(sub) // abs(numbers[count + 1]))
else:
sub2 = sub // numbers[count + 1]
queue.append([count + 1, sub2, tool2])
print(maxi)
print(mini)
n = int(input())
number3 = list(map(int, input().split()))
tool3= list(map(int, input().split()))
solution(n, number3, tool3)
'알고리즘 풀이 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 13458번 시험 감독 풀이 With Python (0) | 2020.03.09 |
---|---|
백준 알고리즘 3190번 뱀 풀이 With Python (0) | 2020.03.09 |
백준 알고리즘 14500번 테트로미노 풀이 with Python (0) | 2020.02.25 |
백준 알고리즘 3055번 탈출 풀이 with Python (0) | 2020.02.23 |
백준 알고리즘 15685번 드래곤 커브 풀이 with Python (0) | 2020.02.21 |
Comments