結果

問題 No.3 ビットすごろく
ユーザー kashiwagi513
提出日時 2019-02-21 20:26:47
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,551 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 25,472 KB
最終ジャッジ日時 2024-11-20 20:43:14
合計ジャッジ時間 137,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13 TLE * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

import sys
import os

class Option:
    val = []
    def isNone(v):
        return isinstance(v, _None)
    def unwrap(v):
        return v.val[0]

class Some(Option):
    def __init__(self, v):
        self.val = [v]
    def __str__(self):
        return "Some(" + str(self.val[0]) + ")"

class _None(Option):
    def __init__(self):
        self.val = []
    def __str__(self):
        return "_None"

def lfind(pred, ls):
    res = list(filter(pred, ls))
    if len(res) > 0:
        return Some(res[0])
    else:
        return _None()

def lookup(key, ls):
    res = lfind(lambda l: l[0] == key, ls)
    if Option.isNone(res):
        return _None()
    else:
        return Some(Option.unwrap(res)[1])

def rangeOfCell(n, acc):
    if n==0:
        return acc
    else:
        return rangeOfCell(n//2, acc+(n%2))

def updateCell(steps, n):
    r = rangeOfCell(n, 0)
    prev_ = filter(lambda v: v != -1, lookup(n-r, steps).val)
    next_ = filter(lambda v: v != -1, lookup(n+r, steps).val)
    both = list(prev_) + list(next_)
    if len(both) > 0:
        return min(both)+1
    else:
        return -1

def updateSteps(steps):
    steps_ = list(map(lambda s: (s[0], updateCell(steps, s[0])) if s[1] == -1 else s, steps))
    if steps == steps_:
        return steps_
    else:
        return updateSteps(steps_)

def solve(inputs):
    board = range(1,inputs)
    steps = list(map(lambda b: (b, -1), board)) + [(inputs, 1)]
    steps_ = updateSteps(steps)
    print(steps_[0][1])

inputs = int(input())
solve(inputs)
0