luogu1644_跳马问题

luogu1644_跳马问题

时空限制    1000ms/128MB

题目描述

中国象棋半张棋盘如图1所示。马自左下角(0,0)向右上角(m,n)跳。规定只能往右跳,不准往左跳。比如图1中所示为一种跳行路线,并将路径总数打印出来。

输入输出格式

输入格式:

只有一行:两个数n,m

输出格式:

只有一个数:总方案数total。

输入输出样例

输入样例#1:

4 8

输出样例#1:

37

说明

所有数据:n,m<=18

扫描二维码关注公众号,回复: 2536618 查看本文章

代码

#include<iostream>
using namespace std;
const int dx[] = {-2,-1, 1, 2},
		  dy[] = { 1, 2, 2, 1};
int m,n,tot=0;

void search(int x,int y){
	if (x==m && y==n){
		tot++;
		return;
	}
	for (int i=0; i<4; i++){
		int xx=x+dx[i],yy=y+dy[i];
		if (xx>=0 && xx<=m && yy>=0 && yy<=n) search(xx,yy);
	}
}

int main(){
	cin>>m>>n;
	search(0,0);	//起点坐标
	cout<<tot<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WDAJSNHC/article/details/81318718
今日推荐