- Notifications
You must be signed in to change notification settings - Fork 845
/
Copy path8.py
27 lines (26 loc) · 1.3 KB
/
8.py
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
fromitertoolsimportpermutations
defsolution(n, weak, dist):
# 길이를 2배로 늘려서 '원형'을 일자 형태로 변형
length=len(weak)
foriinrange(length):
weak.append(weak[i] +n)
answer=len(dist) +1# 투입할 친구 수의 최솟값을 찾아야 하므로 len(dist) + 1로 초기화
# 0부터 length - 1까지의 위치를 각각 시작점으로 설정
forstartinrange(length):
# 친구를 나열하는 모든 경우 각각에 대하여 확인
forfriendsinlist(permutations(dist, len(dist))):
count=1# 투입할 친구의 수
# 해당 친구가 점검할 수 있는 마지막 위치
position=weak[start] +friends[count-1]
# 시작점부터 모든 취약한 지점을 확인
forindexinrange(start, start+length):
# 점검할 수 있는 위치를 벗어나는 경우
ifposition<weak[index]:
count+=1# 새로운 친구를 투입
ifcount>len(dist): # 더 투입이 불가능하다면 종료
break
position=weak[index] +friends[count-1]
answer=min(answer, count) # 최솟값 계산
ifanswer>len(dist):
return-1
returnanswer