本文整理汇总了C++中map::getOneStepMove方法的典型用法代码示例。如果您正苦于以下问题:C++ map::getOneStepMove方法的具体用法?C++ map::getOneStepMove怎么用?C++ map::getOneStepMove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类map
的用法示例。
在下文中一共展示了map::getOneStepMove方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getSuccessors
void basicFourWayExpander::getSuccessors(int a_currentPosition, int a_parent, int a_finalPosition, map& a_map, Direction* a_direction, std::vector<std::pair<int, Direction> >& a_successors)
{
const int* oneStepMove = a_map.getOneStepMove();
for (int j = 0; j < 4; j++)
{
int nextPos = a_currentPosition + oneStepMove[j];
Direction nextDir = a_direction[j];
std::pair<int, Direction> nextPair(nextPos, nextDir);
a_successors.push_back(nextPair);
}
}
示例2: findAllJumpPoints
void jumpPointTools::findAllJumpPoints(const map& a_map, std::unordered_map<int, std::unordered_map<char, std::vector<std::tuple<int, Direction, bool> > > >& a_jumpPoints)
{
int mapSize = a_map.getWidth()*a_map.getHeight();
Direction directions [] = { Direction::N,
Direction::S,
Direction::E,
Direction::W,
Direction::NE,
Direction::NW,
Direction::SE,
Direction::SW};
const int* oneStepMove = a_map.getOneStepMove();
// loop over all positions
for (int position = 0; position < mapSize; position++)
{
// if traversible
if (a_map[position])
{
std::unordered_map<char, std::vector<std::tuple<int, Direction, bool> > > thisPositionJPMap;
// might not have any jump point (ie a corner) and therefore dont need to insert into map
bool hasPoint= false;
// loop over the 8 parent directions (direction that parent takes to get here)
for (int parentDir = 0; parentDir < 8; parentDir++)
{
// if parent is traversible (hence it is possible to get here from the parent), parent is in opposite direction of parent direction
if (a_map[position - oneStepMove[parentDir]])
{
std::vector<std::tuple<int, Direction, bool> > jumpPoints;
findJumpPoints(position, a_map, directions[parentDir], oneStepMove, jumpPoints);
// insert into submap only if it has jump point in this direction (not next to a wall or something)
if (jumpPoints.size() > 0)
{
hasPoint = true;
thisPositionJPMap[parentDir] = jumpPoints;
}
}
}
if (hasPoint)
{
a_jumpPoints[position] = thisPositionJPMap;
}
}
}
}
示例3: eightDirectionSearch
// vanilla 8 direction search
int aStarSearch::eightDirectionSearch(const int a_startX, const int a_startY, const int a_targetX, const int a_targetY, const int a_mapWidth, const int a_mapHeight, map& a_map, int* a_outBuffer, const int a_outBufferSize)
{
// create open set, priority queue returns the greater of the two compared elements, so tell it to use > instead of < to compare
// the pair to be put in queue is the (f cost, position)
std::priority_queue<std::pair<double,int>, std::vector<std::pair<double,int> >, std::greater<std::pair<double,int> > > openSet;
std::unordered_map<int,int> cameFrom;
std::unordered_map<int,double> costSoFar;
// keep track of who is visited, the bool is sort of pointless..
std::unordered_map<int,bool> visited;
// get heuristic
octileHeuristic heuristic;
int startPosition = a_startX + a_startY * a_mapWidth;
int finalPosition = a_targetX + a_targetY * a_mapWidth;
// start with initial condition and put it in queue
std::pair<double,int> startPair(0,startPosition);
cameFrom[startPosition] = startPosition;
openSet.push(startPair);
// one step in order: N S E W NE NW SE SW
const int* oneStepMove = a_map.getOneStepMove();
while (!openSet.empty())
{
// get best position so far
int currentPosition = openSet.top().second;
openSet.pop();
// never visited
if (!visited.count(currentPosition))
{
// put it in visited
visited[currentPosition] = true;
if (currentPosition == finalPosition)
{
return reconstructPath(startPosition, currentPosition, a_map, cameFrom, a_outBuffer, a_outBufferSize);
}
// consider the 8 possible moves
double cost;
for (int successor = 0; successor < 8; successor++)
{
int nextPos = currentPosition + oneStepMove[successor];
// check if move is possible
if (a_map[nextPos])
{
// for visualization purposes
a_map[nextPos] = 2;
// straight moves cost 1, diagonal moves cost sqrt(2)
if (successor < 4)
{
cost = costSoFar[currentPosition] + 1;
}
else
{
cost = costSoFar[currentPosition] + sqrt(2);
}
// if the neighbor is not in open set or cost from this node is better
if (!costSoFar.count(nextPos) || cost < costSoFar[nextPos])
{
costSoFar[nextPos] = cost;
double priority = cost + heuristic.estimateCost(nextPos, a_targetX, a_targetY, a_mapWidth);
std::pair<double,int> nextPair(priority,nextPos);
openSet.push(nextPair);
cameFrom[nextPos] = currentPosition;
}
}
}
}
}
// couldn't find a solution
return -1;
}
示例4: jumpPointSearch
// vanilla jps
int aStarSearch::jumpPointSearch(const int a_startX, const int a_startY, const int a_targetX, const int a_targetY, const int a_mapWidth, const int a_mapHeight, map& a_map, int* a_outBuffer, const int a_outBufferSize)
{
// compute start and final positions
int startPosition = a_startX + a_startY * a_mapWidth;
int finalPosition = a_targetX + a_targetY * a_mapWidth;
// create open set, priority queue returns the greater of the two compared elements, so tell it to use > instead of < to compare
// the pair to be put in queue is the (f cost, position)
std::priority_queue<std::pair<double,int>, std::vector<std::pair<double,int> >, std::greater<std::pair<double,int> > > openSet;
std::unordered_map<int,int> cameFrom;
std::unordered_map<int,double> costSoFar;
// keep track of who is visited, the bool is sort of pointless..
std::unordered_map<int,bool> visited;
// get heuristic
octileHeuristic heuristic;
double priority, cost;
// get jump point finder
jumpPointTools jpTools;
/*
// get dead end detector and detect deadend
deadendDetector deDetector;
std::vector<int> relevantZones(a_map.getNumZones()+1,0);
deDetector.deadendSearch(a_map.getZone(startPosition), a_map.getZone(finalPosition), a_map.getZoneConnections(), relevantZones);
*/
cameFrom[startPosition] = startPosition;
// one step in order: N S E W NE NW SE SW
const int* oneStepMove = a_map.getOneStepMove();
Direction directions [] = { Direction::N,
Direction::S,
Direction::E,
Direction::W,
Direction::NE,
Direction::NW,
Direction::SE,
Direction::SW};
// start problem off by moving one step in every direction from starting position
visited[startPosition] = true;
for (int successor = 0; successor < 8; successor++)
{
int nextPos = startPosition + oneStepMove[successor];
// check if move is possible
if (a_map[nextPos])
{
// for visualization purposes
a_map[nextPos] = 2;
// straight moves cost 1, diagonal moves cost sqrt(2)
if (successor < 4)
{
cost = costSoFar[startPosition] + 1;
}
else
{
cost = costSoFar[startPosition] + sqrt(2);
}
costSoFar[nextPos] = cost;
priority = cost + heuristic.estimateCost(nextPos, a_targetX, a_targetY, a_mapWidth);
std::pair<double,int> nextPair(priority,nextPos);
openSet.push(nextPair);
cameFrom[nextPos] = startPosition;
}
}
// look at open set
while (!openSet.empty())
{
// get best position so far
int currentPosition = openSet.top().second;
openSet.pop();
// haven't visited this node yet
if (!visited.count(currentPosition))
{
visited[currentPosition] = true;
// check end condition
if (currentPosition == finalPosition)
{
return reconstructPath(startPosition, currentPosition, a_map, cameFrom, a_outBuffer, a_outBufferSize);
}
// find out how parent got here, currentPosition = parent + parentDirection
Direction parentDirection = jpTools.findParentDirection(currentPosition, cameFrom[currentPosition], oneStepMove, directions);
// find successors!
std::vector<std::pair<int, Direction> > successors;
jpTools.findJumpPoints(currentPosition, a_map, parentDirection, finalPosition, oneStepMove, successors);
for (int j = 0; j < successors.size(); j++)
{
//.........这里部分代码省略.........