Buscar

BuscaProfundidade

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ia_exercicio01;
/**
 *
 * @author marco.silva2
 */
public class BuscaProfundidade extends Busca {
 @Override
 public boolean busca(int i, int k) {
 count++;
 
 //valida se a posição esta dentro da matriz
 if ((i < 0 || i >=col) || (k < 0 || k >=lin)) {
 return false;
 //verifica se a posição é um obstaculo
 } else if (matriz[i][k] == '#') {
 return false;
 //verifica se a posição já foi percorrida
 } else if (matriz[i][k] == '-') {
 return false;
 //verifica se a posição é a final
 } else if (matriz[i][k] == 'F') {
 matriz[i][k] ='X';
 imprime();
 
 System.out.println("Movimentos: "+count);
 return true;
 } 
 else {
 matriz[i][k] = '-';
 count++;
 imprime();
 if (busca(i + 1, k)) {
 return true;
 } else if (busca(i + 1, k + 1)) {
 return true;
 } else if (busca(i + 1, k - 1)) {
 return true;
 } else if (busca(i, k + 1)) {
 return true;
 } else if (busca(i, k - 1)) {
 return true;
 } else if (busca(i - 1, k + 1)) {
 return true;
 } else if (busca(i - 1, k)) {
 return true;
 } else if (busca(i - 1, k - 1)) {
 return true;
 }
 }
 return false;
 }
 @Override
 public boolean buscaH(int i, int k, int fimX, int fimY) {
 throw new UnsupportedOperationException("Not supported yet.");
 }
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais