Home > Blockchain >  Sum of specific path in a 2D-Arrray
Sum of specific path in a 2D-Arrray

Time:12-06

Given way of traversal Given String-Array

So given the String Array it should travers as shown via the red path and give back the sum of the numbers which are on the path (1 2 3 4 5 6 7 8 9 = 45). The directional instructions: "L", "R", "D" or "U" (for left, right, down, up)

` **Don't really know if it's even possible to implement a while-Loop inside a for-Loop, wasn't really sure how i should solve this one. **

int sumPath(String[][] maze) {
    int sum = 0;


    for (int i = 0; i < maze.length; i  ) {
        for (int j = 0; j < maze[0].length; j  ) {
            while (!(maze[i][j].equals("E") & (!(maze[i][j].equals("0"))))) {

                if (maze[i][j].equals("R")) {
                    j  ; //move to the right (increase col)
                    
                }
                else if (maze[i][j].equals("D")) {
                    i  ; //move down (increase row)

                }
                else if (maze[i][j].equals("L")) {
                    j--; //move to the left (decrease col)

                }
                else if (maze[i][j].equals("U")) {
                    i--; //move up (decrease row)

                }
                else if (!(maze[i][j].equals("R") || maze[i][j].equals("D") || maze[i][j].equals("L") || maze[i][j].equals("U") || maze[i][j].equals("E"))) {
                    sum  = Integer.parseInt(maze[i][j]);


                }break;

            }

        }



    }return sum;


}

`

CodePudding user response:

Two things:

  1. In your while loop, you use an & (bitwise and) instead of && (logical and).

  2. You can essentially just remove the for loops as you just want it to run until you reach the 'E'.

    int sumPath(String[][] maze) {
        int sum = 0;
        int i = 0; // because you start at the top left
        int j = 0;
    
        while (!(maze[i][j].equals("E") && (!(maze[i][j].equals("0"))))) {
    
             if (maze[i][j].equals("R")) {
                 j  ; //move to the right (increase col)
    
             }
             else if (maze[i][j].equals("D")) {
                 i  ; //move down (increase row)
    
             }
             else if (maze[i][j].equals("L")) {
                 j--; //move to the left (decrease col)
    
             }
             else if (maze[i][j].equals("U")) {
                 i--; //move up (decrease row)
    
             }
             else { // just use an else block because you already checked all other options
                 sum  = Integer.parseInt(maze[i][j]);
             }
         }
         return sum;
     }
    

This assumes that the maze is made correctly. I can't check the code right now but this should work.

CodePudding user response:

**So i just figured it out ** *Main thing i did is save the current direction the path is going in a string, and switching to a switch *

    int sumPath3(String[][] maze) {
    int sum = 0;
    int i = 0;
    int j = 0;
    String currDir = "";

    while (!(maze[i][j].equals("E") & (!(maze[i][j].equals("0"))))) {
        if ((maze[i][j].equals("R") | maze[i][j].equals("D") | maze[i][j].equals("L") | maze[i][j].equals("U"))) {
            currDir = maze[i][j];
            switch (currDir) {
                case "R" -> j  ;
                case "D" -> i  ;
                case "L" -> j--;
                case "U" -> i--;
            }
        } else {
            sum  = Integer.parseInt(maze[i][j]);
            switch (currDir) {
                case "R" -> j  ;
                case "D" -> i  ;
                case "L" -> j--;
                case "U" -> i--;
            }
        }
    }
    return sum;
}
  • Related