题目大意:
一个矩形网格grid(n*m),从左下角走到右上角,问所有可能的路径数目。
解题思路:
以3×4的网格为例,问题等价于从_ _ _ _ _ _ _选出4个空格为“→”,剩下3个空格为“↑”,例如:
→ → → → ↑ ↑ ↑
→ ↑ → ↑ → ↑ →
… … … …
!!!组合数C(n+m,m)
为了减少计算,这里m取min{n,m}
- C(n,m)=A(n,m)/A(m,m)=n! / (n-m)!×m!
关于组合数C(n,m)的代码实现,这里提供两种方法:
(1)递归调用
(2)迭代(自己复合自己,重叠一定的步骤,新的值替代旧的值,累加、累乘都属于迭代过程)
参考代码+部分解释:
递归调用
#include
#include
#include
#include
==========================华丽的分割线=============================
迭代
#include
#include
#include
#include