文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>瓷砖覆盖问题——from《编程之美》

瓷砖覆盖问题——from《编程之美》

时间:2008-10-08  来源:rockins

    """
    # defensive precaution
    if ((N * M) % 2 != 0):
        return (0)
    
    # boundary
    if ((N <= 1 and M < 2) or
        (N < 2 and M <= 1) or
        (N <= 0) or (M <= 0)):
        return (0)
    
    if ((N == 1 and M == 2) or
        (N == 2 and M == 1)):
        return (1)
    
    # recursion
    retval = (
            covManners(N, M-2) +
            covManners(N-1, M) -
            covManners(N-1, M-2)
            ) + (
            covManners(N, M-1) +
            covManners(N-2, M) -
            covManners(N-2, M-1)
            )
    
    return (retval)

def covMannersGeneral(N, M, p, q):
    """NxM floor, PxQ brick
    """
    # defensive precaution
    if (p <= 0 or q <= 0):
        return (0)
    if ((N * M) % (p * q) != 0):
        return (0)
    
    # boundary
    if ((N <= p and M < q) or
        (N < q and M <= p) or
        (N <= 0) or (M <= 0)):
        return (0)
    
    if ((N == p and M == q) or
        (N == q and M == p)):
        return (1)
    
    # recursion
    retval = (
            covMannersGeneral(N, M-q, p, q) +
            covMannersGeneral(N-p, M, p, q) -
            covMannersGeneral(N-p, M-q, p, q)
            ) + (
            covMannersGeneral(N, M-p, p, q) +
            covMannersGeneral(N-q, M, p, q) -
            covMannersGeneral(N-q, M-p, p, q)
            )
    
    return (retval)

def main():
    # test
    p, q = 1, 2
    for n in range(-5, 9):
        for m in range(-5, 9):
            assert covManners(n, m) == covMannersGeneral(n, m, p, q)
            print "%+d x %+d floor, %+d x %+d brick: %d" %(n, m, p, q,
                                                    covManners(n, m))
    
    p, q = 2, 3
    for n in range(-5, 9):
        for m in range(-5, 9):
            print "%+d x %+d floor, %+d x %+d brick: %d" %(n, m, p, q,
                            covMannersGeneral(n, m, p, q))
            
if __name__ == '__main__':
    main()

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载