不知道错哪里了,求大佬调一下
查看原帖
不知道错哪里了,求大佬调一下
1113550
Dihuan楼主2023/12/4 20:53
#include <iostream>
using namespace std;
int map[25][25];
int attack_x[] = { 0, -2, -1, 1, 2, 2, 1, -1, -2 };
int attack_y[] = { 0, 1, 2, 2, 1, -1, -2, -2, -1 };
bool check_being_attack(int temp_x, int temp_y, int horse_x, int horse_y)//是否处于马的攻击点上
{
	for (int i = 0;i < 9;i++)
	{
		if (temp_x == horse_x + attack_x[i] && temp_y == horse_y + attack_y[i])
		{
			return true;
		}
	}
	return false;
}
bool check_in_area(int temp_x, int temp_y, int horse_x, int horse_y)//是否在有效范围内,是否越界
{
	if (temp_x >= 0 &&temp_x<=horse_x&&temp_y>=0&&temp_y<=horse_y)
	{
		return true;
	}
	return false;
}
long long int dp(int temp_x, int temp_y, int horse_x, int horse_y)
{
	if (temp_x == 0 && temp_y == 0)//递归出口为坐标回到起点或者是越界
	{
		return 1;
	}
	if (check_in_area(temp_x, temp_y, horse_x, horse_y) == false)return 0;
	if (check_being_attack(temp_x, temp_y,horse_x,horse_y) == true)return 0;
	return dp(temp_x - 1, temp_y, horse_x, horse_y) + dp(temp_x, temp_y-1, horse_x, horse_y);//递归
}
int main() {
	int horse_x, horse_y, final_x, final_y;
	cin >> final_x >> final_y >> horse_x >> horse_y;//输入终点和马的坐标
	cout << dp(final_x, final_y, horse_x, horse_y);//输出方法的总数

}
2023/12/4 20:53
加载中...