<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>labyrith</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
<b>&nbsp;<span lang="en-ca">&nbsp;&nbsp; </span>Snake, Snake, Snake</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. Fifth<span lang="en-ca">?</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is the continued struggle for me to solve 15 puzzle. The most efforts of mine is to construct</font></b></pre>
</div>
<div align="left">
  <pre><font size="3"><b>a snake which can eat node and grow by swim around board. But I have to admit this idea is dead as</b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b>snake will be stuck when the dimension of board becomes too small. So, it is useless in solving 15</b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b>puzzle. I have given up this approach.</b></font></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">
  <p ALIGN="LEFT"><span lang="en-ca">The 15 puzzle? Or the path for a node to 
  move? Or the path for a cluster of number to move, like snake? Or the</span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">number cluster itself?</span></div>
<div align="left">
  <p ALIGN="LEFT">　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>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">
  　</div>
<div align="left">
  <pre><span lang="en-ca">Since I have to memorize all coordinate I have been in the labyrinth, there is no better way than a simple DFS. I would not</span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">bother to find any algorithm, as all algorithms require you to memorize the position you have been to.</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>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">The next step is to implement the move a single node on board, then the whole cluster move.</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left" style="width: 898; height: 86">
  <pre><font size="3"><b><span lang="en-ca">1. node</span>.</b></font><span lang="en-ca"><font size="3"><b>h</b></font></span></pre>
  <pre><span lang="en-ca"><font size="3"><b>2</b></font></span><font size="3"><b><span lang="en-ca">. node</span>.cpp</b></font></pre>
  <pre><span lang="en-ca"><font size="3"><b>3</b></font></span><font size="3"><b><span lang="en-ca">. cluster</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre><span lang="en-ca"><font size="3"><b>4</b></font></span><font size="3"><b><span lang="en-ca">. cluster</span>.cpp</b></font></pre>
  <pre><span lang="en-ca"><font size="3"><b>5</b></font></span><font size="3"><b><span lang="en-ca">. board</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre><span lang="en-ca"><font size="3"><b>6</b></font></span><font size="3"><b><span lang="en-ca">. board</span>.</b></font><span lang="en-ca"><font size="3"><b>cpp</b></font></span></pre>
  <pre><font size="3"><b><span lang="en-ca">7. main</span>.<span lang="en-ca">cpp</span></b></font></pre>
  <pre>　</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: node</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre>#ifndef NODE_H
#define NODE_H

#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

const int BoardSize=4;
const int MaxNodesLength=BoardSize*4;
//const int MaxBoardSize=BoardSize*4;
const int MinimumRotateSize=2;

//#define abs(x) (x&gt;=0?x:-x)


enum Direction
{
	U, R , D , L, None 
};

enum Position
{
	Right, DownRight, Down, DownLeft, Left, UpLeft, Up, UpRight
};

//class Puzzle;

class Coord
{
	friend class Board;
private:
	int row;
	int col;	
public:
	const Coord&amp; operator=(const Coord&amp; rhs);
	bool operator==(const Coord&amp; rhs) const;
	bool operator!=(const Coord&amp; rhs) const ;
	bool neighbour(const Coord&amp; rhs) const;
	void move(Direction dir);
	Coord(int r=-1, int c=-1);
};



#endif</pre>
  <pre>　</pre>
  <pre>　</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: node</span>.</b></font><span lang="en-ca"><font size="3" color="#FF0000"><b>cpp</b></font></span></pre>
  <pre>#include &quot;node.h&quot;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

char dirStr[4]={'U', 'D' , 'L' , 'R'}; 

Coord::Coord(int r, int c)
{
	row=r;
	col=c;
}

void Coord::move(Direction dir)
{
	switch (dir)
	{
	case U:
		row--;
		break;
	case D:
		row++;
		break;
	case L:
		col--;
		break;
	case R:
		col++;
		break;
	}
}


//assignment of coord
const Coord&amp; Coord::operator =(const Coord&amp; rhs)
{
	row=rhs.row;
	col=rhs.col;
	return *this;
}

bool Coord::operator !=(const Coord&amp; rhs) const
{
	return !(this-&gt;operator==(rhs));
}

bool Coord::operator ==(const Coord&amp; rhs) const
{
	if (row==rhs.row&amp;&amp;col==rhs.col)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Coord::neighbour(const Coord&amp; rhs) const
{
	//next row
	if (abs(row-rhs.row)==1)
	{
		return col==rhs.col;
	}
	else
	{
		//same row
		if (row==rhs.row)
		{
			//next col
			return abs(col-rhs.col)==1;
		}
		else
		{
			//nothing
			return false;
		}
	}
}
	

</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: cluster</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre>#include &lt;stdio.h&gt;
#include &quot;node.h&quot;


class Cluster
{
private:
	int length;
	Coord nodes[MaxNodesLength];
	//bool ascending;
	int head;//this is the head pos, as head is moving
	void initialize();
public:
	int getLength(){return length;}
	void clear();
	const Coord getHead();
	bool step(const Coord&amp; newPos);//must be a valid move
	bool step(Direction dir);
	bool addTail(const Coord&amp; newTail);
	bool isNode(const Coord&amp; rhs);
	bool addHead(const Coord&amp; newHead);
	bool removeHead();
	bool removeTail();
	bool setHead(const Coord&amp; first);
	void swapHeadTail();
	Coord operator[](int index) const;
	Coord&amp; operator[](int index);
	int index(const Coord&amp; pos) const;
	Cluster();
	Cluster&amp; operator=(const Cluster&amp; dummy);
};



</pre>
  <pre>

<font size="3" color="#FF0000"><b><span lang="en-ca">file name: cluster</span>.<span lang="en-ca">cpp</span></b></font></pre>
  <pre>#include &quot;cluster.h&quot;
#include &lt;math.h&gt;


const Coord Cluster::getHead()
{
	return nodes[head];
}

Cluster&amp; Cluster::operator =(const Cluster&amp; dummy)
{
	initialize();
	for (int i=0; i&lt;dummy.length; i++)
	{
		addTail(dummy[i]);
	}	
	return *this;
}

void Cluster::swapHeadTail()
{
	Coord hold;
	int step=-1, headIndex, tailIndex;
	do
	{
		step++;
		if (step&gt;length/2)
		{
			break;
		}
		headIndex=head-step&gt;=0?head-step:head-step+MaxNodesLength;
		tailIndex=(head+length-step)%MaxNodesLength;
		hold=nodes[headIndex];
		nodes[headIndex]=nodes[tailIndex];
		nodes[tailIndex]=hold;

	}while (nodes[headIndex]!=nodes[tailIndex]);
}




bool Cluster::removeTail()
{
	if (length==0)
	{
		return false;
	}
	else
	{
		length--;
		if (length==0)
		{
			head=-1;
		}	
	}
	return true;
}

bool Cluster::removeHead()
{
	if (length==0)
	{
		return false;
	}
	else
	{
		length--;
		if (length==0)
		{
			head=-1;
		}
		else
		{
			head=(head+1)%MaxNodesLength;	
		}
	}
	return true;
}

void Cluster::clear()
{
	initialize();
}

bool Cluster::step(Direction dir)
{
	Coord dummy;
	dummy=nodes[head];
	dummy.move(dir);
	return step(dummy);
}

//valid for two meanings:
//1. not eat any node
//2. adjacent to head
bool Cluster::step(const Coord&amp; newPos)
{
	//don't bite your tail or body
	if (isNode(newPos))
	{
		return false;
	}
	//can you bite too far?
	if (!nodes[head].neighbour(newPos))
	{
		return false;
	}
	head--;
	if (head&lt;0)
	{
		head+=MaxNodesLength;
	}
	nodes[head]=newPos;
	return true;
}

bool Cluster::isNode(const Coord&amp; rhs)
{
	for (int i=0; i&lt;length; i++)
	{
		if (this-&gt;operator[](i)==rhs)
		{
			return true;
		}
	}
	return false;
}

void Cluster::initialize()
{
	head=-1;
	length=0;
}

Cluster::Cluster()
{
	initialize();
}


//unless it is first time or cleared, you cannot set head
//when it already has one
bool Cluster::setHead(const Coord&amp; first)
{
	if (head!=-1)
	{
		return false;
	}
	head=0;
	length=1;
	nodes[head]=first;
	return true;
}

bool Cluster::addTail(const Coord&amp; newTail)
{
	if (length==0)
	{
		setHead(newTail);
		return true;
	}
	int tail;
	if (length==MaxNodesLength)
	{
		return false;
	}
	if (isNode(newTail))
	{
		return false;
	}
	/*
	if (!nodes[tail].neighbour(newTail))
	{
		return false;
	}
	*/
	tail=(head+length)%MaxNodesLength;
	length++;	
	nodes[tail]=newTail;
	return true;
}

int Cluster::index(const Coord&amp; pos) const
{
	for (int i=0; i&lt;length; i++)
	{
		if (this-&gt;operator [](i)==pos)
		{
			return i;
		}
	}
	return -1;
}


bool Cluster::addHead(const Coord&amp; newHead)
{
	if (length==0)
	{
		setHead(newHead);
		return true;
	}
	if (length==MaxNodesLength)
	{
		return false;
	}
	if (isNode(newHead))
	{
		return false;
	}
	if (!nodes[head].neighbour(newHead))
	{
		return false;
	}
	length++;
	head--;
	if (head&lt;0)
	{
		head+=MaxNodesLength;
	}
	
	nodes[head]=newHead;
	return true;
}


Coord&amp; Cluster::operator [](int index)
{
	return nodes[(head+index)%MaxNodesLength];
}

Coord Cluster::operator [](int index) const
{
	return nodes[(head+index)%MaxNodesLength];
}</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: board</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre>#include &quot;cluster.h&quot;
#include &quot;node.h&quot;

const MaxFrameLength=BoardSize*2-1;

class Board
{
	friend class Cluster;
	//friend class Solve;
private:
	int board [BoardSize][BoardSize];
	bool map[BoardSize][BoardSize];
	bool restrict[BoardSize][BoardSize];
	Cluster path;

	int size;
	void initialize();
	bool valid(const Coord&amp; pos);
	Direction findDir(const Coord&amp; start, const Coord&amp; end);
	//Direction findNextDir(const Coord&amp; curr, Direction dir);
	bool impasse(const Coord&amp; curr, Direction dir);
	bool doPathFinding(const Coord&amp; curr, const Coord&amp; end, Direction dir);
	//the below are for puzzle
	int frame[MaxFrameLength];
	int frameLength;
	Cluster spacePath;
	Cluster nodePath;
	Cluster snakePath;
	Cluster snake;
	Cluster framePath;
	Coord space;
	bool moveNode(Coord&amp; current, const Coord&amp; dest);
	bool getSpacePath(const Coord&amp; dest);
	bool moveSpace(const Coord&amp; dest);
	void swapNode(Coord&amp; target);//the other one is always the space
	void snakeGrow(const Coord&amp; newHead);
	void setFrame(int level);//set number string
	void makeSnake(int level);
	Coord findNode(int value);
	bool snakeSwim(const Coord&amp; dest);
	void snakeStep(const Coord&amp; dest);
	bool snakeBite(const Coord&amp; dest);
	bool snakeSwim();
	bool canStep(const Coord&amp; dest);
	void showSnake(const Coord&amp; dest);
public:
	Board(int theSize=BoardSize);
	bool solve();
	void display();
	void displayPath(bool showPath=true);
	bool pathFinding(const Coord&amp; start, const Coord&amp; end);
	//bool pathFinding(const Coord&amp; end);
	bool pathFinding2(const Coord&amp; start, const Coord&amp; end);
	void setRestrict(const Coord&amp; pos);
	void resetRestrict(const Coord&amp; pos);
};


</pre>
  <pre>
</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: board</span>.<span lang="en-ca">cpp</span></b></font></pre>
  <pre>#include &quot;board.h&quot;



int settings[BoardSize][BoardSize]=
{
	{1, 5,   7,  3 },
	{4, 10,   0,  2 }, 
	{8, 11,   14, 9},
	{12, 13, 6,  15}
};

void Board::swapNode(Coord&amp; target)
{
	int hold;
	bool bHold;
	Coord temp;
	bHold=restrict[target.row][target.col];
	restrict[target.row][target.col]=restrict[space.row][space.col];
	restrict[space.row][space.col]=bHold;
	hold=board[space.row][space.col];//0
	board[space.row][space.col]=board[target.row][target.col];
	board[target.row][target.col]=hold;
	
	temp=space;
	space=target;
	target=temp;
}

bool Board::getSpacePath(const Coord&amp; dest)
{
	if (pathFinding(space, dest))
	{
		spacePath=path;
		spacePath.addHead(dest);
		return true;
	}
	else
	{
		printf(&quot;error! cannot find space path for (%d,%d)\n&quot;, dest.row, dest.col);
		return false;
	}
}

bool Board::moveSpace(const Coord&amp; dest)
{
	Coord curr;
	if (getSpacePath(dest))
	{
		for (int i=spacePath.getLength()-1; i&gt;=0; i--)
		{
			curr=spacePath[i];
			swapNode(curr);
		}
		//path does not include the start and dest
		//swapNode(dest);
		return true;
	}
	return false;
}


bool Board::moveNode(Coord&amp; current, const Coord&amp; dest)
{
	Coord curr;
	restrict[current.row][current.col]=true;
	if (pathFinding(current, dest))
	{
		//pathFinding didn't give you the full path, it excludes the dest
		nodePath=path;
		nodePath.addHead(dest);
		for (int i=nodePath.getLength()-1; i&gt;=0; i--)
		{
			curr=nodePath[i];
			moveSpace(curr);
			swapNode(current);
		}
		return true;
	}
	return false;
}


void Board::setFrame(int level)
{
	/*
	int r=level, c=0;
	Coord curr;
	frameLength=size;
	framePath.clear();
	for (int i=0; i&lt;frameLength; i++)
	{
		frame[i]=r*size+c;
		curr.row=r;
		curr.col=c;
		framePath.addTail(curr);
	}
*/
	
	int r=0, c=level;
	Coord curr;
	//bool begin=false;
	frameLength=(level+1)*2-1;
	framePath.clear();
	for (int i=0; i&lt;frameLength; i++)
	{
		frame[i]=r*size+c;
		curr.row=r;
		curr.col=c;
		framePath.addTail(curr);
		if (r&lt;level)
		{
			r++;
		}
		else
		{			
			c--;
		}
	}
	
}


void Board::displayPath(bool showPath)
{
	Coord curr;
	int index;
	for (int i=0; i&lt;size; i++)
	{
		for (int j=0; j&lt;size; j++)
		{
		
			if (restrict[i][j])
			{
				printf(&quot;%c%c\t&quot;, '@','@');
			}
			else
			{
				curr.row=i;
				curr.col=j;
				index=path.index(curr);
				if (index!=-1)
				{
					if (showPath)
					{
						printf(&quot;%02d\t&quot;, index);
					}
					else
					{
						printf(&quot;%02d\t&quot;, board[i][j]);
					}
				}
				else
				{
					printf(&quot;%c%c\t&quot;, '*', '*');
				}
			}
		}
		printf(&quot;\n&quot;);
	}
}

Coord Board::findNode(int value)
{
	Coord result;
	for (int i=0; i&lt;size; i++)
	{
		for (int j=0; j&lt;size; j++)
		{
			if (board[i][j]==value)
			{
				result.row=i;
				result.col=j;
				break;
			}
		}
	}
	return result;
}


void Board::snakeGrow(const Coord&amp; newHead)
{
	snake.addHead(newHead);
	restrict[newHead.row][newHead.col]=true;
}

bool Board::snakeBite(const Coord&amp; dest)
{
	Coord curr;
	bool canMove=false;
	restrict[dest.row][dest.col]=true;
	if (!snakeSwim(dest))
	{
		while (true)
		{
			canMove=false;
			for (int i=0; i&lt;4; i++)
			{
				curr=snake[0];
				curr.move((Direction)i);
				if (!snake.isNode(curr))
				{
					canMove=true;
					snakeStep(curr);
					if (snakeSwim(dest))
					{
						snake.addHead(dest);
						return true;
					}
					
				}
			}
			if (!canMove)
			{
				return false;
			}
		}
	}
	else
	{

		//swim ok
		snake.addHead(dest);
		return true;
	}
	//showSnake(dest);
}

void Board::showSnake(const Coord&amp; dest)
{
	printf(&quot;snake want to bite (%d,%d)\n&quot;, dest.row, dest.col);
	display();
}



bool Board::solve()
{
	makeSnake(size-1);
	return true;
	
}


void Board::makeSnake(int level)
{
	Coord curr, newHead, dest;
	snake.clear();
	setFrame(level);
	curr=findNode(frame[0]);
	
	//snake.addHead(curr);
	snakeGrow(curr);
	newHead=snake[0];//the head
	//all other pieces must swim to head
	for (int i=1; i&lt;frameLength; i++)
	{
		curr=findNode(frame[i]);
		//for test
		showSnake(curr);
		if (!snakeBite(curr))
		{
			printf(&quot;snake said,\&quot;I am stuck\&quot;\n&quot;);
		}


		/*
		if (pathFinding(curr, newHead))
		{
			//nodePath=path;
			//dest=nodePath[0];
			dest=path[0];//only need the head which is the last pos
			moveNode(curr, dest);//curr is moving
			//snake.addHead(curr);
			snakeGrow(curr);


		}
		else
		{
			//we may need to move snake a bit
			//snakeSwim();//???where to 
			//printf(&quot;can this really happen? I mean the length of snake is reduced\n&quot;);
			snakeSwim();
		}
		*/
	}
}

bool Board::snakeSwim(const Coord&amp; dest)
{
	Coord theHead;
	//showSnake(dest);
	if (snake.isNode(dest))
	{
		printf(&quot;snake said :\&quot;cannot bite myself!\&quot;\n&quot;);
		return false;
	}
	else
	{
		theHead=snake[0];
		if (pathFinding(theHead, dest))
		{
			snakePath=path;
			//snakePath.addHead(dest);
			for (int i=snakePath.getLength()-1; i&gt;=0; i--)
			{
				snakeStep(snakePath[i]);
			}
			return true;
		}
		return false;
	}
}

void Board::snakeStep(const Coord&amp; dest)
{
	moveSpace(dest);
	for (int i=0; i&lt;snake.getLength(); i++)
	{
		swapNode(snake[i]);//restrict is changed 
	}
}




//this is the rather direct path
bool Board::pathFinding(const Coord&amp; start, const Coord&amp; end)
{
	Coord curr;
	Direction dir;
	path.clear();
	if (start==end)
	{
		return true;
	}
	//path.setHead(start);
	//curr=path.getHead();
	curr=start;
	dir=findDir(curr, end);
	curr.move(dir);
	while (curr!=end)
	{			
		if (valid(curr))
		{
			path.addHead(curr);
			//curr=path.getHead();//??not necessary
		}
		else
		{
			//it must be something restricted and it is required 
			//a more generate way of finding path
			for (int i=0; i&lt;size; i++)
			{
				for (int j=0; j&lt;size; j++)
				{
					map[i][j]=false;
				}
			}
			map[start.row][start.col]=true;//I am here!!!
			path.clear();//restart!!
			//path.setHead(start);//do not include start itself
			return pathFinding2(start, end);
		}
		dir=findDir(curr, end);
		curr.move(dir);		
	}
	return true;
}

bool Board::pathFinding2(const Coord&amp; start, const Coord&amp; end)
{
	Coord curr;

	for (int dir=0; dir&lt;4; dir++)
	{
		curr=start;
		curr.move((Direction)dir);
		if (curr==end)
		{
			return true;
		}
		if (valid(curr))
		{
			if (!map[curr.row][curr.col])
			{
				path.addHead(curr);
				map[curr.row][curr.col]=true;
				if (pathFinding2(curr, end))
				{
					return true;
				}
				else
				{
					path.removeHead();
					//anyway, I have been here before
					//map[curr.row][curr.col]=false;
				}
			}
		}	
	}
	return false;
}


void Board::setRestrict(const Coord&amp; pos)
{
	restrict[pos.row][pos.col]=true;
}

void Board::resetRestrict(const Coord&amp; pos)
{
	restrict[pos.row][pos.col]=false;
}

bool Board::impasse(const Coord&amp; curr, Direction dir)
{
	Coord dummy;
	dummy=curr;
	dummy.move(dir);
	return valid(dummy);
}

/*
//this way has a fatal problem of circling
Direction Board::findNextDir(const Coord&amp; curr, Direction dir)
{
	Direction result=dir, turnLeft;
	//first try old direction,
	while (impasse(curr, result))
	{
		//cannot advance, so turn right, or next dir
		result=(Direction)((result+1)%4);
	}
	if (result!=dir)//means we already turned to right, so, return
	{
		return result;
	}
	else
	{
		//we must turn to left BEFORE we run into wall
		turnLeft=result;//initialize
		do 
		{
			result=turnLeft;//must!!!
			turnLeft=(Direction)(result-1&lt;0?3:result-1);
		}while (!impasse(curr, turnLeft));
		return result;//result is the dir before turning left
	}
}
	


bool Board::pathFinding(const Coord&amp; end)
{
	Coord curr;
	Direction dir;
	curr=path.getHead();
	//in what case, should I return false? a dead end?
	dir=findDir(curr, end);
	while (curr!=end)
	{
		dir=findNextDir(curr, dir);
		curr.move(dir);
		path.addHead(curr);
		curr=path.getHead();
	}
	return true;
}

*/

Direction Board::findDir(const Coord&amp; start, const Coord&amp; end)
{
	int rOff, cOff;
	rOff=end.row-start.row;
	cOff=end.col-start.col;
	if (abs(rOff)&gt;abs(cOff))
	{
		return rOff&gt;0?D:U;
	}
	else
	{
		return cOff&gt;0?R:L;
	}
}


Board::Board(int theSize)
{
	size=theSize;
	initialize();

}

bool Board::valid(const Coord&amp; pos)
{
	if (pos.col&lt;0||pos.row&lt;0||pos.col&gt;=size||pos.row&gt;=size)
	{
		return false;
	}
	return !restrict[pos.row][pos.col];
}


void Board::display()
{
	Coord curr;
	printf(&quot;the size of board is %d\n&quot;, size);
	for (int i=0; i&lt;size; i++)
	{
		for(int j=0; j&lt;size; j++)
		{
			curr.row=i;
			curr.col=j;
			if (snake.isNode(curr))
			{
				printf(&quot;%6d%c&quot;, board[i][j], '*');
			}
			else
			{
				printf(&quot;%6d&quot;, board[i][j]);
			}
		}
		printf(&quot;\n&quot;);
	}
}

void Board::initialize()
{
	for (int i=0; i&lt;size; i++)
	{
		for (int j=0; j&lt;size; j++)
		{
			board[i][j]=settings[i][j];
			restrict[i][j]=false;
			if (board[i][j]==0)
			{
				space.row=i;
				space.col=j;
			}
		}
	}
	snake.clear();
	path.clear();
	nodePath.clear();
	framePath.clear();
	snakePath.clear();
}
		
</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: main</span>.<span lang="en-ca">cpp</span></b></font></pre>
</div>
<pre>#include &lt;stdio.h&gt;
#include &quot;board.h&quot;
#include &quot;node.h&quot;



int main()
{
	Board B;
	Coord start(0,3), end(3,2), temp(2,2), temp1(3,3), temp2(1,2), temp3(1,1);
	B.display();
	/*
	B.setRestrict(temp);
	B.setRestrict(temp1);
	B.setRestrict(temp2);
	B.setRestrict(temp3);
	if (B.pathFinding(start, end))
	{
		B.displayPath();
	}
	else
	{
		printf(&quot;no way!\n&quot;);
	}
	*/
	B.solve();
	return 0;
}</pre>
<pre></pre>
<pre><b><font color="#0000FF"><span lang="en-ca"><a name="result"></a>The result is like following:</span></font></b></pre>

<pre><font color="#0000FF"><b>The following is a part of debug result and pls note that when snake wants to bite node (0,1) which has</b></font></pre>

<pre><font color="#0000FF"><b>value of 13, it gets stuck because the '0' is at (0,0) and snake head is at (1,0).  The head of snake and</b></font></pre>

<pre><font color="#0000FF"><b>its target together stop &quot;0&quot; from moving!</b></font></pre>

<pre>　</pre>

<pre>the size of board is 4
     1     5     7     3
     4    10     0     2
     8    11    14     9
    12    13     6    15
snake want to bite (0,2)
the size of board is 4
     1     5     7     3*
     4    10     0     2
     8    11    14     9
    12    13     6    15
snake want to bite (2,1)
the size of board is 4
     1     5     7*     3*
     4    10     0     2
     8    11    14     9
    12    13     6    15
snake want to bite (2,3)
the size of board is 4
     5    10     0     2
     1     7*     3*     9
     4    11*    14    15
     8    12    13     6
snake want to bite (3,2)
the size of board is 4
     1     5    10     2
     4     3*     0     9
     8     7*    11*    15*
    12    13    14     6
snake want to bite (0,1)
the size of board is 4
     0    13     9     2
     3*    12    10     5
     7*     8     1     4
    11*    15*    14*     6
error! cannot find space path for (2,2)
snake want to bite (1,0)
the size of board is 4
    11*    13*     9     2
    12    14*    15*     5
     8    10     1     4
     0     3*     7*     6
</pre>

<pre>　</pre>

<pre><span lang="en-ca">				</span> <a href="game24.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"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></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;</p>

</body>

</html>