如何使用贪婪算法找到最短路径 我的代码是这样的,带有二维数组路径。输出始终为零或三。 你们能告诉我我的代码怎么了吗?
我想要的路径是: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);
}
}