A
You have to make the possibilities explored so you don't have to use if else because that means it's only going to take a path (if you enter a if others are not evaluated because of the else), you have to use only if you do something like this:public static void encontrarCamino(int[][] matriz, int i, int j, int valor, int valorAnterior){
int maximo = obtenerMaximo(matriz);
//Va imprimiendo la secuencia de valores que sigue.
System.out.println("(" + i + ", " + j + ") / " + valor);
//Comprueba si puede moverse a cada una de las posiciones y si es asi se mueve a ella de forma recursiva.
if( i - 1 >= 0 && j - 1 >= 0 && i - 1 < matriz.length && j - 1 < matriz[i].length && matriz[i - 1][j - 1] >= valor){
encontrarCamino(matriz, i - 1, j - 1, matriz[i - 1][j - 1], valor);}
if( i - 1 >= 0 && j >= 0 && i - 1 < matriz.length && j < matriz[i].length && matriz[i - 1][j] >= valor ){
encontrarCamino(matriz, i - 1, j, matriz[i - 1][j], valor);}
if( i - 1 >= 0 && j + 1 >= 0 && i - 1 < matriz.length && j + 1 < matriz[i].length && matriz[i - 1][j + 1] >= valor){
encontrarCamino(matriz, i - 1, j + 1, matriz[i - 1][j + 1], valor);}
if( i >= 0 && j - 1 >= 0 && i < matriz.length && j - 1 < matriz[i].length && matriz[i][j - 1] >= valor){
encontrarCamino(matriz, i, j - 1, matriz[i][j - 1], valor);}
if( i >= 0 && j + 1 >= 0 && i < matriz.length && j + 1 < matriz[i].length && matriz[i][j + 1] >= valor){
encontrarCamino(matriz, i, j + 1, matriz[i][j + 1], valor);}
if( i + 1 >= 0 && j - 1 >= 0 && i + 1 < matriz.length && j - 1 < matriz[i].length && matriz[i + 1][j - 1] >= valor){
encontrarCamino(matriz, i + 1, j - 1, matriz[i + 1][j - 1], valor);}
if( i + 1 >= 0 && j >= 0 && i + 1 < matriz.length && j < matriz[i].length && matriz[i + 1][j] >= valor){
encontrarCamino(matriz, i + 1, j, matriz[i + 1][j], valor);}
if( i + 1 >= 0 && j + 1 >= 0 && i + 1 < matriz.length && j + 1 < matriz[i].length && matriz[i + 1][j + 1] >= valor){
encontrarCamino(matriz, i + 1, j + 1, matriz[i + 1][j + 1], valor);}
if(valor == maximo){
System.out.println("El ultimo valor es el maximo");}
else{
System.out.println("No es posible alcanzar el maximo");
}
}
With this it is possible to go back but you are losing knowing if it was possible to reach the minimum or not (I would always say that "The last value is the maxim" or that "It is not possible to reach the maximum"). For this you can use a boolean thereCamino with initial value in false and make it true if there is at least one way remaining the code as follows:public static void encontrarCamino(int[][] matriz, int i, int j, int valor, int valorAnterior){
int maximo = obtenerMaximo(matriz);
boolean hayCamino = false;
//Va imprimiendo la secuencia de valores que sigue.
System.out.println("(" + i + ", " + j + ") / " + valor);
//Comprueba si puede moverse a cada una de las posiciones y si es asi se mueve a ella de forma recursiva.
if( i - 1 >= 0 && j - 1 >= 0 && i - 1 < matriz.length && j - 1 < matriz[i].length && matriz[i - 1][j - 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i - 1, j - 1, matriz[i - 1][j - 1], valor);}
if( i - 1 >= 0 && j >= 0 && i - 1 < matriz.length && j < matriz[i].length && matriz[i - 1][j] >= valor ){
hayCamino = true;
encontrarCamino(matriz, i - 1, j, matriz[i - 1][j], valor);}
if( i - 1 >= 0 && j + 1 >= 0 && i - 1 < matriz.length && j + 1 < matriz[i].length && matriz[i - 1][j + 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i - 1, j + 1, matriz[i - 1][j + 1], valor);}
if( i >= 0 && j - 1 >= 0 && i < matriz.length && j - 1 < matriz[i].length && matriz[i][j - 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i, j - 1, matriz[i][j - 1], valor);}
if( i >= 0 && j + 1 >= 0 && i < matriz.length && j + 1 < matriz[i].length && matriz[i][j + 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i, j + 1, matriz[i][j + 1], valor);}
if( i + 1 >= 0 && j - 1 >= 0 && i + 1 < matriz.length && j - 1 < matriz[i].length && matriz[i + 1][j - 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i + 1, j - 1, matriz[i + 1][j - 1], valor);}
if( i + 1 >= 0 && j >= 0 && i + 1 < matriz.length && j < matriz[i].length && matriz[i + 1][j] >= valor){
hayCamino = true;
encontrarCamino(matriz, i + 1, j, matriz[i + 1][j], valor);}
if( i + 1 >= 0 && j + 1 >= 0 && i + 1 < matriz.length && j + 1 < matriz[i].length && matriz[i + 1][j + 1] >= valor){
hayCamino = true;
encontrarCamino(matriz, i + 1, j + 1, matriz[i + 1][j + 1], valor);}
if(!hayCamino){
if(valor == maximo){
System.out.println("El ultimo valor es el maximo");}
else{
System.out.println("No es posible alcanzar el maximo");
}
}
}
Note:As you are checking all possible paths you will imprison several "The last value is the maxim" or "It is not possible to reach the maximum" because there isCamino = false you will print one thing or the other and there isCamino will be false whenever a road is final (there is no more place to go or get to the maximum) and this will happen a lot. To change this behavior, the signature of the method should be changed.With this it is not possible to correctly indicate in the standard output as a sequence of matrix positions along with the value it contains only if you found a way or not for this you should also change the signature of the method.