<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 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

<blockquote>

<blockquote>
  <blockquote>

<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000" size="6"><b>Code  
Competition(3)&nbsp;</b></font>    
</p>
  </blockquote>
</blockquote>
</blockquote>
<div align="left">
  <pre><b><font color="#ff0000" size="5">A.First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is actually first edition of <a href="CodingProb3.htm">Code Competition 3</a>. (Click to see the problem)</b></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><b>This is from last year of &quot;Code Competition&quot; of &quot;Concordia Computer Science Association&quot;(CCSS)</b></pre>
</div>
<div align="left">
  <pre><b>This is the second simplest one among 4 problems.(I guess that as I browse the result and most of them only </b></pre>
</div>
<div align="left">
  <pre><b>figour out</b> <b>this one and the no 2 problem.) It is actually a common job in data structure courses. I finished </b></pre>
</div>
<div align="left">
  <pre><b>after more then 3 hours. I guess I won't become the winning team as I think I am not the kind of coding fast.</b></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><b>Actually it is not a very difficult problem compared with &quot;Baggles&quot; which I coded with Delphi in Toronto which</b></pre>
</div>
<div align="left">
  <pre><b>is the assignment of &quot;University York&quot;. It is also said to be originally from &quot;MIT&quot; or &quot;Standford&quot; or somewhere.</b></pre>
</div>
<div align="left">
  <pre><b>That problem is actually a small project which involes &quot;lexico&quot;, &quot;depth first search&quot;, and other data structure</b></pre>
</div>
<div align="left">
  <pre><b>skills. I only finished these two parts. Stop this!</b></pre>
</div>
<div align="left">
  <pre><b>Let's talk about something of problem. The input quite cost a lot of job. I made a mistake when reading number</b></pre>
</div>
<div align="left">
  <pre><b>with more than 1 digit. I used a recursive function &quot;doSearch()&quot; which move to new position by original &quot;direction&quot;</b></pre>
</div>
<div align="left">
  <pre><b>and &quot;word&quot; with first letter &quot;missing&quot;. I just move the pointer of &quot;word&quot; by 1. The recursive function doSearch</b></pre>
</div>
<div align="left">
  <pre><b>return write a &quot;Pos&quot; struct to a &quot;Pos*&quot; array when it find out the &quot;word&quot; is only one letter left.</b></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function£º</b></pre>
</div>
<div style="WIDTH: 892px; HEIGHT: 102px" align="left">
  <pre><b>     A. </b>void inputFile(char* fileName)</pre>
  <pre>	This function is quite long as I didn't expect to add so many parts in together. First it read in the &quot;dimention&quot; of </pre>
  <pre>array which give some trouble as I only expect one digit number. Then it just read the length of a word first then read in the </pre>
  <pre>word until read 0;</pre>
  <pre><b>     B. </b>void searchWord(char* word)</pre>
  <pre>	This function first find the pos match the first char of &quot;word&quot; and then call &quot;scanWord&quot;.</pre>
  <pre>	C.  void scanWord(int i, int j, char* word)</pre>
  <pre>	   void doSearch(int i, int j, char* pWord, Direction dir);</pre>
  <pre>	within &quot;scanWord&quot; there is the recursive function of &quot;doSearch&quot; which &quot;move&quot; from current co-ordinate(i,j) to next position</pre>
  <pre>calculated by &quot;Direction&quot;---dir. The function &quot;testDir&quot; test next position also return next destination co-ordinate. Then</pre>
  <pre>the function just pass new co-ordinate, original direction, and &quot;word&quot; with first char &quot;missing&quot;(move the pointer by 1) to </pre>
  <pre>recursive call itself. &quot;doSearch&quot; stop to add new struct of &quot;Pos&quot; to a list when he found the &quot;word&quot; is only one char.</pre>
  <pre>	D. bool testDir(int&amp; row, int&amp; col, Direction dir)</pre>
  <pre>	A standard moving test function. I think it is exactly same as my old &quot;Delphi&quot; function which I lost when my old laptop</pre>
  <pre>choose to suitcide.</pre>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. This simple program cost me almost 3 hours.	</b></pre>
  <pre><b><font color="#FF0000"><i>	</i></font>B. I forgot to delete dynamically allocated memory at end of execution.</b></pre>
  <pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;</pre>
  <pre>using namespace std;</pre>
  <pre>enum Direction
{LeftUp, Up, RightUp, Right, RightDown, Down, LeftDown, Left};</pre>
  <pre>const MaxDim = 100;</pre>
  <pre>const MaxWordCount = 100;</pre>
  <pre>const MaxRec = 200;</pre>
  <pre>struct Pos
{
	char* word;
	int startRow;
	int startCol;
	int endRow;
	int endCol;
};</pre>
  <pre>Pos* record[MaxRec];</pre>
  <pre>int recordCount =0;</pre>
  <pre>Pos start;

</pre>
  <pre>char * words[MaxWordCount];</pre>
  <pre>char matrix[MaxDim][MaxDim];</pre>
  <pre>int dim = 0;</pre>
  <pre>int wordCount =0;</pre>
  <pre>void inputFile(char* fileName);</pre>
  <pre>void outputFile(char * fileName);</pre>
  <pre>void searchWord(char* word);</pre>
  <pre>void scanWord(int i, int j, char* word);</pre>
  <pre>bool testDir(int&amp; row, int&amp; col, Direction dir);</pre>
  <pre>int main()
{
	inputFile(&quot;c:\\nickinput.txt&quot;);
	outputFile(&quot;c:\\nickoutput.txt&quot;);
	return 0;
}</pre>
  <pre>void outputFile(char* fileName)
{
	ofstream f;
	f.open(fileName);
	for (int i=0; i&lt;recordCount; i++)
	{
		f&lt;&lt;record[i]-&gt;word&lt;&lt;&quot; &quot;&lt;&lt;record[i]-&gt;startRow&lt;&lt;&quot; &quot;&lt;&lt;record[i]-&gt;startCol;
		f&lt;&lt;&quot; &quot;&lt;&lt;record[i]-&gt;endRow&lt;&lt;&quot; &quot;&lt;&lt;record[i]-&gt;endCol&lt;&lt;&quot;\n&quot;;
	}
}</pre>
  <pre>void inputFile(char* fileName)
{
	FILE* stream;
	char buffer[20];
	char * ptr;
	char ch;
	int len=0;
	stream = fopen(fileName, &quot;r&quot;);
	ch = fgetc(stream);</pre>
  <pre>	while (isdigit(ch))
	{
		dim = 10 * dim + atoi(&amp;ch);
		ch = fgetc(stream);
	}
	</pre>
  <pre>	for (int i=0; i&lt; dim; i++)
	{
		for (int j=0; j&lt;dim; j++)
		{
			matrix[i][j] = ch;
			ch = fgetc(stream);
		}
	}
	while (!isalnum(ch))
	{
		ch = fgetc(stream);   //eat non alpha number
	}
	while(true)
	{
		len =0;		
		while(isdigit(ch))
		{			
			len = len*10 + atoi(&amp;ch);
			ch = fgetc(stream);
		}
		if (len==0)
			break;
		ptr = buffer;
		for (i=0; i&lt; len; i++)
		{
			*ptr = ch;
			ch = fgetc(stream);
			ptr++;
		}
		*ptr = '\0';
		words[wordCount] = (char*)malloc(len+1);
		strcpy(words[wordCount], buffer);
		wordCount++;			
		</pre>
  <pre>	}
	for (i=0; i&lt; wordCount; i++)
	{
		searchWord(words[i]);
	}
}</pre>
  <pre>bool testDir(int&amp; row, int&amp; col, Direction dir)
{
	int i = row, j = col;
	switch(dir)
	{
	case LeftUp:
		i--;
		j--;
		break;
	case Up:
		i--;
		break;
	case RightUp:
		i--;
		j++;
		break;
	case Right:
		j++;
		break;
	case RightDown:
		i++;
		j++;
		break;
	case Down:
		i++;
		break;
	case LeftDown:
		i++;
		j--;
		break;
	case Left:
		j--;
		break;
	}
	if (i&lt;0||j&lt;0||i&gt;=dim||j&gt;=dim)
	{
		return false;
	}
	else
	{
		row = i;
		col = j;
		return true;
	}
}


</pre>
  <pre>void searchWord(char* word)
{
	for (int i =0; i&lt;dim; i++)
	{
		for (int j=0; j&lt; dim; j++)
		{
			if (matrix[i][j]==*word)
			{
				scanWord(i, j, word);
			}
		}
	}
}</pre>
  <pre>void doSearch(int i, int j, char* pWord, Direction dir)
{
	if (*pWord == matrix[i][j])
	{
		if (strlen(pWord)==1)
		{
			Pos * ptr = new Pos;
			ptr-&gt;word = start.word;
			ptr-&gt;startRow = start.startRow;
			ptr-&gt;startCol = start.startCol;
			ptr-&gt;endRow = i;
			ptr-&gt;endCol = j;
			record[recordCount] = ptr;
			recordCount++;
			return;
		}
		else
		{
			if (testDir(i, j, dir))
			{
				doSearch(i, j, pWord+1, dir);
			}
		}
	}	
}
</pre>
  <pre>void scanWord(int i, int j, char* word)
{
	int row = i, col = j;
	char *ptr = word;
	void doSearch(int i, int j, char* pWord, Direction dir);</pre>
  <pre>	for (int dir = LeftUp; dir&lt;= Left; dir++)
	{
		start.word = word;
		ptr = word;
		row = i;
		col = j;
		start.startRow = i;
		start.startCol = j;
		doSearch(row, col, ptr, (Direction)dir);
	}
}</pre>
</div>
<p><font color="#0000FF">This is input file contents:</font>          


</p>

<p><font color="#FF0000">21<br>
trihsyffupjsizanpuoscjracyllemsoxcdtkqkhprsulhecrlnvnhoomtnveeadinjakqtkhvacesriabnzyarixtvxvnozktltqprfykglbojvcwuiznjveacrf<br>
jnonaerbeetgiarsclnkmokmnishmcvthpyefdaczyeyscahgtirctsangromnadoomjmtignnsedndndvxuakchttrobhtciaeeckllbvpuddyyenwdapyrbweqx<br>
rozorbukwttprpabazmkkolkjccmcgpeuqodnnbrafragvbqgimltpopaiisrnntnehwjbvfitdjbyeacrtwsiooizdvwxcdtuatrnnxsekrsbbzfhvukxmdsbsnn<br>
eaxgqigafcbffenaqvperzmbegaknirhswmyekardehteelainebenesrsxkramerm<br>
7maestro12yadayadayada8soupnazi5cosmo11elainebenes8costanza6kramer12aboutnothing5frank6george6newman14crazyjoedavola8<br>
uncleleo9smellycar10juniormint8thedrake10puffyshirt7thebris12poppiespizza6mickey9shrinkage8bigsalad5bania13jonvoightscar6<br>
mrpitt5puddy12steinbrenner9jpeterman0</font>           


</p>

<p><font color="#0000FF">This is output file contents:</font>          


</p>

<p><font color="#FF0000">maestro 18 19 12 19<br> 
yadayadayada 7 12 18 12<br> 
soupnazi 0 19 0 12<br> 
cosmo 10 1 6 1<br> 
elainebenes 20 0 20 10<br> 
costanza 1 12 8 19<br> 
kramer 20 14 20 19<br> 
aboutnothing 16 12 5 1<br> 
frank 5 19 1 15<br> 
george 14 0 19 0<br> 
newman 9 13 14 18<br> 
crazyjoedavola 0 20 13 20<br> 
uncleleo 9 19 2 12<br> 
smellycar 1 9 1 1<br> 
juniormint 1 0 10 9<br> 
thedrake 19 19 19 12<br> 
puffyshirt 0 9 0 0<br> 
thebris 8 6 2 0<br> 
poppiespizza 14 11 3 11<br> 
mickey 14 6 19 11<br> 
shrinkage 19 8 19 0<br> 
bigsalad 10 7 3 0<br> 
bania 11 14 15 14<br> 
jonvoightscar 0 10 12 10<br> 
mrpitt 6 19 1 14<br> 
puddy 11 0 11 4<br> 
steinbrenner 17 13 6 13<br> 
jpeterman 15 10 7 2<br> 
</font>          


</p>

<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;&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="CodeCompetition2.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; 
<a href="CodeCompetition4.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
