Skip to content

Commit 989d0ff

Browse files
committed
[PGS] 양과 늑대 / Level 3 / 60분 / 성공
1 parent bf9218d commit 989d0ff

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
양 모은 수보다 늑대의 수가 같거나 많아지면 종료됨
3+
부모 노드는 방문했지만 자식 노드는 방문하지 않았을 때 방문할 수 있음
4+
5+
'''
6+
7+
8+
def solution(info, edges):
9+
# info - 0: 양, 1: 늑대
10+
global answer
11+
answer = 0
12+
visited = [False] * len(info)
13+
visited[0] = True # 0번 노드는 항상 양이 있음
14+
15+
def dfs(sheep, wolf):
16+
global answer
17+
18+
if wolf < sheep: # 양이 늑대보다 더 많으면 모을 수 있는 최대 양 마리 수 업데이트
19+
answer = max(answer, sheep)
20+
else: # 늑대가 양의 수와 같거나 더 많을 경우 종료됨
21+
return
22+
23+
for parent, child in edges:
24+
# 부모 노드는 방문했지만 자식 노드는 방문하지 않았을 때
25+
if visited[parent] and not visited[child]:
26+
visited[child] = True
27+
if info[child] == 0: # 양이라면 양 마리 수 + 1
28+
dfs(sheep + 1, wolf)
29+
else: # 늑대라면 늑대 마리 수 + 1
30+
dfs(sheep, wolf + 1)
31+
visited[child] = False
32+
33+
dfs(1, 0) # (sheep, wolf)
34+
35+
return answer

0 commit comments

Comments
 (0)