C语言出错农夫过河
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 15
int a[N][4];
int b[N];
char *name[]=
{
" ",
"and wolf",
"and goat",
"and cabbage"
};
void search(int step)
{
int i;
if(a[step][0]+a[step][1]+a[step][2]+a[step][3]==4)
{
for(i=0;i<=step;i++)
{
printf("east: ");
if(a[i][0]==0)
printf("wolf ");
if(a[i][1]==0)
printf("goat ");
if(a[i][2]==0)
printf("cabbage ");
if(a[i][3]==0)
printf("farmer ");
if(a[i][0]&&a[i][1]&&a[i][2]&&a[i][3])
printf("none");
printf(" ");
printf("west: ");
if(a[i][0]==1)
printf("wolf ");
if(a[i][1]==1)
printf("goat ");
if(a[i][2]==1)
printf("cabbage ");
if(a[i][3]==1)
printf("farmer ");
if(!(a[i][0]||a[i][1]||a[i][2]||a[i][3]))
printf("none");
printf("\n\n\n");
if(i<step)
printf(" the %d time\n",i+1);
if(i>0&&i<step)
{
if(a[i][3]==0)
{
printf(" ----------> farmer ");
printf("%s\n",name[b[i]+1]);
}
else
{
printf(" <---------- farmer ");
printf("%s\n",name[b[i]+1]);
}
}
}
printf("\n\n\n\n");
return;
}
for(i=0;i<step;i++)
{
if(memcmp(a[i],a[step],16)==0)
{
return;
}
}
if(a[step][1]!=a[step][3]&&(a[step][2]==a[step][1]||a[step][0]==a[step][1]))
{
return;
}
for(i=-1;i<=2;i++)
{
b[step]=i;
memcpy(a[step+1],a[step],16);
a[step+1][3]=1-a[step+1][3];
if(i==-1)
{
search(step+1);
}
else
if(a[step][i]==a[step][3])
{
a[step+1][i]==a[step+1][3];
search(step+1);
}
}
}
int main()
{
printf("\n\n 农夫过河问题,解决方案如下:\n\n\n");
search(0);
return 0;
}
æ¾å°æåï¼å ¶å®åªæ¯ä¸ä¸ªå¾å°çé®é¢ã
searchå½æ°æåä¸ä¸ªif
if(a[step][i]==a[step][3])