728x90
반응형
https://www.acmicpc.net/problem/2621
2621번: 카드게임
근우는 오늘 재미있는 카드 게임을 배우고 있다. 카드는 빨간색, 파란색, 노란색, 녹색의 네 가지 색이 있고, 색깔별로 1부터 9까지 숫자가 쓰여진 카드가 9장씩 있다. 카드는 모두 36(=4x9)장이다.
www.acmicpc.net
문제 해결
- 전형적인 구현 문제
- 메모르도 가장 적게 쓰면서 시간복잡도를 줄이고 싶었다.
- 하지만 생각보다 많은 부분을 정의하고 조건 나온대로 푸는 것이 가장 깔끔하고 잘 풀렸다.
- 카드 모양별 개수, 카드 숫자별 갯수, 카드 숫자대로 배열 등 여러가지 정의하고 들어가야 편하다.
CODE
from collections import defaultdict
# R, B, Y, G
nums = defaultdict(int)
cnt_nums = []
colors = {'R':0, 'B':0, 'Y':0, 'G':0}
for _ in range(5):
color, num = input().split()
cnt_nums.append(int(num))
colors[color] += 1
nums[int(num)] += 1
sort_nums = cnt_nums.copy()
sort_nums.sort()
if max(colors.values())==5:
if sort_nums[0] +1 == sort_nums[1] and sort_nums[1]+1 == sort_nums[2] and sort_nums[2]+1 == sort_nums[3] and sort_nums[3]+ 1 == sort_nums[4]:
print(900+sort_nums[-1])
exit()
else:
print(sort_nums[-1]+600)
exit()
elif max(nums.values()) == 4:
A = [k for k, v in nums.items() if v==4]
print(A[0]+800)
exit()
elif max(nums.values())==3 and 2 in nums.values():
A = [k for k, v in nums.items() if v==3]
B = [k for k, v in nums.items() if v==2]
print(A[0]*10+B[0] + 700)
exit()
elif sort_nums[0] +1 == sort_nums[1] and sort_nums[1]+1 == sort_nums[2] and sort_nums[2]+1 == sort_nums[3] and sort_nums[3]+1 == sort_nums[4]:
print(sort_nums[-1]+500)
exit()
elif max(nums.values()) == 3:
A = [k for k, v in nums.items() if v==3]
print(A[0]+400)
elif max(nums.values()) == 2:
A = sorted([k for k, v in nums.items() if v==2])
if len(A) ==2:
print(300+A[1]*10+A[0])
else:
print(200+A[0])
else:
print(sort_nums[-1] + 100)
728x90
반응형
'알고리즘 > [python] 백준 BOJ' 카테고리의 다른 글
[python] 백준 3649 로봇 프로젝트 (1) | 2023.04.22 |
---|---|
[python] 백준 2589 보물섬 (1) | 2023.04.22 |
[python] 백준 1799 비숍 (0) | 2023.04.20 |
[python] 백준 16987 계란으로 계란치기 (0) | 2023.04.18 |
[python] 백준 1941 소문난 칠공주 (0) | 2023.04.18 |
댓글