【问题标题】:What is wrong with this solution to Path in Matrix Problem on geeksforgeeks?在 geeksforgeeks 上解决矩阵问题中的路径有什么问题?
【发布时间】:2019-02-27 01:00:01
【问题描述】:

你能帮我解决这个 geeksforgeeks 问题吗
给定一个正整数的 N X N 矩阵 Matrix[N][N]。单元格 Matrix[r][c] 中只有三种可能的移动。

  1. 矩阵[r+1][c]

  2. 矩阵[r+1][c-1]

  3. 矩阵[r+1][c+1]

从第 0 行的任何列开始,返回到第 N-1 行的所有路径的最大总和。

问题链接:https://practice.geeksforgeeks.org/problem-page.php?pid=271

解决方案链接:https://ide.geeksforgeeks.org/R5iu3xwTAO

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t  =1;
        int n;
        int a[][];
        while(t-->0){
            n = 17;
            a = new int [n][n];
            int input[]={67,280,171,381,930,781,925,4,393,380,246,433,762,258,5,166,315,503,385,728,854,350,464,288,304,80,689,56,313,843,92,379,122,614,111,403,394,387,406,138,767,651,571,880,260,927,398,926,429,782,653,634,132,468,274,435,548,314,490,212,156,933,942,629,546,404,31,292,142,436,781,260,86,703,140,697,630,537,622,410,318,275,44,801,94,669,236,993,982,77,204,137,10,497,765,907,900,147,550,42,582,331,301,19,33,792,715,14,680,336,424,350,962,467,150,408,135,737,400,468,814,956,956,175,452,72,433,704,218,983,97,799,665,749,169,49,541,883,63,572,570,486,921,884,304,423,291,790,159,42,257,324,997,212,498,801,283,283,504,500,617,952,650,281,700,818,329,592,52,743,164,621,228,436,856,883,858,498,672,17,540,928,340,536,139,190,336,773,472,191,272,88,142,921,720,842,90,400,433,141,143,948,114,722,384,969,605,593,819,276,961,358,556,301,893,46,842,581,819,665,771,90,104,265,363,823,106,452,574,890,945,68,190,58,790,925,378,746,517,196,373,478,905,280,130,798,326,323,730,144,987,500,585,90,764,947,264,221,751,837,463,47,257,652,456,46,576,185,143,444,381,867,921,285,147,402,434,472,724,163,615,710,15,551,151,130,498,414,703};
            int k=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    a[i][j] = input[k];
                    k++;
                }
            }
            HashMap<String,Integer> h = new HashMap();
            int max=Integer.MIN_VALUE,ans=0;
            for(int i=0;i<a.length;i++){
                ans=CostPath(a,0,i,h);
                if(ans>max)
                    max=ans;
            }
            System.out.println(ans);
        }

    }


    public static int CostPath(int a[][],int x,int y,HashMap<String,Integer>h){
        if(h.containsKey(x+","+y))
            return h.get(x+","+y);
        int r;
        if(x>=a.length || y>= a.length || x<0 || y<0 ){
            r= Integer.MIN_VALUE;
        }
        else if(x==a.length-1 ){
            r= a[x][y];
        }

        else{
            r= a[x][y]+Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y-1,h));
        }
        h.put(x+","+y,r);
        return r;
    }
}

这应该给出输出 13785 但它给出了 10689

【问题讨论】:

  • 有什么问题?您是否收到错误、不正确的解决方案或其他问题?
  • 你学过动态编程吗,如果不看看 LCS 问题。非常相似的想法。
  • 欢迎来到 SO。为了使帮助更容易,请对测试数据进行硬编码(而不是使用扫描仪)。还包括预期结果。
  • @skandigraun 是的,我得到不正确的输出而不是 13785 我得到 10689
  • @c0der 进行了更改,硬编码输入

标签: java matrix dynamic-programming


【解决方案1】:

代码中有两个小错误:

r = a[x][y]+ Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y-1,h)); 

错了(CostPath(a,x+1,y-1,h)被计算了两次)应该是:

r = a[x][y]+ Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y+1,h)); 

还有这个

System.out.println(ans);

应该改为

System.out.println(max);

进行这两项更改后,输出是正确的。 旁注:
无需将无效 x,y 值添加到地图路径中。为了防止它并使程序更有效地改变

r= Integer.MIN_VALUE;

return 0; 

【讨论】:

    【解决方案2】:

    这是动态编程解决方案。这里不需要存储一个HashMap,甚至路径本身。笔记;我删除了路径信息,它是一个二维数组来存储最大值的来源。

    如您所见,这里没有递归。如果你从底层看递归,你就会明白这段代码。

    注意;你输入的结果是 13785。

    import java.util.*;
    
    public class path {
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t  =1;
            int n;
            int values[][];
    
            while(t-->0){
    
                n = 17;
                values = new int [n][n];
    
                int input[]={67,280,171,381,930,781,925,4,393,380,246,433,762,258,5,166,315,503,385,728,854,350,464,288,304,80,689,56,313,843,92,379,122,614,111,403,394,387,406,138,767,651,571,880,260,927,398,926,429,782,653,634,132,468,274,435,548,314,490,212,156,933,942,629,546,404,31,292,142,436,781,260,86,703,140,697,630,537,622,410,318,275,44,801,94,669,236,993,982,77,204,137,10,497,765,907,900,147,550,42,582,331,301,19,33,792,715,14,680,336,424,350,962,467,150,408,135,737,400,468,814,956,956,175,452,72,433,704,218,983,97,799,665,749,169,49,541,883,63,572,570,486,921,884,304,423,291,790,159,42,257,324,997,212,498,801,283,283,504,500,617,952,650,281,700,818,329,592,52,743,164,621,228,436,856,883,858,498,672,17,540,928,340,536,139,190,336,773,472,191,272,88,142,921,720,842,90,400,433,141,143,948,114,722,384,969,605,593,819,276,961,358,556,301,893,46,842,581,819,665,771,90,104,265,363,823,106,452,574,890,945,68,190,58,790,925,378,746,517,196,373,478,905,280,130,798,326,323,730,144,987,500,585,90,764,947,264,221,751,837,463,47,257,652,456,46,576,185,143,444,381,867,921,285,147,402,434,472,724,163,615,710,15,551,151,130,498,414,703};
                int k=0;
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        values[i][j] = input[k];
                        k++;
                    }
                }
    
                for ( int r = n-2 ; r >=0 ; r-- ) {
    
                    for ( int c = 0 ; c < n ; c++ ) {
    
                        if (c == 0) {
                            values[r][c] += Math.max(values[r+1][c], values[r+1][c+1]);
    
                        } else if (c == n-1) {
                            values[r][c] += Math.max(values[r+1][c], values[r+1][c-1]);
                        } else {
                            values[r][c] += Math.max(values[r+1][c], Math.max(values[r+1][c-1], values[r+1][c+1]));
                        }
                    }
                }
    
                int max = values[0][0];
    
                for(int c = 1; c <= n-1 ; c++) 
                    max = Math.max(max, values[0][c]);
    
                System.out.println(max);
            }
        }
    }
    

    【讨论】:

    • while(t--&gt;0){ 是什么?也正如发布的那样,输出应该是 13785
    • 它来自原始代码,我认为它用于T测试。我没删。我知道它发布为 13785,但我很确定,无论如何,我会再次检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2019-10-24
    • 2014-10-28
    • 2021-11-12
    • 1970-01-01
    • 2018-12-09
    • 2022-01-23
    相关资源
    最近更新 更多