=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); /*进行下一步*/
}
}
return 0;
}
int main()
{
printf("\n\n 农夫过河问题,解决方案如下:\n\n\n");
search(0);
return 0;
}
运行结果:
农夫过河问题,解决方案如下:
east: wolf goat cabbage farmer west: none
the 1 time
east: wolf cabbage west: goat farmer
the 2 time
<----- farmer
east: wolf cabbage farmer west: goat
the 3 time
-----> farmer and wolf
east: cabbage west: wolf goat farmer
the 4 time
<----- farmer and goat
east: goat cabbage farmer west: wolf
the 5 time
-----> farmer and cabbage
east: goat west: wolf cabbage farmer
the 6 time
<----- farmer
east: goat farmer west: wolf cabbage
the 7 time
-----> farmer and goat
east: none west: wolf goat cabbage farmer
east: wolf goat cabbage farmer west: none
the 1 time
east: wolf cabbage west: goat farmer
&n