【问题标题】:How to print the maximum valued path in a 2D array in Java?如何在Java中打印二维数组中的最大值路径?
【发布时间】:2013-04-22 16:46:00
【问题描述】:

我想你们都知道有些人在工作面试中给你的“草莓”问题,你需要计算一个二维数组的两个角之间的路径,你只能向上或向右移动,然后你就有了计算最大值路径。 我有一个完美的工作代码,可以在递归中完成,但它的复杂性很高。 我还在 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");
}    
}

希望您能帮忙,谢谢!

【问题讨论】:

    标签: java arrays multidimensional-array max-path


    【解决方案1】:

    您需要在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];
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2017-08-08
      • 2021-11-13
      • 1970-01-01
      • 2018-09-14
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多