<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>
Missionary</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">my first edition of </span>my <span lang="en-ca">Missionary</span> class <span lang="en-ca">inherited from DFSArray class, so you can also regard it as </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>an application of DFS class.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <b><font size="2"><span lang="en-ca">This is a rather classic problem which we 
  all solved during our primary school or middle school and quite </span></font></b></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>frequently it is given as an example in 
  many AI course. </b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>There are 3 missionaries and 3 barbarians 
  in one side of river, there is only one boat which can hold maximum</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>two persons. That is either MM, BB, MB, 
  M, B. (If you regard barbarian also as a person...) There is one rule </b>
  </font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>that whenever barbarians are 
  over-numbered with missionaries then missionaries will be eaten by barbarians. 
  So,</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>try to arrange a way to let 3 
  missionaries to cross river with that boat.</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre><b><span lang="en-ca">Basically you should realize that there are actually 10 possible choices for shipping at every occation.</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>MM_LR, BB_LR, MB_LR, M_LR, B_LR, MM_RL, BB_RL, MB_RL, M_RL, B_RL</b></span><b><span lang="en-ca">: LR stands for from LEFT TO RIGHT, RL is opposite.</span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">And MM stands for two missionaries, BB for two barbarians, M of course stands for one missionary, B stands for </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>one barbarian, MB stands for one missionary and one barbarian. </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>So, we actually figure out all possible choices. Then the job is only how to verify the options which is quite</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>a bit complicated. I designed a structure to hold the info for the ten choices. And in the class Missionary, there</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>is a private member Position which records the state of game. It is the problem that when and how should we update</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>this state!</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>As you know, I already compile the DFS class into object file (.obj file) and from that I use the &quot;lib&quot; command to</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>make it a .lib file and put it into the &quot;lib&quot; directory under &quot;vc98&quot;. And the &quot;DFSArray.h&quot; file was placed in </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>directory &quot;include&quot;. On the project setting menu, I insert the name of &quot;DFSArray.lib&quot;. So, this implies that my </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>DFSArray class is actually not reachable for source code! I cannot change anything from the base class! </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This really creates some trouble if your base class is not designed properly. For example, the Position array </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>cannot be updated when verifying data as in function &quot;validateOption(int sonData)&quot;, you have no access to your</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>son node. Then you can only do the synchronization action when you add son nodes. This is rather complicated!</b></span></pre>
</div>
<div align="left">
  <pre><b><font size="5" color="#FF0000">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b>1.<span lang="en-ca"> bool Missionary::evaluate()</span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">This function tests the son node, but you need to synchronize Position with &quot;data&quot;, otherwise, you will only check</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>the parent Position as when new node is created, Position are copied from parent.</b></span></pre>
</div>
<div align="left">
  <pre><b>2. bool Missionary::validateOption(int sonData)</b></pre>
</div>
<div align="left">
  <pre><b>T<span lang="en-ca">here is a lot of issues needed to be taken care with! First, check if boat is available for the option which is </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>simple. Second, there should be enough person to be shipped to other side and barbarians can never outnumber </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>missionaries at either sides of river. Third, check if direct repetition or indirect repetition is made. Direct</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>means you ship something back immediately after you ship it here. But indirect is a little tricky! At beginning</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>my program is cunning enough to produce a lot of indirect repetition---after several rounds, it become the original</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>starting state. So, I add an extra condition-checking function to see if original starting state is reached.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="5" color="#FF0000"><b>E</b></font></span><b><font color="#ff0000" size="5">.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1.<span lang="en-ca"> You tell me!</span></b></pre>
</div>
<pre>　</pre>
<pre><b><font color="#FF0000" size="3"><span lang="en-ca">DFSArray.h  (placed in &quot;include&quot; directory under vc98)</span></font></b></pre>
<pre>#ifndef DFSARRAY_H
#define DFSARRAY_H
#include &lt;iostream&gt;

using namespace std;

const int MaxSonCount=8;

const int MaxLevel =20;

const int MaxOptions = 10;

const int MaxNodeCount = 1000;

class DFS
{
private:
	void initialize(DFS* now);
	static DFS* nodeList[MaxNodeCount];
	static bool findAll;
	static int optionArray[MaxOptions];
	static int optionCount;
	static char** nameString;
	static int solutionCount;
	static int maxLevel;
	static int domino;
	void addSon(int sonData);
	bool touchDown();
	void setMaxLevel(int max) { maxLevel = max;}
	DFS* upDate();
	bool hasBrother();
	DFS* nextBrother();
	bool generate();
	DFS* findNext();
	DFS* getNode(int theIndex){return nodeList[theIndex];}
	void putNode(int theIndex) { nodeList[theIndex] = this;}
	DFS* getParent(){return getNode(parent);}
	bool hasParent(){return parent!=-1;}
protected:
	int parent;
	int sonCount;
	int sonIndex;
	int son;
	int depth;
	int index;//index in nodeList
	int data;
	void backTrack();
	static int trackRecord[MaxLevel];
	virtual DFS* createSon();
	virtual bool validateOption(int sonData);	
	virtual void onLeave();
	virtual bool evaluate();
	virtual void collectResult();
public:
	int getSolutionCount(){ return solutionCount;}
	void setOptions(int* array, int size);
	void setNameStr(char** str){nameString = str;}
	void beginCount(int max, bool findall=false);
};

#endif</pre>
<pre>　</pre>
<pre><b><font color="#FF0000" size="3"><span lang="en-ca">DFSArray.lib is placed in &quot;lib&quot; directory under &quot;vc98&quot; directory!</span></font></b></pre>
<pre>　</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">missionary.cpp</font></span></b></pre>
<pre>#include &lt;DFSArray.h&gt;

using namespace std;

enum RiverSide
{LEFT, RIGHT};

enum People
{MISSIONARY, BARBARIAN};

struct Action
{
	int people[2];
	RiverSide riverSide;
};

Action actionArray[10] = {{{2,0},LEFT}, {{0,2}, LEFT}, {{1,1},LEFT},{{1,0},LEFT},
{{0,1}, LEFT}, {{2,0},RIGHT}, {{0,2}, RIGHT}, {{1,1,}, RIGHT}, {{1,0}, RIGHT},{{0,1},RIGHT}};

//2M, 2B, 1M1B, 1M, 1B and LR means from left to right, RL is opposite
enum OptionType
{MM_LR, BB_LR, MB_LR, M_LR, B_LR, MM_RL, BB_RL, MB_RL, M_RL, B_RL}; 

//3 missionary are at left side of river
//3 barbarian are also at left side

int optionArray[10] = {MM_LR, BB_LR, MB_LR, M_LR, B_LR, MM_RL, BB_RL, MB_RL, M_RL, B_RL}; 

char* optionString[10] = {&quot;MM_LR&quot;, &quot;BB_LR&quot;, &quot;MB_LR&quot;, &quot;M_LR&quot;, &quot;B_LR&quot;, &quot;MM_RL&quot;, 
	&quot;BB_RL&quot;, &quot;MB_RL&quot;, &quot;M_RL&quot;, &quot;B_RL&quot;};

bool isRoot = true;

class Missionary: public DFS
{
private:
	int Position[2][2];
	RiverSide boat;
	bool isLooping(int sonData);
	bool checkData(int sonData);
	bool initialized;
protected:
	bool validateOption(int sonData);
	DFS* createSon();
	bool evaluate();
public:
	Missionary();
};


int main()
{
	Missionary M;
	M.setNameStr(optionString);
	M.setOptions(optionArray, 10);
	M.beginCount(20, true);
	
	return 0;
}

Missionary::Missionary()
{
	if (isRoot)
	{
		Position[MISSIONARY][LEFT] = 3;
		Position[MISSIONARY][RIGHT]=0;
		Position[BARBARIAN][LEFT] = 3;
		Position[BARBARIAN][RIGHT]=0;
		isRoot = false;
		boat = LEFT;
	}
	initialized =false;
}

bool Missionary::evaluate()
{
	if (!initialized&amp;&amp;parent!=-1)//this is the first son
	{
		RiverSide river = actionArray[data].riverSide;
		RiverSide opposite = (river==LEFT?RIGHT:LEFT);
		//initialize Position so that it reflects the move
		//this is ackward as I cannot access son Ptr anymore
		Position[MISSIONARY][river]-= actionArray[data].people[MISSIONARY];
		Position[MISSIONARY][opposite] += actionArray[data].people[MISSIONARY];
		Position[BARBARIAN][river]-= actionArray[data].people[BARBARIAN];
		Position[BARBARIAN][opposite] += actionArray[data].people[BARBARIAN];
		boat = (boat==LEFT?RIGHT:LEFT);
		initialized = true;
	}
	return Position[MISSIONARY][RIGHT]==3;
}

bool Missionary::isLooping(int sonData)
{
	backTrack();
	return (trackRecord[depth-1] - sonData==5)||(sonData - trackRecord[depth-1] ==5);
}


bool Missionary::checkData(int sonData)
{
	RiverSide river = actionArray[sonData].riverSide;
	RiverSide opposite = (river==LEFT?RIGHT:LEFT);
	
	if (river==RIGHT
		&amp;&amp;Position[MISSIONARY][river] - actionArray[sonData].people[MISSIONARY]==0
		&amp;&amp;Position[BARBARIAN][river] - actionArray[sonData].people[BARBARIAN]==0)
		return false;

	return ((Position[MISSIONARY][river] - actionArray[sonData].people[MISSIONARY]&gt;=
		Position[BARBARIAN][river] - actionArray[sonData].people[BARBARIAN])
		||(Position[MISSIONARY][river] - actionArray[sonData].people[MISSIONARY]==0))
		
		&amp;&amp;
		((Position[MISSIONARY][opposite]+ actionArray[sonData].people[MISSIONARY]&gt;=
		Position[BARBARIAN][opposite]+actionArray[sonData].people[BARBARIAN])
		||(Position[MISSIONARY][opposite]+ actionArray[sonData].people[MISSIONARY]==0));
}

bool Missionary::validateOption(int sonData)
{
	
	if (!initialized&amp;&amp;parent!=-1)//this is the first son
	{
		RiverSide river = actionArray[data].riverSide;
		RiverSide opposite = (river==LEFT?RIGHT:LEFT);
		//initialize Position so that it reflects the move
		//this is ackward as I cannot access son Ptr anymore
		Position[MISSIONARY][river]-= actionArray[data].people[MISSIONARY];
		Position[MISSIONARY][opposite] += actionArray[data].people[MISSIONARY];
		Position[BARBARIAN][river]-= actionArray[data].people[BARBARIAN];
		Position[BARBARIAN][opposite] += actionArray[data].people[BARBARIAN];
		boat = (boat==LEFT?RIGHT:LEFT);
		initialized = true;
	}


	if (boat!=actionArray[sonData].riverSide)
	{
		return false;
	}
	else
	{
		if (Position[MISSIONARY][boat]&lt;actionArray[sonData].people[MISSIONARY]||
			Position[BARBARIAN][boat]&lt;actionArray[sonData].people[BARBARIAN])
		{
			return false;
		}
		if (!isLooping(sonData))
		{
			if (checkData(sonData))
			{
				return true;
			}
		}
	}
	return false;
}


DFS* Missionary::createSon()
{
	DFS* ptr= new Missionary;
	for (int i=0; i&lt;2; i++)
	{
		for (int j=0; j&lt;2; j++)
		{
			((Missionary*)(ptr))-&gt;Position[i][j] = Position[i][j];
		}
		((Missionary*)(ptr))-&gt;boat = boat;
	}
	return ptr;
}</pre>
<pre>
</pre>
<pre></pre>
<pre></pre>
<pre><b><font color="#FF0000" size="3"><span lang="en-ca"><a name="result"></a>Running result of program:</span></font></b></pre>
<pre>The route is:BB_LR, B_RL, BB_LR, B_RL, MM_LR, MB_RL, MM_LR, It is number:0
The route is:MB_LR, M_RL, BB_LR, B_RL, MM_LR, MB_RL, MM_LR, It is number:1
Press any key to continue</pre>
<pre>　</pre>
<pre><font color="#FF0000"><span lang="en-ca">Do you understand the result? It means 2B go, 1B back, 2B go, 1B back, 2M go, 1M1B back, 2M go--&gt;succeed!</span>
</font></pre>
<pre></pre>
<pre>　</pre>
<pre>	</pre>
<pre></pre>
<pre>			
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>