There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus or . The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.
For each game, you will get an array of clouds numbered if they are safe or if they must be avoided.
Example
c = [0,1,0,0,0,1,0]
Index the array from 0~6. The number on each cloud is its index in the list so the player must avoid the clouds at indices 1 and 5. They could follow these two paths: 0-2-4-6 or 0-2-3-4-6. The first path takes 3 jumps while the second takes 4. Return 3.
Explanation
추가 설명
밟으면 되는 구름이 0, 안되는 구름이 1로 표시되어 있는 구름 리스트가 있다.
한번에 1칸 혹은 2칸을 점프할 수 있다고 했을 때, 밟으면 되는 구름만 밟아서 가는 경로 중 점프 수가 가장 적은 경로의 점프 수를 구하라.
2칸 점프가 가능한지 아닌지를 나타내는 플래그를 사용, 가장 적은 점프수를 구하도록 알고리즘을 구현했다.
#!/bin/python3
import math
import os
import random
import re
import sys
def jumpingOnClouds(c):
double_jump = 0
jump = 0
for cloud in c[1:]:
if cloud == 0 and double_jump == 0:
jump += 1
double_jump = 1
elif cloud == 0 and double_jump == 1:
double_jump = 0
elif cloud == 1:
jump += 1
double_jump = 1
else:
pass
return jump
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
c = list(map(int, input().rstrip().split()))
result = jumpingOnClouds(c)
fptr.write(str(result) + '\n')
fptr.close()