2013-04-29 96 views
2

我想你们都知道一些在求职面试中给予你的“草莓”问题,你需要计算一个二维阵列的两个角之间的路径,你只能向上或向右移动,并且你已经计算出了最大价值路径。 我有一个完美的工作代码,它在递归,但它的复杂性是很高。 我也解决了这个问题,在“for循环”的解决方案,它在O(n^2)复杂性。 但在这个解决方案中,我只是不知道如何在递归解决方案中打印路线。 这是我的代码(在这里阅读很长,所以我想你应该复制,编译和运行)。 看看递归解决方案的结果,BTW - 路径需要从左下角到右上角 我要打印的路线更好的解决方案以同样的方式:如何在Java中打印二维数组中的最大值路径?

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"); 
}  
} 

希望你能帮忙,谢谢!

回答

0

您需要loopAlg内的另一个二维数组,它记住了在初始2-dim数组中每个条目到达下一个条目所需的步骤。请参阅以下代码和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]; 
} 
+0

是的,我认为这就是我一直在寻找的:)谢谢! – TomG 2013-04-29 18:36:08