当前位置: 首页>>代码示例>>C++>>正文


C++ Maze::Goal方法代码示例

本文整理汇总了C++中Maze::Goal方法的典型用法代码示例。如果您正苦于以下问题:C++ Maze::Goal方法的具体用法?C++ Maze::Goal怎么用?C++ Maze::Goal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Maze的用法示例。


在下文中一共展示了Maze::Goal方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SolveMaze

/*----- S o l v e M a z e (  ) ------------------------------

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
-------------------------------------------------------------*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
	maze.Mark(maze.Start(), 0);					// Mark the maze start with distance 0
	positionQueue.Enqueue(maze.Start());		// Add maze start to queue
	CellState distance = 0;						// cell distance from start

	while (!positionQueue.Empty())
	{
		while (((maze.State(positionQueue.Head() + StepEast)) == Open)			// While head position has any unmarked neighbors
			|| ((maze.State(positionQueue.Head() + StepSouth)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepWest)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepNorth)) == Open)) 	
		{
			distance = maze.State(positionQueue.Head());										// Set distance

			if ((maze.State(positionQueue.Head() + StepEast)) == Open)							// Is east cell open?
			{
				maze.Mark(positionQueue.Head() + StepEast, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepEast) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepEast);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepSouth)) == Open)					// Is south cell open?
			{
				maze.Mark(positionQueue.Head() + StepSouth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepSouth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepSouth);						// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepWest)) == Open)						// Is West cell open?
			{
				maze.Mark(positionQueue.Head() + StepWest, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepWest) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepWest);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepNorth)) == Open)					// Is North cell open?
			{
				maze.Mark(positionQueue.Head() + StepNorth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepNorth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepNorth);						// Add it to the queue
			}
		}
		positionQueue.Dequeue();
	}
	return false;
}
开发者ID:WorldWideWebster,项目名称:Data-Structures,代码行数:65,代码来源:Prog7.cpp

示例2: SolveMaze

/*----- S o l v e M a z e (  ) -----

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
/*
const int Open     = -1;	// Cell is open
const int Obstacle = -2;	// Cell is an obstacle
const int StartCell= -3;	// Cell is the start cell
const int GoalCell = -4;	// Cell is the goal cell
const int PathCell = -5;	// Cell is on the shortest path
*/ 
	Position curPos = maze.Start();
	Position neighbor;
	positionQueue.Enqueue(curPos);
	maze.Mark(curPos, 0);
	int distance;

	while(!positionQueue.Empty()){
		curPos = positionQueue.Dequeue();
		distance = maze.State(curPos);
		neighbor = openPosition(maze, curPos);
		while(curPos != neighbor){
			maze.Mark(neighbor, distance + 1);
			if(neighbor == maze.Goal())
				return true;
			positionQueue.Enqueue(neighbor);
			neighbor = openPosition(maze, curPos);
		}
	}
	return false;
}
开发者ID:jazzywhit,项目名称:Data-Structures,代码行数:42,代码来源:Prog6.cpp

示例3: Retrace

/*----- R e t r a c e (  ) ----------------------------------

PURPOSE
Mark the path from the goal to the start cell.

INPUT PARAMETERS
maze           -- the maze object to be marked
-------------------------------------------------------------*/
void Retrace(Maze &maze)
{
 
	Position curPos = maze.Goal();
	int distance = maze.State(maze.Goal());		// Distance from start cell

	while (distance >= 0)
	{
		maze.Mark(curPos, PathCell);
		if ((maze.State(curPos + StepNorth)) == (distance - 1))
			curPos = curPos + StepNorth;
		
		else if ((maze.State(curPos + StepWest)) == (distance - 1))
			curPos = curPos + StepWest;
		
		else if ((maze.State(curPos + StepSouth)) == (distance - 1))
			curPos = curPos + StepSouth;
		
		else if ((maze.State(curPos + StepEast)) == (distance - 1))
			curPos = curPos + StepEast;

		distance -= 1;
	}
}
开发者ID:WorldWideWebster,项目名称:Data-Structures,代码行数:32,代码来源:Prog7.cpp

示例4: RetracePath

/*----- R e t r a c e P a t h (  ) -----

PURPOSE
Mark the path from the goal to the start cell.

INPUT PARAMETERS
maze           -- the maze object to be marked
*/
void RetracePath(Maze &maze)
{
	Position curPos = maze.Goal();  //This function is used on complete mazes.
	Position neighbor;
	int distance;

	do{
		distance = maze.State(curPos);
		maze.Mark(curPos, PathCell);
		do{
			neighbor = pathCheck(maze, curPos,distance);
			if (maze.State(neighbor) < distance || distance == 0){
				curPos = neighbor;
				break;
			}		
		}while(curPos != neighbor);
	}while(distance > 0);
}
开发者ID:jazzywhit,项目名称:Data-Structures,代码行数:26,代码来源:Prog6.cpp


注:本文中的Maze::Goal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。