問題:
Starting in thetop left corner of a 2×2 grid, and only being able to move to the right anddown, there are exactly 6 routes to the bottom right corner.
How many suchroutes are there through a 20×20 grid?
解決思路:
將上圖數據放正,就是楊輝三角:
前十行如下:
1 | ||||||||||||||||||
1 | 1 | |||||||||||||||||
1 | 2 | 1 | ||||||||||||||||
1 | 3 | 3 | 1 | |||||||||||||||
1 | 4 | 6 | 4 | 1 | ||||||||||||||
1 | 5 | 10 | 10 | 5 | 1 | |||||||||||||
1 | 6 | 15 | 20 | 15 | 6 | 1 | ||||||||||||
1 | 7 | 21 | 35 | 35 | 21 | 7 | 1 | |||||||||||
1 | 8 | 28 | 56 | 70 | 56 | 28 | 8 | 1 | ||||||||||
1 | 9 | 36 | 84 | 126 | 126 | 84 | 36 | 9 | 1 |
第11 行:
1,10,45,120,210,252,210,120,45,10,1
第12 行:
1,11,55,165,330,462,462,330,165,55,11,1
第13行:
1,12,66,220,495,792,924,792,495,220,66,12,1
第14行:
1,13,78,286,715,1287,1716,1716,1287,715,286,78,13,1
……
第n-1行:
1,(n-1)C1,(n-2)C2,…,(n-1)C(r-1),…,(n-1)C(n-2),1
第n行:
1,nC1,nC2,…,nC(r-1),nCr,…,nC(r-1),1
問題要求的20*20的網格的路徑數目,即楊輝三角第41行第21個數。
利用楊輝三角的性質:
第n行的m個數可表示爲C(n-1,m-1),即爲從n-1個不同元素中取m-1個元素的組合數。
組合數計算方法:C(n,m)=n!/[m!(n-m)!]
可計算第41行,第21個數爲:C(40,20)=40!/[20!*20!]=137846528820