Search code examples
c++arraysvectorstructrandom-walk

Efficient way to allow for an n-dimensional coordinate?


I am currently building a program that will calculate a self-avoiding walk in n-dimensions. The program has to have a list of coordinates that it has previously visited. For a known maximum number of dimensions, I would simply make a vector of a position struct as such:

struct Position
{
    long int x;
    long int y;
    long int z;
    long int w;
    etc...
}

std::vector<Position> history;
Position currentSite;

But when programming for an n-dimensional position, I am not sure how to do that without making an array of n*walk_length in size.

Is there a more "correct" way to do it?

Note: I'm programming in C++.


Solution

  • I'd use vector for coordinates, and set for positions:

    typedef std:vector<long> Position;
    typedef std::set<Position> VisitedPositions;
    

    Then you'll be able to choose n dynamically and search for positions more quickly.