赞
踩
有一个m行n列的矩阵,最左下角是(1,1)从这个方格走到最右上角有多少种走法。.
用递归来实现,一般从任意一个方格开始 有两种走法,向右走和向上走。于是递归函数里也分为两条路。
递归的终止条件是当走到了最右上角,走法就会加一条。
递归限制条件,行不能超过m列不能超过n。
dp和递归递推确实是一类题。
- #include <iostream>
- #include<algorithm>
- #include<cmath>
- int h=0;int n,m;
- using namespace std;
- int f(int a,int s)
- {if(a==n&&s==m){h++;return 0;}
- if(a>n||s>m)return 0;
- f(a+1,s);f(a,s+1);
- }
- int main()
- {
- cin>>n>>m;
- f(1,1);
- cout<<h;
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。