tenho q resolver um labirinto a partir de uma matriz, onde 1 é parede e 0 é espaço vazio disponível pra passagem.
fiz esse código e ele retorna true se há solução para a matriz e false se não há solução.
Porém quando eu peço pra retornar a matriz depois de tentar resolvê-la, ela marca todos os locais tentados na solução, e gostaria de marcar apenas onde eu passei com a solução.
ex.:
{{0,1,0}, -->>> {{2,1,0},
{0,0,0}, -->>> {2,2,2},
{0,1,0}} -->>> {0,1,2}}
[code]public class Labirinto{
private int[][] matriz;
private int num = matriz.length;
boolean ehPossivel = false;
public Labirinto(int [][]m){
if(matriz.length == matriz[0].length){
matriz = m;
}
else {System.out.println(“matriz não quadrada.”);}
}
private boolean tenteMovimento(int x, int y) {
if (ehPossivel) return true;
if (!ehAceitavel(x,y)) return false;
if (matriz[x][y] != 0) return false;
if (x == (num-1) && y == (num-1))
{
matriz[x][y] = 2;
ehPossivel = true;
}
if(matriz[x][y] ==2 ){matriz[x][y] =0;}
matriz[x][y] = 2;
tenteMovimento(x + 1, y); // direita
tenteMovimento(x - 1, y); // esq
tenteMovimento(x, y + 1); // baixo
tenteMovimento(x, y - 1); // cima
return ehPossivel;
}
}[/code]