Question

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++.

Was it helpful?

Solution 2

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.

OTHER TIPS

If n is known at compile time, you could use std::array<long,n> to represent a position.

If n is not known until runtime, std::vector<long> would be a good choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top