如何在路径查找器中使用贪心算法

如何使用贪婪算法找到最短路径 我的代码是这样的,带有二维数组路径。输出始终为零或三。 你们能告诉我我的代码怎么了吗?

我想要的路径是:1,1,3

        int[][] path = {{0,2,1,0},{0,3,4,3},0}};
        int count = 0;
        int node;
        for(int i = 0; i<path.length; i++){
            for(int j = 0; j < path.length; j++){
                    if(path[i][j] > 0 && j < path.length-1) {
                        if (path[i][j] < path[i][j + 1] ) {
                            if(path[i][j+1] == 0){
                                node = path[i][j];
                                count+= node;
                                i = j;
                                System.out.print(" "+node);
                            }else{
                                node = path[i][j];
                                count+= node;
                                i = j;
                                System.out.print(" "+node);
                            }
                        }
                    }else if (path[i][j] > 0 && j == path.length-1){
                        node = path[i][j];
                        count+= node;
                        i = j;
                        System.out.print(" "+node);
                    }
            }
        }
        System.out.println(" : "+count);
    }
}
cmwapfang 回答:如何在路径查找器中使用贪心算法

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2875391.html

大家都在问