解题思路:用递归去遍历所有可能,对于正确的可能返回1,最后返回所有正确的情况
注意事项:注意递归的调用条件
参考代码:
#include using namespace std; int m,n; int answer(int p,int q) { if(p%2!=0||q%2!=0)//如果能走(行标列标不同时为偶数) { if(n==p&&q==m)//能递归到最后一格,代表这种方案可行,返回 1 { return 1; } else if(p>n||q>m)//若递归内容超过二维矩阵,代表不可取,返回0 { return 0; } else//返回下一步(向下,向右)的值 { return answer(p+1,q)+answer(p,q+1); } } else//不能走返回0 return 0; } int main() { cin>>n>>m; cout<<answer(1,1)<<endl;//从1,1,开始,求解所有正确方案,并输出 return 0; }
0.0分
22 人评分