Longest Increasing Sequence 2D matrix recursion

后端 未结 4 1314

I have been presented with a new homework assignment that has been somewhat frustrating to say the least. Basically, I have a create a 2D array of integers as follows:

4条回答
  •  庸人自扰
    2020-12-23 23:21

    I know this is a very old question, but, I'm reading Lubomir Stanchev's book titled Learning Java Through Games, and the Chapter 14 Project is that exact 2D array of integers. The assignment is to find the longest increasing sequence but only in two directions, South and East, no diagonals or anything. Still it took me hours to figure out the logic, not used to the recursion either. I simplified the problem by creating auxiliary methods that check if the next index is valid in that direction (that is, not out of bounds and greater than the current value). Then I placed the base case at the start of the method, which is when there is no next possible index. The tricky part is the assignment of the String variable, so each time the method uses recursion the indexes are saved in the String. I solved it by using the String.length() method to compare the length of each sequence, when there is more than one possible path. With the basic logic in place, in order to expand the method all that it would require is creating more auxiliary methods in the direction needed, and adding those directions to the logic.

    public static boolean isRightLegal(int[][] array, int row, int column) {
        //if we are at the last column
        if (column >= array[row].length - 1) {
            return false;
        }
        //if we are not at the one before last
        if ((column + 1) < array[row].length) {
            //if the current number is greater than the next
            if (array[row][column] > array[row][column + 1]) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isDownLegal(int[][] array, int row, int column) {
        //if we are at the last row
        if (row >= array.length - 1) {
            return false;
        }
        //if we are not at the one before last
        if ((row + 1) < array.length) {
            //if the current number is greater than the next
            if (array[row][column] > array[row + 1][column]) {
                return false;
            }
        }   
        return true;
    }
    
    public static String recursiveSequence(int[][] array, int row, int column, String path) {
        //base case: when we reach the end of the sequence
        if (! isDownLegal(array, row, column) && ! isRightLegal(array, row, column)) {
            return "(" + row + "," + column + ") ";
        }
        path = "(" + row + "," + column + ") ";
        //if both paths are valid
        if (isDownLegal(array, row, column) && isRightLegal(array, row, column)) {
            //calculate each sequence and pick the longest one
            if (recursiveSequence(array, (row + 1), column, path).length() > recursiveSequence(array, row, (column + 1), path).length()) {
                path += recursiveSequence(array, (row + 1), column, path);
            } else {
                path += recursiveSequence(array, row, (column + 1), path);
            }
            return path;
        }
        //if only the down path is valid
        if (isDownLegal(array, row, column)) {
            path += recursiveSequence(array, (row + 1), column, path);
        }
        //if only the right path is valid
        if (isRightLegal(array, row, column)) {
            path += recursiveSequence(array, row, (column + 1), path);
        }
        return path;
    }
    

    }

提交回复
热议问题