elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Ayuda backtraking laberinto
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda backtraking laberinto  (Leído 1,390 veces)
fenixskate

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Ayuda backtraking laberinto
« en: 14 Octubre 2015, 19:57 pm »

Hola amigos, espero y se encuentren bien, queria saber si me pueden ayudar, necesito hacer un backtraking de laberinto tengo q leer un txt, y guardarlo en un arreglo bidimensional y luego hacer la busqueda mas corta, ya tengo la parte donde lee el txt


Código
  1. if (tecla == KeyEvent.VK_R) {
  2. ObtenerTxt txt = new ObtenerTxt();
  3.  
  4. txt.obtieneLaberinto();
  5.  
  6.  
  7. }
  8.  
  9. public class ObtenerTxt {
  10. char entradaE = 'e';
  11. char salidaS = 's';
  12. int inicioFila = 0;
  13. int inicioColumna = 0;
  14. int finFila = 0;
  15. int finColumna = 0;
  16. int tam = 0;
  17.  
  18. char[][] lectura = new char[15][15];
  19.  
  20. public char[][] obtieneLaberinto() {
  21.  
  22. try {
  23. String cadena = "";
  24. int filas = 0;
  25. int columnas = 0;
  26. //JFileChooser file = new JFileChooser();
  27.  
  28. //int seleccion = file.showOpenDialog(null);
  29.  
  30. //file.showOpenDialog(null);
  31.  
  32. //if (seleccion == JFileChooser.APPROVE_OPTION) {
  33. File abre = new File("C:\\Users\\pionitos\\Documents\\Nueva Carpeta (3)\\x.txt"); // file.getSelectedFile();
  34. FileReader fr = new FileReader(abre);
  35. BufferedReader entrada = new BufferedReader(fr);
  36. while ((cadena = entrada.readLine()) != null) {
  37. for (int i = 0; i < cadena.length(); i++) {
  38. lectura[filas][columnas] = cadena.charAt(i);
  39. this.setLectura(lectura);
  40.  
  41. this.setTam(tam = cadena.length());
  42. if (cadena.charAt(i) == entradaE) {
  43. this.setInicioFila(filas);
  44. this.setInicioColumna(columnas);
  45. }
  46. if (cadena.charAt(i) == salidaS) {
  47. this.setFinFila(filas);
  48. this.setFinColumna(columnas);
  49. }
  50. columnas++;
  51. }
  52.  
  53. filas++;
  54. columnas = 0;
  55.  
  56. }
  57. //}
  58.  
  59. for (int i = 0; i < lectura.length; i++) {
  60. for (int j = 0; j < lectura.length; j++) {
  61. System.out.print(lectura[i][j]);
  62.  
  63. }
  64. System.out.println("");
  65. }
  66. return lectura;
  67. } catch (IOException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. return null;
  71. }
  72. }
  73.  
  74. public int getTam() {
  75. return tam;
  76. }
  77.  
  78. public void setTam(int tam) {
  79. this.tam = tam;
  80. }
  81.  
  82. public char[][] getLectura() {
  83. return lectura;
  84. }
  85.  
  86. public void setLectura(char[][] lectura) {
  87. this.lectura = lectura;
  88. }
  89.  
  90. public int getInicioFila() {
  91. return inicioFila;
  92. }
  93.  
  94. public void setInicioFila(int inicioFila) {
  95. this.inicioFila = inicioFila;
  96. }
  97.  
  98. public int getInicioColumna() {
  99. return inicioColumna;
  100. }
  101.  
  102. public void setInicioColumna(int inicioColumna) {
  103. this.inicioColumna = inicioColumna;
  104. }
  105.  
  106. public int getFinFila() {
  107. return finFila;
  108. }
  109.  
  110. public void setFinFila(int finFila) {
  111. this.finFila = finFila;
  112. }
  113.  
  114. public int getFinColumna() {
  115. return finColumna;
  116. }
  117.  
  118. public void setFinColumna(int finColumna) {
  119. this.finColumna = finColumna;
  120. }
  121.  
  122. }
  123.  
  124.  
  125. y la parte de busqueda
  126. public class Laberinto {
  127.  
  128. private static char L = 'L'; // pared
  129. private static char E = '0'; // espacio en blanco
  130. private static char MARCA = '*'; // señala las posiciones visitadas
  131. private static int x0 = 1; // inicio X
  132. private static int y0 = 0; // inicio Y
  133. /*
  134. * private static int xf = 11; // salida X private static int yf = 20; //
  135. * salida Y
  136. */
  137.  
  138. private static ObtenerTxt txt = new ObtenerTxt();
  139.  
  140. public static boolean valida(int f, int c) {
  141. // controla si la posicion esta fuera del laberinto
  142. if (f < 0 || f == txt.inicioFila || c < 0 || c == txt.inicioColumna) {
  143. return false;
  144. }
  145. // controla si la posicion ya fue visitada o es muro
  146. if (txt.getLectura()[f][c] == MARCA || txt.getLectura()[f][c] == L) {
  147. return false;
  148. }
  149. return true;
  150. }
  151.  
  152. public static boolean recorre(int fil, int col) {
  153. boolean listo = false; // Indica si se ha encontrado la salida
  154. // Se marca la casilla como visitada
  155. txt.getLectura()[fil][col] = MARCA;
  156. // Condicion de termino de recursividad: " Llegamos a la salida ?"
  157. if (fil == txt.finFila - 2 && col == txt.finColumna - 1) {
  158. return (true);
  159. }
  160. if (!listo && valida(fil - 1, col)) { /* Intento hacia arriba */
  161. listo = recorre(fil - 1, col);
  162. }
  163. if (!listo && valida(fil, col + 1)) { /* Intento a la derecha */
  164. listo = recorre(fil, col + 1);
  165. }
  166. if (!listo && valida(fil + 1, col)) { /* Intento hacia abajo */
  167. listo = recorre(fil + 1, col);
  168. }
  169. if (!listo && valida(fil, col - 1)) { /* Intento a la izquierda */
  170. listo = recorre(fil, col - 1);
  171. }
  172.  
  173. if (!listo) {
  174. txt.getLectura()[fil][col] = E;
  175. }
  176. // Se retorna true / false dependiendo de si se encontro solucion
  177. return (listo);
  178. }
  179.  
  180. public static void main(String[] args) {
  181. System.out.println("LABERINTO PROPUESTO:");
  182. desplegarArreglo(txt.lectura);
  183. System.out.println("LABERINTO RESUELTO");
  184. recorre(x0, y0);
  185. desplegarArreglo(txt.lectura);
  186. }
  187.  
  188. public static void desplegarArreglo(char[][] matriz) {
  189. for (int i = 0; i < matriz.length; ++i) {
  190. String texto = new String("");
  191. for (int j = 0; j < matriz.length; ++j) {
  192. texto += matriz[i][j] + " ";
  193. }
  194. System.out.println(texto);
  195. }
  196. }
  197. }
pero al momento de querer buscar no logro que me muestre elr esultado, espero y me puedan orientar. gracias
este es mi resultado
este es mi txt.
Código:
LLLLLLLLLLLLLLL
L0000000000000L
L0000000000000L
L0000000000000L
L0000000000000L
L0000000000000L
L00000s0000000L
L0000000000000L
L0000000000000L
L000000000e000L
L0000000000000L
L0000000000000L
L0000000000000L
L0000000000000L
LLLLLLLLLLLLLLL

la parte e = entrada, s= salida.

me podrian ayudar, ya que no estoy muy familiarizado con los arreglos. o explicar por favor gracias


Mod: Los códigos deben ir en etiquetas GeSHi


« Última modificación: 14 Octubre 2015, 19:58 pm por engel lex » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Ayuda] Necesito el Laberinto de MCKSys Argentina
Programación Visual Basic
sebah97 5 3,232 Último mensaje 23 Junio 2010, 19:37 pm
por sebah97
Ayuda con laberinto en una matriz
Programación C/C++
edotropic 5 4,474 Último mensaje 20 Diciembre 2013, 13:29 pm
por leosansan
urgente!ayuda, Laberinto C++
Programación C/C++
RuKsu 7 5,313 Último mensaje 7 Diciembre 2014, 05:41 am
por sebah97
Ayuda, Codigo Laberinto
Programación C/C++
RuKsu 0 2,600 Último mensaje 10 Diciembre 2014, 21:03 pm
por RuKsu
ayuda urgente laberinto con grafos
Programación C/C++
blaaaack 0 3,730 Último mensaje 20 Junio 2017, 02:33 am
por blaaaack
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines