Skip to content

Commit fc26fb1

Browse files
committed
[BOJ] #16987. 계란으로 치기 / 골드5 / 1시간 / 힌트, 성공
1 parent d6b92e3 commit fc26fb1

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
N = int(input()) # 계란의 수
6+
eggs = [list(map(int, input().split())) for _ in range(N)] # 계란의 내구도와 무게를 담은 리스트
7+
8+
max_broken = 0 # 최대 깨진 계란 수를 저장하는 변수
9+
10+
11+
def count_broken_eggs(eggs):
12+
# 깨진 계란의 수 구하기
13+
count = 0
14+
for S, W in eggs:
15+
if S <= 0:
16+
count += 1
17+
return count
18+
19+
20+
def dfs(cur_idx):
21+
global max_broken
22+
# 모든 계란을 한 번씩 든 경우
23+
if cur_idx == N:
24+
# 깨진 계란의 수 세고 max_broken 갱신
25+
max_broken = max(max_broken, count_broken_eggs(eggs))
26+
return
27+
28+
# 현재 계란이 깨졌다면 다음 계란으로 넘어간다.
29+
if eggs[cur_idx][0] <= 0:
30+
dfs(cur_idx + 1)
31+
return
32+
33+
broken = False # 현재 계란으로 다른 계란을 깼는지 여부
34+
for i in range(N):
35+
if i == cur_idx or eggs[i][0] <= 0:
36+
continue
37+
38+
# 계란끼리 치기
39+
eggs[cur_idx][0] -= eggs[i][1]
40+
eggs[i][0] -= eggs[cur_idx][1]
41+
broken = True
42+
43+
# 탐색
44+
dfs(cur_idx + 1) # 다음 계란으로 넘어간다.
45+
46+
# 상태 복구
47+
eggs[cur_idx][0] += eggs[i][1]
48+
eggs[i][0] += eggs[cur_idx][1]
49+
50+
# 칠 수 있는 계란이 없었던 경우, 다음으로 넘어가야 한다.
51+
if not broken:
52+
dfs(cur_idx + 1)
53+
54+
55+
dfs(0)
56+
print(max_broken)

0 commit comments

Comments
 (0)