Home > Back-end >  About C solve the merchant model (deep search)
About C solve the merchant model (deep search)

Time:09-17

Problem: I don't know why the third recursive functions are not executed, requires only a way to get to the,
Procedure is as follows:

# include

using namespace std;

# define MAX 20

The class Point
{
Public:
Boolean operator==(Point pp)
{
If (pp. Businessman==this - & gt; Businessman & amp; & Pp. Slave==this - & gt; Slave)
return true;
The else
return false;
}

Int Businessman;
Int Slave;
};

Int step=0;//number of boat crossing the river

Point END={0, 0};//end mark

Int Cross_Step [MAX] [2];//used to represent each change in the number of business and the number of slaves Cross_Step [step] [0] on behalf of the merchant

Int SOLVE [5] [2]={{2, 0}, {0, 2}, {0, 1}, {1, 0}, {1, 1}};//the ship's people have five cases

Void DFS (Point Now)
{
If (Now==END)//print path
{
For (int I=1; I & lt;=step; I++)
{
Cout & lt; <"The first" & lt; }
system("pause");
exit(0);
}
Else if (Now. Businessman & gt; 3 | | Now. Businessman<0 | | Now. Slave> 3 | | Now. Slave<0)//in the wrong solution
{
Cout & lt; <"An error scheme" & lt; Step -;
return;
}
Else if (step & gt;=2)//from the second crossing the river began to row hard
{
If (Cross_Step (step 1) [0]==Cross_Step [step] [0] & amp; & Cross_Step [step], [1]==Cross_Step [step 1] [1])
{
Cout & lt; <"Duplicate" & lt; Step -;
return;
}
}
Else if ((Now. Businessman> 0 & amp; & Now. The Businessman & lt; Now the Slave) | | (Now. Businessman<3 & amp; & Now. The Businessman & gt; Now the Slave))//servant revolt
{
Cout & lt; <"A servant to revolt" & lt; Step -;
return;
}
The else
{

int i;
Point temp;
For (I=0; I & lt; 5; Five attempt i++)//
{
Temp=Now;
Step++;
Cout & lt; <"The first" & lt; If (step % 2!=0)//from sea to shining sea
{
Temp. Businessman -=SOLVE [I] [0];
Temp. Slave -=SOLVE [I] [1];
}
The else//come back
{
Temp. Businessman +=SOLVE [I] [0];
Temp. Slave +=SOLVE [I] [1];
}
Cross_Step [step] [0]=SOLVE [I] [0];
Cross_Step [step], [1]=SOLVE [I] [1].
DFS (temp);
}
}
}

//int SOLVE [5] [2]={{2, 0}, {0, 2}, {0, 1}, {1, 0}, {1, 1}};//the ship's people have five cases
Int main ()
{
Point the Begin;
The Begin. Businessman=Begin. Slave=3;
DFS (Begin);
Cout & lt; <"There is no solution!"
system("pause");
return 0;
}

Run screenshot:

CodePudding user response:

Thank you has been solved,

CodePudding user response:

  • Related