예제 #1
0
파일: 280.py 프로젝트: mebubo/education
def f(st=-8, top=(0,0,0,0,0), bot=(1,1,1,1,1), carry=False, rec=0):
    r = 0
    rec += 1
    if carry:
        excl = tuple(1-array(top))
        bot_new = list(bot)
        bot_new[st] = 0
        bot_new = tuple(bot_new)
        for i, e in enumerate(excl):
            if e==1:
                top_new = list(top)
                top_new[i] = 1
                top_new = tuple(top_new)
                p, s = ex(st, i, excl)
                r += s + p * (f(i, top_new, bot_new, False, rec))
    else:
        excl = bot
        for i, e in enumerate(excl):
            if e==1:
                p, s = ex(st, i, excl)
                r += s + p * (f(i, top, bot, True, rec))
    #print "-"*rec + ">", st, top, bot, carry, r
    return r

if __name__=="__main__":
    ret = eu_format(f())
    print ret
    assert ret=="430.088247"

예제 #2
0
파일: 213.py 프로젝트: mebubo/education
def _filter(choices, rows, cols):
    return [(i, j) for (i, j) in choices if i not in (-1, rows) and j not in (-1, cols)]

def apply_mul(func, arg, mul):
    for k in range(mul):
        arg = func(arg)
    return arg

def connectivity(n):
    dim = n*n
    M = zeros((dim,dim))
    for i in range(n):
        for j in range(n):
            choices = ((i-1, j), (i+1, j), (i, j-1), (i,j+1))
            choices = _filter(choices, n, n)
            prob = 1.0/len(choices)
            for c in choices:
                M[c[0]*n+c[1], i*n+j] += prob
    return M

def e213(n=30, p=50):
    c = connectivity(n)
    r = apply_mul(lambda x: dot(c, x), c, p-1)
    o = ones((n**2, n**2))
    m = o-r
    return sum([prod(r) for r in m])

if __name__=="__main__":
    print eu_format(e213())