2

我想你们都知道有些人在求职面试中给你的“草莓”问题,你需要计算一个二维数组的两个角之间的路径,你只能向上或向右移动,你有计算最大值小路。我有一个完美的工作代码,可以在递归中完成,但它的复杂性很高。我还在 O(n^2) 复杂度中解决了“for循环”解决方案中的问题。但在这个解决方案中,我只是想不出一种方法来打印路线,就像我在递归解决方案中所做的那样。这是我的代码(在这里阅读很长,所以我想你应该复制、编译和运行)。看递归解的结果,

public class Alg
{
public static void main(String args[])
{
    String[] route = new String[100];                  
    int[][]array = {{4,-2,3,6}
                    ,{9,10,-4,1}
                    ,{-1,2,1,4}
                    ,{0,3,7,-3}};
    String[][] route2 = new String[array.length][array[0].length];                
    int max = recursionAlg(array,array.length-1,0,route);        
    int max2 = loopAlg(array,array.length-1,0,route2);
    System.out.println("The max food in the recursion solution is: "+max);
    System.out.println("and the route is: ");
    printRouteArray(route);
    System.out.println("The max food in the loop solution: "+max2);
    System.out.println("The route is: ");
    //SHOULD PRINT HERE THE ROUTE
}


public static int loopAlg(int [][] arr,int x, int y, String[][] route)
{
    int n=0;
    int[][]count = new int[arr.length][arr[0].length];
    for(int i = x; i>=0 ; i--)
    {
        for(int j = 0; j<arr[0].length; j++)
        {
            if (i==x && j==0) {count[i][j]=arr[i][j];}
            else if (i == x) { count[i][j]=count[i][j-1]+arr[i][j];}
            else if (j == 0) { count[i][j]=count[i+1][j]+arr[i][j]; }
            else{
                if (count[i][j-1]>count[i+1][j]) {count[i][j]=count[i][j-1]+arr[i][j];}
                else { count[i][j]= count[i+1][j]+arr[i][j];}
            }
        }
    }
    return count[0][arr[0].length-1];
}   

public static int recursionAlg(int [][] arr, int x, int y,String[] route)
{
    return recursionAlg(arr,0,x,y,arr[0].length-1,route,0);        
}

public static int recursionAlg(int[][]arr,int count,int x, int y, int max_y, String[] route, int i)
{
    if (x == 0 && y == max_y) {return count;}
    else if (x == 0) {
        route[i]="Right";
        return recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1);
    }
    else if (y==max_y){
        route[i]="Up";
        return recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1);
    }     
    else if (recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1)>recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1))
    {
        route[i]="Up";
        return  recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1);
    }
    else
    {
        route[i]="Right";
        return recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1);
    }
}

public static void printRouteArray(String[] arr)
{
    int i=0;
    while (i<arr.length && (arr[i]=="Up" || arr[i]=="Right"))
    {
        System.out.print(arr[i]+"-->");
        i++;
    }
    System.out.println("End");
}    
}

希望能帮到你,谢谢!

4

1 回答 1

0

您需要在其中记住另一个二维数组,该数组loopAlg记住要为初始二维数组中的每个条目执行下一个条目所采取的步骤。有关演示,请参见以下代码和https://ideone.com/kM8BAZ :

public static void main(String args[])
{
    String[] route = new String[100];                  
    int[][]array = {{4,-2,3,6}
                    ,{9,10,-4,1}
                    ,{-1,2,1,4}
                    ,{0,3,7,-3}};
    String[] route2 = new String[100];                
    int max = recursionAlg(array,array.length-1,0,route);        
    int max2 = loopAlg(array,array.length-1,0,route2);
    System.out.println("The max food in the recursion solution is: "+max);
    System.out.println("and the route is: ");
    printRouteArray(route);
    System.out.println("The max food in the loop solution: "+max2);
    System.out.println("The route is: ");
    printRouteArray(route2);
}

public enum Dirs {START, FROM_LEFT, FROM_DOWN};

public static int loopAlg(int [][] arr,int x, int y, String[] route)
{

    int n=0;
    int[][]count = new int[arr.length][arr[0].length];
    Dirs[][] directions = new Dirs[arr.length][arr[0].length];
    List<String> path = new ArrayList<String>();

    for(int i = x; i>=0 ; i--)
    {
        for(int j = 0; j<arr[0].length; j++)
        {
            if (i==x && j==0) {count[i][j]=arr[i][j]; directions[i][j] = Dirs.START;}
            else if (i == x) { count[i][j]=count[i][j-1]+arr[i][j]; directions[i][j] = Dirs.FROM_LEFT;}
            else if (j == 0) { count[i][j]=count[i+1][j]+arr[i][j]; directions[i][j] = Dirs.FROM_DOWN;}
            else{
                if (count[i][j-1]>count[i+1][j]) {count[i][j]=count[i][j-1]+arr[i][j];directions[i][j] = Dirs.FROM_LEFT;}
                else { count[i][j]= count[i+1][j]+arr[i][j];directions[i][j] = Dirs.FROM_DOWN;}
            }
        }
    }
    int i=0, j=arr[0].length-1;
    while(directions[i][j]!= Dirs.START) {
        if(directions[i][j] == Dirs.FROM_LEFT) {
            path.add("Right");
            j--;
        }
        else {
            path.add("Up");
            i++;
        }
    }
    Collections.reverse(path);
    i=0;
    for(String part:path) {
        route[i] = part;
        i++;
    }

    return count[0][arr[0].length-1];
}   
于 2013-04-29T10:21:13.783 回答