0

This is for a Brute-Force Algorithm I'm trying. Trying every combination.

The code produces every combination of moves of length 10 from 4 moves(up, down, left, right). So that's why the nested for loops are 10 deep. But what if there are less or more moves required like 9 or 11 or 12. How can I make the nested for loop dynamic. I'm having trouble with recursion.

int available = new int[] {TILT_MOVE.UP, TILT_MOVE.DOWN, TILT_MOVE.LEFT, TILT_MOVE.RIGHT}.length;

    int n = 10;
    int total = 1;

    for (int i = 0; i < n; i++) {
        total *= available;
    }

    int combinations[][] = new int[total][n];
    int count = 0;

    //My head got burnt up with the implementation of maze tilting logic so this is the best I can do for now.
    for (int a = 0; a < available; a++) {
        for (int b = 0; b < available; b++) {
            for (int c = 0; c < available; c++) {
                for (int d = 0; d < available; d++) {
                    for (int e = 0; e < available; e++) {
                        for (int f = 0; f < available; f++) {
                            for (int g = 0; g < available; g++) {
                                for (int h = 0; h < available; h++) {
                                    for (int i = 0; i < available; i++) {
                                        for (int j = 0; j < available; j++) {   
                                            int[] moves = new int[n];
                                            moves[0] = a;
                                            moves[1] = b;
                                            moves[2] = c;
                                            moves[3] = d;
                                            moves[4] = e;
                                            moves[5] = f;
                                            moves[6] = g;
                                            moves[7] = h;
                                            moves[8] = i;
                                            moves[9] = j;

                                            combinations[count++] = moves;

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
5
  • 4
    "But what if there are less or more moves required like 9 or 11 or 12." - Then you use recursion. --- "I'm having trouble with recursion." - In the beginning, everyone has. Shying away from it won't make it better. Practice it! You will understand it eventually =) Commented Jun 15, 2018 at 15:35
  • @Carcigenicate Very likely? It would take a lot more than 12 levels... Commented Jun 15, 2018 at 15:53
  • @shmosel Ya, I'm not not why I wrote very likely. If you can guarantee a max of 12 levels, it would be very unlikely. Commented Jun 15, 2018 at 15:58
  • what are the values of TILT_MOVE.UP, TILT_MOVE.DOWN, TILT_MOVE.LEFT, TILT_MOVE.RIGHT Commented Jun 15, 2018 at 16:32
  • In general if you are having 4 or more nested loops you probably need to rethink your logic Commented Jun 15, 2018 at 19:24

2 Answers 2

4

I believe this should work:

for (int i = 0; i < total; i++)
{
    int[] moves = new int[n];
    for (int move = 0; move < n; ++move)
    {
        moves[move] = (i / (int) Math.pow(available, move)) % available;
    }
    combinations[i] = moves;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work. These is an example move from my code [0 0 3 2 3 3 2 2 1 1 ]. This is from your code [0 9 49 349 1349 61349 61349 61349 61349 61349 ]. The size 10 array only needs values from 0 - 3 which corresponds to TILT_UP, TILT_DOWN, TILT_RIGHT, TILT_LEFT.
@user4408936 Sorry, yes it wasn't correct before. I've updated my answer.
1

You're trying to find a path through a maze?

If so, stop thinking of the maze as a human maze. To a computer a maze is a graph, where each node is possible location.

The solution to the maze is a sequence of moves that leads the balls to the hole(s). This can be thought of as a tree, where each node represents {tilt-direction, ballA-position, ballB-position}.

In this more complicated version of the maze you'll need to account for the ability to tilt when one ball is already stuck up against a barrier. Only when both balls have a wall to their left do you no longer have a possible left move.

You'll also need to make sure that you don't keep recurring over the same moves. If ballA and B have both been on the same space during the same turn don't continue trying this move.

5 Comments

Rather, a maze is a graph. In particular, you'll have to remember which nodes are already visited (to avoid infinite recursion), which is more complicated than simple tree traversal.
@pkpnd you mean Dijkstra's algorithm? =)
@Turing85 I was thinking BFS as a step up from a tree traversal algorithm.
@Turing85 yes a maze. But it's quite different. There are two balls and two holes and also a few 1x1 blocks. I've already solve it using the code above. Now just I'm learning how to do that code in a more dynamic way because it seems most of the sample problem given by the company I'm applying for uses code like that.
@pkpnd You're right I worded this wrong. The maze is a graph, but the solution can be displayed as a tree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.