用递归实现的,没有stackoverflow但是tle了
#include <stdio.h>
int isHorse(int x, int y, int x_horse, int y_horse);
int abs(int n);
int routeNumbers(int x, int y, int x_horse, int y_horse, int n, int m);
int main(int argc, char const *argv[]) {
int x_horse, y_horse, n, m, num;
scanf("%d%d%d%d", &n, &m, &x_horse, &y_horse);
num = routeNumbers(0, 0, x_horse, y_horse, n, m);
printf("%d", num);
return 0;
}
int isHorse(int x, int y, int x_horse, int y_horse) {
return (abs(x - x_horse) == 2 && abs(y - y_horse) == 1) ||
(abs(x - x_horse) == 1 && abs(y - y_horse) == 2) ||
(x == x_horse && y == y_horse);
}
int abs(int n) { return n < 0 ? -n : n; }
int routeNumbers(int x, int y, int x_horse, int y_horse, int n, int m) {
int num = 0;
if ((x == n) && (y == m))
num++;
else if (!isHorse(x, y, x_horse, y_horse)) {
if (x < n) num += routeNumbers(x + 1, y, x_horse, y_horse, n, m);
if (y < m) num += routeNumbers(x, y + 1, x_horse, y_horse, n, m);
}
return num;
}