<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; 
Consecutive 0's</span>--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 another application of my BFS class, or you can call it <span lang="en-ca">fourth</span> edition of <span lang="en-ca">BFS class</span>, whatever.</b></pre>
</div>
<div align="left">
  <pre><b>The base framework are exactly same as Counting problem except that I standardize my BFS class<span lang="en-ca"> once more</span>. I made </b></pre>
</div>
<div align="left">
  <pre><b>it similar<span lang="en-ca"> possible to inheriting! As I mentioned before, I use a static private BFS object for &quot;root&quot; which </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>destroy the OO as we cannot override a static object which is declared as BFS. All its derived class is impossible</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>to override it. Now the problem solved by declaring it only as a pointer of BFS. And when the derived class is </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>created, he call the initialize() method and pass its own pointer as parameter. Then the root is pointing to this</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>object itself!</b></span><b> <span lang="en-ca"> In order all his descendants to be sure of same class, programmer need to override method </span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">&quot;createSon&quot; which simply create a new object of descendant class.</span></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>A string that contains 0s,1s and 2s is called a ternary string. Find the number of ternary strings that contain</b></pre>
</div>
<div align="left">
  <pre><b>consecutive 0s.(<span lang="en-ca">This is the exact complement problem of <a href="CountNumber.htm">&quot;countNumber&quot;</a>.</span>)</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. This is an application of my BFS class which allow user to override several</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>key method to do the BFS. In this case, user need to override two method: createSon() and validateOption().</b></span></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. bool Isomorphic::validateOption(int sonData)</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is always the application specific method. In this case, you have to only take care the last two level. As you</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>know, if it has consecutive 0's, it either ended with consecutive 0's. Or it has consecutive 0's at previous part.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>And if it doesnot have 2 consecutive 0's, then the creating of last two level is vital. It must be 0,0. Otherwise</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>it would be impossible to have 2 consecutive 0's. I used a boolean switch like &quot;amZero&quot; to indicate if it prevous</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>part has two 0's.</b></span></pre>
</div>
<div align="left">
  <pre><b>2. BFS* Isomorphic::createSon()</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>You must override this virtual function! Otherwise there will be no inheritation!</b></span></pre>
</div>
<div align="left">
  <pre><b>3. void BFS::initialize(BFS* now)</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>I modified this method by adding a parameter. Then when the object is to begin searching, it will pass itself to </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>this method and make itself the root. Then I avoid using static object!</b></span></pre>
</div>
<pre>กก</pre>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxSonCount =8;
const int MaxLevel = 20;
const int MaxOptionCount = 10;

enum BitName
{Zero, One, Two};

char* BitString[3] = {&quot;0&quot;, &quot;1&quot;, &quot;2&quot;};

int bitOption[3] = {Zero, One, Two};

class BFS
{
protected:
	static BFS *root;
	static int level;
	static int optionArray[MaxOptionCount];
	static int optionCount;
	static int maxLevel;
	int depth;
	BFS* parent;
	BFS* sonArray[MaxSonCount];
	int data;
	int sonCount;
	int sonIndex;
	void addSon(int sonData);
	BFS* findBrother();
	BFS* findUncle();
	bool hasBrother();
	BFS* nextBrother();
	BFS* diving();
	void initialize(BFS* now);
	bool touchDown();
	bool canDive();
	bool generate();
	void traverse();
	void setMaxLevel(int max) { maxLevel = max;}
	void assignString(char** nameString);
	virtual BFS* createSon();
	virtual bool lastChance();
	virtual void doGenerate();
	virtual bool validateOption(int sonData);	
	virtual void onVisit(int&amp; counter);
public:
	int getData(){return data;}
	BFS* getParent() { return parent;}
	void setOptionArray(int* array, int arraySize);
	void beginCount(int max);
};

int BFS::level = 0;
BFS* BFS::root;

int BFS::optionArray[MaxOptionCount] = {0};
int BFS::optionCount = 0;
int BFS::maxLevel = 0;

class Isomorphic: public BFS
{
protected:
	BFS* createSon();
	bool validateOption(int sonData);
};

int main()
{
	Isomorphic R;
	R.setOptionArray(bitOption, 3);
	R.beginCount(5);
	
	return 0;
}

BFS* Isomorphic::createSon()
{
	BFS* ptr = new Isomorphic;
	return ptr;
}

bool Isomorphic::validateOption(int sonData)
{
	BFS* ptr;
	bool amZero = false;

	if (depth&gt;=maxLevel-2)
	{
		if (sonData==0&amp;&amp;getData() ==0)
		{
			return true;	
		}
		else
		{
			//it has the potential
			if (sonData==0)
			{
				return true;
			}
			//the following is to find consecutive 0's
			ptr = this;
			amZero = false; //initialize
			while (ptr!=NULL)
			{
				if (ptr-&gt;getParent()==NULL)
				{
					break;
				}
				if (amZero)  //
				{
					if (ptr-&gt;getData()==0)//means the son is 0
					{
						return true;
					}
					else
					{
						amZero = false; //reset
					}
				}
				else
				{
					if (ptr-&gt;getData() ==0) //give you a chance to replay the game
					{
						amZero = true;
					}
				}
				ptr = ptr-&gt;getParent();//next generation
			}	
			return false; //ended with no consecutive 0's
		}
	}
	return true;//if not the last last chance, why matters?
}


void BFS::setOptionArray(int *array, int arraySize)
{
	optionCount = arraySize;
	for (int i=0; i&lt; optionCount; i++)
	{
		optionArray[i] = array[i];
	}
}


BFS* BFS::createSon()
{
	BFS* ptr = new BFS;
	return ptr;
}

void BFS::assignString(char** nameString)
{
	int temp[MaxLevel] = {0};
	BFS* 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++)//depth is one more than level!!!!!!
	{
		cout&lt;&lt;nameString[temp[i]]&lt;&lt;&quot;, &quot;;
	}
	cout&lt;&lt;endl;
}


void BFS::onVisit(int&amp; counter)
{
	cout&lt;&lt;&quot;now it is:&quot;&lt;&lt;++counter;
	assignString(BitString);

}

bool BFS::validateOption(int sonData)
{
	if (sonData==Zero)
	{
		if (parent!=NULL)
		{
			if (data==Zero)
			{
				return false;
			}
		}
	}
	return true;
}

void BFS::doGenerate()
{
	for (int i=0; i&lt;optionCount; i++)
	{
		if (validateOption(optionArray[i]))
		{
			addSon(optionArray[i]);
		}
	}
}

bool BFS::lastChance()
{
	return level==maxLevel-1;
}


void BFS::traverse()
{
	int counter=0;
	BFS* ptr= root;
	while (ptr!=NULL)
	{
		while(ptr-&gt;canDive())
		{
			ptr= ptr-&gt;sonArray[0];
		}
		ptr-&gt;onVisit(counter);
		
		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 BFS::beginCount(int max)
{
	setMaxLevel(max);
	initialize(this);
	while (generate())
	{
		if (!lastChance()) //true to keep go on
		{
			level++;		
		}
		else
		{
			break;
		}
	}
	traverse();
}


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

	return true;
}


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

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

void BFS::initialize(BFS* now)
{
	root = now;
	root-&gt;depth = 0;
	root-&gt;sonCount = 0;
	root-&gt;sonIndex = -1;
	root-&gt;parent = NULL;
	root-&gt;data = 0;
	level = 0;
}


//NULL only when there is no more brother
BFS* BFS::diving()
{	
	BFS* 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 BFS::hasBrother()
{
	if (parent!=NULL)
	{
		if (parent-&gt;sonCount &gt; sonIndex+1)//has little brother
		{
			return true;
		}
	}
	return false;
}

BFS* BFS::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;
}


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

}

BFS* BFS::findBrother()
{
	BFS* 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 BFS::addSon(int sonData)
{	
	BFS* ptr; 
	if (sonCount+1&lt;MaxSonCount)
	{
		ptr = createSon();	
		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>now it is:1 The route is:0, 0, 0, 0, 0,
now it is:2 The route is:0, 0, 0, 0, 1,
now it is:3 The route is:0, 0, 0, 0, 2,
now it is:4 The route is:0, 0, 0, 1, 0,
now it is:5 The route is:0, 0, 0, 1, 1,
now it is:6 The route is:0, 0, 0, 1, 2,
now it is:7 The route is:0, 0, 0, 2, 0,
now it is:8 The route is:0, 0, 0, 2, 1,
now it is:9 The route is:0, 0, 0, 2, 2,
now it is:10 The route is:0, 0, 1, 0, 0,
now it is:11 The route is:0, 0, 1, 0, 1,
now it is:12 The route is:0, 0, 1, 0, 2,
now it is:13 The route is:0, 0, 1, 1, 0,
now it is:14 The route is:0, 0, 1, 1, 1,
now it is:15 The route is:0, 0, 1, 1, 2,
now it is:16 The route is:0, 0, 1, 2, 0,
now it is:17 The route is:0, 0, 1, 2, 1,
now it is:18 The route is:0, 0, 1, 2, 2,
now it is:19 The route is:0, 0, 2, 0, 0,
now it is:20 The route is:0, 0, 2, 0, 1,
now it is:21 The route is:0, 0, 2, 0, 2,
now it is:22 The route is:0, 0, 2, 1, 0,
now it is:23 The route is:0, 0, 2, 1, 1,
now it is:24 The route is:0, 0, 2, 1, 2,
now it is:25 The route is:0, 0, 2, 2, 0,
now it is:26 The route is:0, 0, 2, 2, 1,
now it is:27 The route is:0, 0, 2, 2, 2,
now it is:28 The route is:0, 1, 0, 0, 0,
now it is:29 The route is:0, 1, 0, 0, 1,
now it is:30 The route is:0, 1, 0, 0, 2,
now it is:31 The route is:0, 1, 1, 0, 0,
now it is:32 The route is:0, 1, 2, 0, 0,
now it is:33 The route is:0, 2, 0, 0, 0,
now it is:34 The route is:0, 2, 0, 0, 1,
now it is:35 The route is:0, 2, 0, 0, 2,
now it is:36 The route is:0, 2, 1, 0, 0,
now it is:37 The route is:0, 2, 2, 0, 0,
now it is:38 The route is:1, 0, 0, 0, 0,
now it is:39 The route is:1, 0, 0, 0, 1,
now it is:40 The route is:1, 0, 0, 0, 2,
now it is:41 The route is:1, 0, 0, 1, 0,
now it is:42 The route is:1, 0, 0, 1, 1,
now it is:43 The route is:1, 0, 0, 1, 2,
now it is:44 The route is:1, 0, 0, 2, 0,
now it is:45 The route is:1, 0, 0, 2, 1,
now it is:46 The route is:1, 0, 0, 2, 2,
now it is:47 The route is:1, 0, 1, 0, 0,
now it is:48 The route is:1, 0, 2, 0, 0,
now it is:49 The route is:1, 1, 0, 0, 0,
now it is:50 The route is:1, 1, 0, 0, 1,
now it is:51 The route is:1, 1, 0, 0, 2,
now it is:52 The route is:1, 1, 1, 0, 0,
now it is:53 The route is:1, 1, 2, 0, 0,
now it is:54 The route is:1, 2, 0, 0, 0,
now it is:55 The route is:1, 2, 0, 0, 1,
now it is:56 The route is:1, 2, 0, 0, 2,
now it is:57 The route is:1, 2, 1, 0, 0,
now it is:58 The route is:1, 2, 2, 0, 0,
now it is:59 The route is:2, 0, 0, 0, 0,
now it is:60 The route is:2, 0, 0, 0, 1,
now it is:61 The route is:2, 0, 0, 0, 2,
now it is:62 The route is:2, 0, 0, 1, 0,
now it is:63 The route is:2, 0, 0, 1, 1,
now it is:64 The route is:2, 0, 0, 1, 2,
now it is:65 The route is:2, 0, 0, 2, 0,
now it is:66 The route is:2, 0, 0, 2, 1,
now it is:67 The route is:2, 0, 0, 2, 2,
now it is:68 The route is:2, 0, 1, 0, 0,
now it is:69 The route is:2, 0, 2, 0, 0,
now it is:70 The route is:2, 1, 0, 0, 0,
now it is:71 The route is:2, 1, 0, 0, 1,
now it is:72 The route is:2, 1, 0, 0, 2,
now it is:73 The route is:2, 1, 1, 0, 0,
now it is:74 The route is:2, 1, 2, 0, 0,
now it is:75 The route is:2, 2, 0, 0, 0,
now it is:76 The route is:2, 2, 0, 0, 1,
now it is:77 The route is:2, 2, 0, 0, 2,
now it is:78 The route is:2, 2, 1, 0, 0,
now it is:79 The route is:2, 2, 2, 0, 0,
The total number is :79
Press any key to continue</pre>
<pre>กก</pre>

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