Problem 15
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
# 20*20的网格中从左上到右下(只能下或右)有多少种方法。
思路:结果为排列组合C(20,40),用程序计算可以用动态规划,动态规划还可以计算每条边都有权重的网格。
f = [[i for i in range(21)] for i in range(21)]
for i in range(21):
f[0][i] = f[i][0] = 1
for i in range(1,21):
for j in range(1,21):
f[i][j] = f[i-1][j] + f[i][j-1]
print(f[20][20])
结果:137846528820