Currently I am implementing the A* algorithm in C++. I have chosen to use a hybrid of a '2D vector grid' and two 1D pointer vectors to specific places in the '2D vector grid'. I have chosen to do it this way, so I can access the nodes using coordinates and also for iterating over the appropriate nodes(rather than the whole '2D vector grid').
In the below examples I have not included the full code because I deemed it irrelevant to the question.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
vector <int> CInGame::AStarAlgorithm(vector<vector<bool>>& resistanceMap, int startX, int startY, int targetX, int targetY, int cutOff)
vector <int> returnVec;
vector <vector<CNode>> twoDimNodes;
vector <CNode*> openSet;
vector <CNode*> closedSet;
//lots of code here
if (closestX==startX && closestY==startY)
Assuming the pointers in openSet and closedSet point to the objects in twoDimNodes.... this will only work as long as the objects do not move in memory.
The nasty gotcha here is that vector might have to move elements around in memory when you resize in order to keep all the memory contiguous. If that happens... all of your pointers in openSet and closedSet will go bad!
So if you're taking this approach... you must be absolutely sure you are not adding any more elements to your twoDimNodes vector once openSet or closedSet are populated.