

#include<iostream>
using namespace std;
int xadd[2] = { 1,0 };
int yadd[2] = { 0,1 };
int way[20][20] = { 0 };
int sum = 0;
struct horse
{
int x[9] = { 0,-1,-2,-2,-1,1,2,2,1 };
int y[9] = { 0,-2,-1,1,2,-2,-1,1,2 };
int place[9][2];
};
void horseplace(struct horse*h,int a,int b)
{
for (int i = 0; i < 9; i++)
{
h->place[i][0] = a + h->x[i];
h->place[i][1] = b + h->y[i];
}
}
int isin(int x, int y,int n,int m,struct horse* h)
{
if (x<0 || x>n || y<0 || y>m
|| x == h->place[0][0] && y == h->place[0][1]
|| x == h->place[1][0] && y == h->place[1][1]
|| x == h->place[2][0] && y == h->place[2][1]
|| x == h->place[3][0] && y == h->place[3][1]
|| x == h->place[4][0] && y == h->place[4][1]
|| x == h->place[5][0] && y == h->place[5][1]
|| x == h->place[6][0] && y == h->place[6][1]
|| x == h->place[7][0] && y == h->place[7][1]
|| x == h->place[8][0] && y == h->place[8][1])
return 0;
else
return 1;
}
int num(struct horse* h, int n, int m,int a=0,int b=0)
{
int c, d, k;
for (k = 0; k < 2;++k)
{
c=a + xadd[k];
d=b + yadd[k];
if (isin(c, d, n, m, h))
{
a = c;
b = d;
if (a == n && b == m)
{
sum++;
}
else
{
num(h, n, m, a, b);
}
}
}
return sum;
}
int main()
{
struct horse h;
int a, b, n, m;
cin >> n >> m >> a >> b;
horseplace(&h, a, b);
int g = num(&h, n, m);
cout << g << endl;
system("pause");
return 0;
}
我找到的问题是在函数int num(struct horse* h, int n, int m,int a=0,int b=0)的for循环部分,
按照样例输入“6 6 3 3”,第一次到达终点是先往下走再往右走,然后逐个退出递归。但是在(6,0)位置时,for循环的k值本应该是1,退出递归到(6,0)时却变成了0,导致多计算了一次路径。请帮我找找原因,谢谢!