Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86658
  • 博文数量: 47
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-11 12:11
文章分类

全部博文(47)

文章存档

2008年(47)

我的朋友

分类:

2008-11-12 16:25:33

Problem 15

19 April 2002

Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.

How many routes are there through a 2020 grid?

我们令最右下角的点坐标为(0, 0), 令最左上角坐标为(i, j), 则(i, j)到(0, 0)的路线数为

(i-1, j)到(0, 0)和(i, j-1)到(0, 0)的路线数的和

a15 = {}

def getA15(i, j):
    if i == 0 or j == 0:
        a15[(i, j)] = 1
    else:
        if not a15.has_key((i,j)):
            a15[(i, j)] = getA15(i-1, j)+getA15(i, j-1)

    return a15[(i, j)]

def fun15():
    return getA15(20 ,20)

answer is 137846528820

还有一种更直接的方法,题目中只能向右和下移动,我们将向右移动看做0, 向下移动看做1, 则一共需要向右移动20步和向下移动20步,相当于20个1和20个0的组合

一共(40!)/((20!)*(20!)) = 137846528820

阅读(329) | 评论(0) | 转发(0) |
0

上一篇:Problem 14

下一篇:Problem 18

给主人留下些什么吧!~~