2楼的,没你说的这么简单.
这是我在网上找的别人写好的.大家看看.
#include <stdio.h>
int N;
int num=0;
int S[100][2];
int fit=0;
int p;
int q;
int check(int m,int n,int u,int v,int total)
{
int i;
if(total%2==0)
{
u=u-m;
v=v-n;
}
else {
u=u+m;
v=v+n;
}
for(i=0;i<total+1;i++)
{
if((u==S[i][0])&&(v==S[i][1])&&((total+1)%2==i%2))
{
return 0;}
}
if((u==0)&&(v==0))
return 2;
else
{
if(u<0||v<0||(N-u)<0||(N-v)<0)
{
return 0;}
if((u==v)||(u==0)||u==N)
return 1;
else
{
return 0; }
}
}
void act(int total,int u,int v)
{
int i;
int j;
int a=0;
int flag=0;
if(total>num)
{
num=total;
}
for(i=0;i<=p;i++)
{
for(j=0;j<=q;j++)
{
if((i==0)&&(j==0))
continue;
if((i+j)<=2)
{flag=check(i,j,u,v,total);
if(flag==2)
{
printf("\n");
for(a=0;a<total+1;a++)
printf("(%d,%d)->",S[a][0],S[a][1]);
printf("(0,0)");
fit=1;
return;
}
else if(flag)
{
if(total%2==0)
{
S[total+1][0]=u-i;
S[total+1][1]=v-j;
act(total+1,u-i,v-j);
}
else
{
S[total+1][0]=u+i;
S[total+1][1]=v+j;
act(total+1,u+i,v+j);
}
}
}
}
}
S[total][0]=0;
S[total][1]=0;
}
main()
{
printf("please enter the num:");
scanf("%d",&N);
printf("please enter the capacity of mercher:");
scanf("%d",&p);
printf("please enter the capacity of salave:");
scanf("%d",&q);
S[0][0]=N;
S[0][1]=N;
act(0,N,N);
if(fit==0)
printf("There is no proporate answer.");
getch();
}