Apara
Apara

Reputation: 374

Loop through a list of items in a different order in each iteration

So I have a loop and in each iteration, I want to go through the entire loop in a different order until the end conditions are satisfied.

During each iteration, the variables controlling the order in which I want to go through the loop change.

For example,

    while (endCondition == false){
        Item1;
        Item2;
        Item3;
    }

The first time I enter the loop, I want it to go through in the order 1,2,3. The second time: 2,1,3... and so on (Only I don't know the order for the n-th iteration before hand)

Is there a way to implement this at all? All suggestions welcome!

Edit: I know the order of the iteration at the end of the previous iteration. I want to know how to execute that permutation.

Item1, Item2, Item3 are functions that push elements into a stack. The loop runs until the endCondition element is pushed. At the end of each iteration, the top element is used as a reference to add 3 more elements, in the next iteration, using the above functions. I want to prioritize the elements pushed on to the stack to reach the endCondition faster. And the order of priority will depend on how close the top element is to the endCondition.

    while(maze[currPos_y][currPos_x] != DESTINATION){

        int flag = 0;

        //Adding all open spaces around current position to stack
        if(maze[currPos_y + 1][currPos_x] == OPEN){
            path.push(currPos_y+1, currPos_x);
            flag++;
        }

        if(maze[currPos_y][currPos_x + 1] == OPEN){
            path.push(currPos_y, currPos_x + 1);
            flag++;
        }

        if(maze[currPos_y][currPos_x - 1] == OPEN){
            path.push(currPos_y,currPos_x - 1);
            flag++;
        }

        if(maze[currPos_y - 1][currPos_x] == OPEN){
            path.push(currPos_y - 1, currPos_x);
            flag++;
        }

        //If path is blocked
        if (flag <= 1){
            path.pop();
        }

        currPos_x = path.top_x();
        currPos_y = path.top_y();

}

path is a stack I implemented. There is no issue with the stack and it works fine.

As you can see, this code is going to run for a long time if the 2D array maze is large in size. So I want to make it faster by going in the direction of the DESTINATION. So the position that is closest to DESTINATION will be pushed last in order to be accessed first.

Upvotes: 1

Views: 325

Answers (2)

Chris Sharp
Chris Sharp

Reputation: 1995

You may try the code below. Not knowing how you are getting the pattern back I have not solved for that part. This should give you at least another approach that's fairly conventional.

#include <iostream>
#include <vector>

using namespace std;

int subtractOne(int num){
    return num - 1;
}

vector<int> patternIteration(vector<int> pattern, vector<int> nums){
    vector<int> ret;
    for (int i = 0; i < pattern.size(); i++){
        ret.push_back(subtractOne(nums.at(pattern.at(i))));
    }
    return ret;
}

int main(int argc, const char * argv[]) {
    vector<int> answer = patternIteration({2, 3, 1, 0}, {5, 1, 3, 8});

    for (int i = 0; i < answer.size(); i++){
        cout << answer.at(i) << " \n";
    }
    return 0;
}

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

Seems to me like a garden-variety std::map would be the appropriate container here.

Based on the information given in your question, it seems that you know what ordinal values are assigned to each instance of your object. You know which object is object #1, which one is object #2, which one is #3, and so on.

If so, then just put your objects into a

std::map<int, object_type> m;

Then, when you have an order of [1,2,3], go ahead and do what you want with your objects, by accessing m[1], m[2], and then m[3].

Then, if on the next iteration your order of visitation is [3,1,2], go ahead and do your thing with m[3], m[1], and m[2].

It might be slightly inconvenient if your class does not have a default constructor, and you can't use operator[]; if that's the case that'll be only a minor detail to square away, if needed.

Upvotes: 2

Related Questions