I wrote a recursive function to calculate the number of the way for "horse" to move from one point to another in Chinese Chess in the following, but I cannot get the correct answer. My idea is to sum up the other 8(or less than 8) points' way toward the target. Thanks for any opinions in advance!(adsbygoogle = window.adsbygoogle || []).push({});

Code (C):

int ans(int orig_x, int orig_y, int tar_x, int tar_y, int place[][8])

{

int type[][2]={-1,2,1,2,1,-2,-1,-2,-2,-1,-2,1,2,1,2,-1};

int next_x,next_y,a[8],b=0,i,j,count_last;

place[orig_x+1][orig_y+1]=-1;

place[tar_x+1][tar_y+1]=1;

for(i=0;i<4;i++)

{

next_x=orig_x+type[i][0];

next_y=orig_y+type[i][1];

if(place[next_x+1][next_y+1]==1&&place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]!=2)

{

if(next_x==tar_x&&next_y==tar_y)

{

count_last=0;

for(j=0;j<4;j++)

{

if(j=i+(i<2)*2-(i>1)*2)

continue;

if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]==2)

count_last+=1;

}

for(j=4;j<8;j++)

{

if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+type[i][0]/abs(type[i][0])+1][next_y+1]==2)

count_last+=1;

}

if(count_last==7)

return 1;

else

a[i]=ans(next_x, next_y, tar_x, tar_y, place)+1;

}

place[orig_x+1][orig_y+1]=-1;

}

else

a[i]=0;

}

for(i=4;i<8;i++)

{

next_x=orig_x+type[i][0];

next_y=orig_y+type[i][1];

if(place[next_x+1][next_y+1]==1&&place[next_x+type[i][0]/abs(type[i][0])+1][next_y+1]!=2)

{

if(next_x==tar_x&&next_y==tar_y)

{

count_last=0;

for(j=0;j<4;j++)

{

if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]==2)

count_last+=1;

}

for(j=4;j<8;j++)

{

if(j=i+(i<6)*2-(i>5)*2)

continue;

if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+type[i][0]/abs(type[i][0])][next_y+1]==2)

count_last+=1;

}

if(count_last==7)

return 1;

else

a[i]=ans(next_x, next_y, tar_x, tar_y, place)+1;

}

place[orig_x+1][orig_y+1]=-1;

}

else

a[i]=0;

}

for(i=0;i<7;i++)

b+=a[i];

return b;

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# About Recursion

Tags:

**Physics Forums | Science Articles, Homework Help, Discussion**