Vous êtes sur la page 1sur 3

import java.util.HashSet; import java.util.

Set; public class Test { final final final final static static static static int int int int abierta = 0; pared = 1; objetivo = 2; visitado =-3;

static int[][] campo = { { 0, 0, 0, 0, 0, 1 }, { 1, 0, 1, 1, 0, 1 }, { 1, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 1, 2 }, { 1, 0, 1, 0, 0, 0 } }; // // // // // // // // asi es como se ve 0 0 0 0 0 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 1 1 1 0 1 0 0 0 ancho= campo.length; altura = campo[0].length; xIniciar = 0; yIniciar = 0; //Iniciamos a la posicin inicial: (x = 0, y = 0) nrSoluciones = 0; // Numero de soluciones

static static static static static

int int int int int

// Se utiliza para almacenar id: s de nodos sin salida. // El ID entero es (x + y * anchura) static Set<Integer> callejosSinSalida = new HashSet<Integer>(); public static void main(String[] arg) { System.out.println("Laberinto Inicial:"); ImprimirCampo(); EncontrarCamino(xIniciar, yIniciar); System.out.println("Nmero de soluciones: " + nrSoluciones); System.out.println("Nmero de callejones sin salida: " + callejosSinSalida .size()); } private static void EncontrarCamino(int x, int y) { if (x < 0 || y < 0 || x >= ancho || y >= altura) { // paso 1 return; } else if (campo[x][y] == objetivo) { // paso 2 nrSoluciones++; System.out.println("Solucin nr " + nrSoluciones + ":"); ImprimirCampo(); return; } else if (campo[x][y] != abierta) { // paso 3 return; } else if (CallejonSinSalida(x, y)) { // Investigacin-paso sin salida adi

cional int uniqueNodeId = x + y * ancho; callejosSinSalida.add(uniqueNodeId); // Report as dead end return; } campo[x][y] = visitado; // paso 4 EncontrarCamino(x, y EncontrarCamino(x + 1, EncontrarCamino(x, y + EncontrarCamino(x - 1, 1); y); 1); y); // // // // paso paso paso paso 5, 6, 7, 8, ir ir ir ir hacia hacia hacia hacia el el el el norte este sur oeste

campo[x][y] = abierta; // paso 9 // // // // } Paso 10 no es realmente necesario, ya que el booleano pretende Mostrar slo si no se ha encontrado una solucin. Esta implementacin Utiliza un int para registrar el nmero de soluciones en su lugar. El valor de retorno sera (nrSoluciones! = 0)

private static boolean CallejonSinSalida(int x, int y) { int nrVisitedNeighbours = 0; if (y > 0) { //si existe vecino del norte if (campo[x][y - 1] == visitado) { nrVisitedNeighbours++; } else if (campo[x][y - 1] != pared) { return false; } } if (x < ancho - 1) { // si existe vecino del este if (campo[x + 1][y] == visitado) { nrVisitedNeighbours++; } else if (campo[x + 1][y] != pared) { return false; } } if (y < altura - 1) { // si existe vecino del sur if (campo[x][y + 1] == visitado) { nrVisitedNeighbours++; } else if (campo[x][y + 1] != pared) { return false; } } if (x > 0) { // si existe vecino occidental if (campo[x - 1][y] == visitado) { nrVisitedNeighbours++; } else if (campo[x - 1][y] != pared) { return false; } } if (nrVisitedNeighbours > 1) { //escenario ruta circular return false; } return true; // El resultado de esta comprobacin exhaustiva: este es un c allejn sin Fin }

private static void ImprimirCampo() { for (int yy = 0; yy < campo[0].length; yy++) { for (int xx = 0; xx < campo.length; xx++) { System.out.print(campo[xx][yy] + " "); } System.out.println(); } System.out.println(); } }

Vous aimerez peut-être aussi