2
\$\begingroup\$

I asked a similar question yesterday and got a brilliant answer, but unsure how I could adapt these methods.

The methods perform a BFS on a state and then store the values in a multi-dimensional array.

You can see that the code is repeating itself, just with different array dimensions.

Method 1:

    public void bfs2(int[][][] three, PDB15Tile.State s, int[] storedNums, String fileName) throws IOException {
        Queue<PDB15Tile.State> q = new LinkedList<>();
        int[] positions = new int[storedNums.length];
        for (int i = 0; i < storedNums.length; i++) {
            positions[i] = getPatternPosition(storedNums[i], s.getState());
        }
        three[positions[0]][positions[1]][positions[2]] = 0;
        q.add(s);
        PDB15Tile.State current;
        while (!q.isEmpty()) {
            current = q.poll();
            // System.out.println(Arrays.toString(current.getState()));
            for (PDB15Tile.State neighbour : current.findNeighbours2()) {
                // System.out.println(Arrays.toString(neighbour.getState()));
                for (int i = 0; i < storedNums.length; i++) {
                    positions[i] = getPatternPosition(storedNums[i], neighbour.getState());
                }
                if (three[positions[0]][positions[1]][positions[2]] == 0) {
                    three[positions[0]][positions[1]][positions[2]] = neighbour.getH();
                    q.add(neighbour);
                }

            }

        }
        serializeArrayToFile(three, fileName);
    }

Method 2:

    public void bfs2(int[][][][][][] six, PDB15Tile.State s, int[] storedNums, String fileName) throws IOException {
        Queue<PDB15Tile.State> q = new LinkedList<>();
        int[] positions = new int[storedNums.length];
        for (int i = 0; i < storedNums.length; i++) {
            positions[i] = getPatternPosition(storedNums[i], s.getState());
        }
        six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] = 0;
        q.add(s);
        PDB15Tile.State current;
        while (!q.isEmpty()) {
            current = q.poll();
            // System.out.println(Arrays.toString(current.getState()));
            for (PDB15Tile.State neighbour : current.findNeighbours2()) {
                // System.out.println(Arrays.toString(neighbour.getState()));
                for (int i = 0; i < storedNums.length; i++) {
                    positions[i] = getPatternPosition(storedNums[i], neighbour.getState());
                }
                //System.out.println(six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]]);
                if (six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] == 0) {
                    six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] = neighbour.getH();
                    q.add(neighbour);
                }

            }

        }
        serializeArrayToFile(six, fileName);
    }

How do I determine the number of dimensions of the object that is passed in and then manipulate the array accordingly?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ May I ask why are you using multi-dimensional arrays for this, that is, what is the purpose of three and six arrays? I don't see any iterating over the 3 or 6 dimensions. You are only ever retrieving values from fixed indexes, which makes me wonder if your choice of data structures is correct. \$\endgroup\$
    – RK1
    Commented Mar 27, 2017 at 17:26
  • \$\begingroup\$ @RK1 You might be interested in reading this: codereview.stackexchange.com/questions/158922/… \$\endgroup\$ Commented Mar 27, 2017 at 19:01
  • \$\begingroup\$ Thats the whole idea of the 3 and 6 dimension arrays @RK1 to prevent having to iterate over the array. The indexs represent value positions, its called sparse indexing \$\endgroup\$ Commented Mar 28, 2017 at 12:41

0

Your Answer

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

Browse other questions tagged or ask your own question.