LoginSignup
0
0

More than 3 years have passed since last update.

【python】雲を渡っていくゲームの最短ステップ数を求めるプログラム

Posted at

【python】雲を渡っていくゲームの最短ステップ数を求めるプログラム

▼設問

  • 0または1が複数入ったlistが与えられる。
  • 0は普通の雲。1は雷雲。
  • インデックス番号が雲の番号になる。
  • 雲を辿って最後のインデックス番号にたどり着けばゴール。
  • 雲をたどるときにジャンプできるのは+1か+2。
  • 雷雲は避ける。
  • ゴールまでに必要な最短のジャンプ回数を求める(必ずゴールできる)

image.png

URL

▼sample input

6
0 0 0 0 1 0

▼sample output

3

▼my answer

def jumpingOnClouds(c):
    i= ans = 0
    while i <= len(c)-2:
        #最終地点の1個前の場合
        if i+2 == len(c):
            ans += 1
            return ans

        #2個とばし可能か?
        elif c[i+2] != 1:
            i += 2
        else:
            i += 1
        ans += 1
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    c = list(map(int, input().rstrip().split()))
    result = jumpingOnClouds(c)
    fptr.write(str(result) + '\n')
    fptr.close()



・returnのあとに代入する式はかけない
☓ return ans += 1
◯ return ans

・len(c)
要素の数を求める。※1以上
c[len(c)]は存在しない。

・「!」
!が前にくる
☓ a =! 3
◯ a != 3

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0