接下来是题目
Description
中国象棋大家都玩过吧!!!
现在有只小马迷路了,它现在在0,0位置,它的家在m,n位置(棋盘大小为m*n),现规定小马只能往右走,请聪明的你帮帮小马算一下回到家有几种方案?
Input
家的位置m,n 比如4,8 1=<m,n<=200
Output
一个整数即方案数
Sample Input
4 8
Sample Output
37
HINT
这题其实十分简单,只要弄懂马向右怎么走就行了。
废话不说多了,直接上代码!
#include <bits/stdc++.h> //万能头文件
using namespace std;
int sum=0,m,n;
int dfs(int h1,int h2) {
if(h1==n&&h2==m) //到达终点,返回1。
return 1;
if(h1<0||h2<0||h1>n||h2>m) //如果越界则返回0。
return 0;
return dfs(h1+1,h2+2)+dfs(h1+2,h2+1)+dfs(h1+2,h2-1)+dfs(h1+1,h2-2); //每个方向的坐标移动
}
int main() {
cin>>m>>n;
sum=dfs(0,0); //从0,0开始。
cout<<sum<<endl; //输出结果
return 0;
}