本文整理汇总了C++中Maze::Start方法的典型用法代码示例。如果您正苦于以下问题:C++ Maze::Start方法的具体用法?C++ Maze::Start怎么用?C++ Maze::Start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Maze
的用法示例。
在下文中一共展示了Maze::Start方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}