<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="left"><font size="6" color="#FF0000"><b><span lang="en-ca">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
Space Walker--Application of BFS</b></font></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 actually an application of my BFS class, or you can call it second edition of counting model, whatever.</b></pre>
</div>
<div align="left">
  <pre><b>The base framework are exactly same as Counting problem. Originally I planned to let derived class to implement</b></pre>
</div>
<div align="left">
  <pre><b>some &quot;protected virtual&quot; method to do their own job. But unfortunately the &quot;static&quot; member root destroy the </b></pre>
</div>
<div align="left">
  <pre><b>ability of multimorphiness of C++. I have to copy the code and rewrite those virtual function without inheriting.</b></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">
  <pre><b><span lang="en-ca">How many </span>ways are there to trave in xyzw space from the origin (0,0,0,0) to the point (4,3,5,4) by taking steps </b></pre>
</div>
<div align="left">
  <pre><b>one unit in the positive x, positive y, positive z, or positive w direction?</b></pre>
</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><span lang="en-ca"><b>No matter how complicated a problem may become, we can divide and conquer it by counting! And breadth-first-search</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>is one of the simplest way to do it. </b></span></pre>
</div>
<div align="left">
  <pre><b>As I mentioned I cannot inheritate from CountTree, I have to rewrite it. And the problem is 16-level BFS with each</b></pre>
</div>
<div align="left">
  <pre><b>level at most 4 options. I found out my laptop cannot go beyond 11 level which is roughly 4,194,304 nodes alone.</b></pre>
</div>
<div align="left">
  <pre><b>So, I simplify the question a bit to point (1,3,2,4) which is 10-level question. And the output file is 420k with </b></pre>
</div>
<div align="left">
  <pre><b>12600 solutions.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b>1. virtual bool validateOption(int sonData);</b></pre>
</div>
<div align="left">
  <pre><b>This is the place where user-defined methods are implemented to determine if the data (integer)	will be verified </b></pre>
</div>
<div align="left">
  <pre><b>to be able to be added.</b></pre>
</div>
<div align="left">
  <pre><b>2. virtual void doGenerate();</b></pre>
</div>
<div align="left">
  <pre><b>This is user-defined way that cases are generated. Method validateOptions will be combined with it to determine </b></pre>
</div>
<div align="left">
  <pre><b>which cases should be added.</b></pre>
</div>
<div align="left">
  <pre><b>3. void CountTree::traverse();</b></pre>
</div>
<div align="left">
  <pre><b>This method allows you to traverse all leaf points. </b></pre>
</div>
<div align="left">
  <pre><b>4. virtual void onVisit();</b></pre>
</div>
<div align="left">
  <pre><b>This is user-defined method like outputing all outcomes.</b></pre>
</div>
<pre>กก</pre>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxSonCount =8;


enum AxisName
{X, Y, Z, W};

int limit[4] = {1,3,2,4};

char* directionString[4] = {&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;, &quot;W&quot;};

int axis[4] = {X,Y,Z,W};
	
class CountTree
{
private:
	static CountTree root;
	static int level;
	int depth;
	CountTree* parent;
	CountTree* sonArray[MaxSonCount];
	int data;
	int sonCount;
	int sonIndex;
	void addSon(int sonData);
	CountTree* findBrother();
	CountTree* findUncle();
	bool hasBrother();
	CountTree* nextBrother();
	CountTree* diving();
	void initialize();
	bool touchDown();
	bool canDive();
	bool generate();
	void traverse();
protected:
	virtual bool lastChance();
	virtual void doGenerate();
	virtual bool validateOption(int sonData);	
	virtual void onVisit();
	virtual bool lastlast();
public:
	void beginCount();
};

int CountTree::level = 0;
CountTree CountTree::root;

int main()
{
	CountTree R;
	R.beginCount();
	
	return 0;
}

bool CountTree::lastlast()
{
	return level==4;
}

void CountTree::onVisit()
{
	int temp[16] = {0};
	CountTree* ptr = this;

	while (ptr-&gt;parent!=NULL)
	{
		temp[ptr-&gt;depth-1] = ptr-&gt;data;
		ptr = ptr-&gt;parent;
	}
	cout&lt;&lt;&quot;The route is:&quot;;
	for (int i=0; i&lt;depth; i++)
	{
		cout&lt;&lt;directionString[temp[i]]&lt;&lt;&quot;, &quot;;
	}
	cout&lt;&lt;endl;
}

bool CountTree::validateOption(int sonData)
{
	int counter[4]={0}; //count all axis advances
	CountTree* ptr= this;

	while (ptr-&gt;parent!=NULL)
	{
		counter[ptr-&gt;data]++; //data is axis
		ptr = ptr-&gt;parent;
	}

	return counter[sonData]&lt;limit[sonData];
}




void CountTree::doGenerate()
{
	for (int i=0; i&lt;4; i++)
	{
		if (validateOption(axis[i]))
		{
			addSon(axis[i]);
		}
	}
}

bool CountTree::lastChance()
{
	return level==9;
}


void CountTree::traverse()
{
	int counter=0;
	CountTree* ptr= &amp;root;
	while (ptr!=NULL)
	{
		while(ptr-&gt;canDive())
		{
			ptr= ptr-&gt;sonArray[0];
		}
		cout&lt;&lt;&quot;now it is:&quot;&lt;&lt;++counter;
		ptr-&gt;onVisit();
		
		if (ptr-&gt;hasBrother())
		{
			ptr= ptr-&gt;nextBrother();		
		}
		else
		{
			ptr=ptr-&gt;findUncle();
		}		
	}	
	cout&lt;&lt;&quot;The total number is :&quot;&lt;&lt;counter&lt;&lt;endl;
}


void CountTree::beginCount()
{
	initialize();
	while (generate())
	{
		if (!lastChance()) //true to keep go on
		{
			level++;
			cout&lt;&lt;&quot;Now is level:&quot;&lt;&lt;level&lt;&lt;endl;
		}
		else
		{
			break;
		}
	}
	traverse();
}


bool CountTree::generate()
{
	CountTree* ptr= this;
	ptr = root.diving();
	if (ptr==NULL)
	{
		return false;
	}
	while (ptr!=NULL)
	{
		ptr-&gt;doGenerate();
		ptr = ptr-&gt;findBrother();
	}

	return true;
}


bool CountTree::touchDown()
{
	return depth ==level;
}

bool CountTree::canDive()
{
	return sonCount&gt;0;
}

void CountTree::initialize()
{
	root.depth = 0;
	root.sonCount = 0;
	root.sonIndex = -1;
	root.parent = NULL;
	root.data = 0;
	level = 0;
}


//NULL only when there is no more brother
CountTree* CountTree::diving()
{	
	CountTree* ptr = this;
	while (!ptr-&gt;touchDown())
	{
		if (ptr-&gt;canDive())
		{
			ptr = ptr-&gt;sonArray[0];
		}
		else
		{
			if (ptr-&gt;hasBrother())
			{
				ptr = ptr-&gt;nextBrother();
			}
			else
			{
				ptr = ptr-&gt;findUncle();
			}
		}
		if (ptr==NULL)
		{
			break; //no more way to dive
		}
	}
	return ptr;
}

bool CountTree::hasBrother()
{
	if (parent!=NULL)
	{
		if (parent-&gt;sonCount &gt; sonIndex+1)//has little brother
		{
			return true;
		}
	}
	return false;
}

CountTree* CountTree::nextBrother()
{
	if (parent!=NULL)
	{
		if (parent-&gt;sonCount &gt; sonIndex+1)//has little brother
		{
			return parent-&gt;sonArray[sonIndex+1]; //next little brother
		}
	}
	return NULL;
}


CountTree* CountTree::findUncle()
{
	CountTree* ptr= this;
	while (!(ptr-&gt;hasBrother()))
	{
		ptr = ptr-&gt;parent;
		if (ptr==NULL)
		{
			return NULL;
		}
	}
	return ptr-&gt;nextBrother();

}

CountTree* CountTree::findBrother()
{
	CountTree* ptr = this;
	if (ptr-&gt;hasBrother())
	{
		return ptr-&gt;nextBrother();
	}
	else
	{
		ptr = ptr-&gt;findUncle();
		if (ptr==NULL)
		{
			return NULL;
		}
		else
		{
			return ptr-&gt;diving();
		}
	}	
}


void CountTree::addSon(int sonData)
{	
	if (sonCount+1&lt;MaxSonCount)
	{
		CountTree* ptr = new CountTree;
		ptr-&gt;data = sonData;
		ptr-&gt;sonCount = 0;
		ptr-&gt;parent = this;
		ptr-&gt;depth = level+1;
		sonArray[sonCount] = ptr;		
		ptr-&gt;sonIndex = sonCount;
		sonCount++;
	}
	else
	{
		cout&lt;&lt;&quot;Exceed max of sons!&quot;;
	}
}
</pre>
<pre>




			
</pre>
<pre><font color="#FF0000"><a name="Result"></a>The following is result of program: (the result is too much even with reduced searching levels)</font></pre>
<pre>...</pre>
<pre>...
</pre>
<pre>now it is:12567The route is:W, W, W, W, Y, Y, Z, Z, X, Y, 
now it is:12568The route is:W, W, W, W, Y, Y, Z, Z, Y, X, 
now it is:12569The route is:W, W, W, W, Y, Z, X, Y, Y, Z, 
now it is:12570The route is:W, W, W, W, Y, Z, X, Y, Z, Y, 
now it is:12571The route is:W, W, W, W, Y, Z, X, Z, Y, Y, 
now it is:12572The route is:W, W, W, W, Y, Z, Y, X, Y, Z, 
now it is:12573The route is:W, W, W, W, Y, Z, Y, X, Z, Y, 
now it is:12574The route is:W, W, W, W, Y, Z, Y, Y, X, Z, 
now it is:12575The route is:W, W, W, W, Y, Z, Y, Y, Z, X, 
now it is:12576The route is:W, W, W, W, Y, Z, Y, Z, X, Y, 
now it is:12577The route is:W, W, W, W, Y, Z, Y, Z, Y, X, 
now it is:12578The route is:W, W, W, W, Y, Z, Z, X, Y, Y, 
now it is:12579The route is:W, W, W, W, Y, Z, Z, Y, X, Y, 
now it is:12580The route is:W, W, W, W, Y, Z, Z, Y, Y, X, 
now it is:12581The route is:W, W, W, W, Z, X, Y, Y, Y, Z, 
now it is:12582The route is:W, W, W, W, Z, X, Y, Y, Z, Y, 
now it is:12583The route is:W, W, W, W, Z, X, Y, Z, Y, Y, 
now it is:12584The route is:W, W, W, W, Z, X, Z, Y, Y, Y, 
now it is:12585The route is:W, W, W, W, Z, Y, X, Y, Y, Z, 
now it is:12586The route is:W, W, W, W, Z, Y, X, Y, Z, Y, 
now it is:12587The route is:W, W, W, W, Z, Y, X, Z, Y, Y, 
now it is:12588The route is:W, W, W, W, Z, Y, Y, X, Y, Z, 
now it is:12589The route is:W, W, W, W, Z, Y, Y, X, Z, Y, 
now it is:12590The route is:W, W, W, W, Z, Y, Y, Y, X, Z, 
now it is:12591The route is:W, W, W, W, Z, Y, Y, Y, Z, X, 
now it is:12592The route is:W, W, W, W, Z, Y, Y, Z, X, Y, 
now it is:12593The route is:W, W, W, W, Z, Y, Y, Z, Y, X, 
now it is:12594The route is:W, W, W, W, Z, Y, Z, X, Y, Y, 
now it is:12595The route is:W, W, W, W, Z, Y, Z, Y, X, Y, 
now it is:12596The route is:W, W, W, W, Z, Y, Z, Y, Y, X, 
now it is:12597The route is:W, W, W, W, Z, Z, X, Y, Y, Y, 
now it is:12598The route is:W, W, W, W, Z, Z, Y, X, Y, Y, 
now it is:12599The route is:W, W, W, W, Z, Z, Y, Y, X, Y, 
now it is:12600The route is:W, W, W, W, Z, Z, Y, Y, Y, X, 
The total number is :12600</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>

<p>กก          


</p>

<p>กก</p>

<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>