Recursive source program is as follows:
The result of running is no solution, and more than three people have no solution.
At the beginning, one side of the river where the merchants and robbers are located is set to 0 state, and the other side is set to 1 state (that is, when the ship sails to the other side, it is set to 1 state, which must meet the conditions).
# include & ltstdlib.h & gt
Struct node /* builds a data structure similar to a stack, and can browse each data point */
{
int x;
int y;
Int status;
Structure node * next
};
Typedef structure node status;
Typedef status * link;
link ppointer 1 = NULL;
link PPointer2 = NULL
int a 1,b 1;
int a2,B2;
/* Each data in the stack is divided into 0, 1 states */
void Push(int a,int b,int n)
{
Link newnode
new node =(link)malloc(sizeof(state));
New node-> x = a;
New node-> y = b;
New node-> state = n;
New node-> next = NULL
if(PPointer 1==NULL)
{
ppointer 1 = new node;
PPointer2 = newnode
}
other
{
PPointer2->next = newnode
PPointer2 = newnode
}
}
Void Pop() /* stack */
{
Link pointer;
if(PPointer 1==PPointer2)
{
Free (p pointer1);
ppointer 1 = NULL;
PPointer2 = NULL
}
Pointer = p pointer1;
While (pointer->; Au Suivant! =PPointer2)
Pointer = pointer-> Next;
Free (p pointer 2);
PPointer2 = pointer;
PPointer2->next = NULL
}
Int history(int a, int b, int n) /* Compare the input data with the duplicate data in the stack */
{
Link pointer;
if(PPointer 1==NULL)
Returns1;
other
{
Pointer = p pointer1;
While (pointer! = empty)
{
If (pointer->; X = = a & amp& amp pointer-> Y = = b & amp& amp pointer-> Status ==n)
Returns 0;
Pointer = pointer-> Next;
}
Returns1;
}
}
Int judge (int a, int b, int c, int d, int n)/* judges whether this state is feasible, in which the historical function */
{
If(history(a, b, n)==0) returns 0;
If (A> = 0& & amp> = 0& & amp & lt = 3& & amp & gt = 0& & amp & lt = 3 & amp & gt = 0. & ampb+d==3)
{
Switch (n)
{
Case 1:
{
If (a==3)
{
Push(a,b,n);
Returns1;
}
else if(a==0)
{
Push(a,b,n);
Returns1;
}
else if(a==b)
{
Push(a,b,n);
Returns1;
}
Otherwise, return 0;
}
Case 0:
{
If (a==3)
{
Push(a,b,n);
Returns1;
}
else if(a==0)
{
Push(a,b,n);
Returns1;
}
else if(a & gt; =b)
{
Push(a,b,n);
Returns1;
}
Otherwise, return 0;
}
}
}
Otherwise, return 0;
}
Int Du He (int a, int b, int n) /* recursive method is used to solve the problem of businessmen crossing the river, if this state conforms to */
{/* Then judge the next state until the problem is solved */
if(a = = 0 & amp; & ampb==0) returns1;
If(n==0) /* Judge whether the merchant's status meets the requirements when the status is 0 */
{
If (judging (a- 1, b- 1, 4a, 4b, 1))
{
If (Du He (a- 1, b- 1,1) =1)
Returns1;
}
If (judging (a, b-2, 3-a, 5-b, 1))
{
If (Du He (a, b-2, 1)== 1)
Returns1;
}
If (judging (a-2, b, 5-a, 3-b, 1))
{
If (Du He (a-2, b, 1)== 1)
Returns1;
}
If (judging (a- 1, b, 4-a, 3-b, 1))
{
If (Du He (a- 1, b, 1)== 1)
Returns1;
}
If (judging (a, b- 1, 3-a, 4-b, 1))
{
If (Du He (a, b- 1, 1)== 1)
Returns1;
}
other
{
pop(0);
Returns 0;
}
}
If(n== 1) /* Judge whether the merchant's status meets the requirements when the status is 0 */
{
If (judging (a+ 1, b+ 1, 2-a, 2-b, 0))
{
If (Du He (a+ 1, b+ 1, 0)== 1)
Returns1;
}
If (judging (a, b+2, 3-a, 1-b, 0))
{
If (Du He (a, b+2,0) = =1)
Returns1;
}
If (judging (a+2, b, 1-a, 3-b, 0))
{
If (Du He (a+2, B, 0)== 1)
Returns1;
}
If (judging (a+ 1, b, 2-a, 3-b, 0))
{
If (Du He (a+ 1, b, 0)== 1)
Returns1;
}
If (judging (a, b+ 1, 3-a, 2-b, 0))
{
If (Du He (a, b+ 1, 0)== 1)
Returns1;
}
other
{
pop( 1);
Returns 0;
}
}
Returns 0;
}
Master ()
{
Link pointer;
Push (3, 3, 0);
Du He (3,3,0);
Pointer = p pointer1;
While (pointer! = empty)
{
Printf( "%d, %d-%d\n ",pointer->; X, pointer->; Y, pointer-> Status);
Pointer = pointer-> Next;
}
getch();
}