Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package solvemazes;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.Scanner;
  11.  
  12. /**
  13. *
  14. * @author bcaunter5094
  15. */
  16. public class SolveMazes {
  17.  
  18. /**
  19. * 7,11
  20. %e%%%%%%%%%
  21. %...%.%...%
  22. %.%.%.%.%%%
  23. %.%.......%
  24. %.%%%%.%%.%
  25. %.%.....%.%
  26. %%%%%%%%%x%
  27. */
  28. static boolean found = false;
  29. static char[][] solution;
  30.  
  31. public static void main(String[] args) throws FileNotFoundException {
  32. Scanner fileInput = new Scanner(new File("maze1.txt"));
  33. int dimX = 0, dimY = 0;
  34. String temp = "";
  35. if (fileInput.hasNextLine()) {
  36. temp = fileInput.nextLine();
  37. dimY = Integer.parseInt(temp.substring(0, temp.indexOf(",")));
  38. dimX = Integer.parseInt(temp.substring(temp.indexOf(",") + 1, temp.length()));
  39.  
  40. }
  41. char[][] maze = new char[dimX][dimY];
  42. int ya = 0;
  43. while (fileInput.hasNextLine()) {
  44. temp = fileInput.nextLine();
  45. for (int xa = 0; xa < temp.length(); xa++) {
  46. maze[xa][ya] = temp.charAt(xa);
  47. }
  48. ya++;
  49. }
  50. boolean bigbreak = false;
  51. found = false;
  52. for (int yy = 0; yy < dimY; yy++) {
  53. if (bigbreak) {
  54. break;
  55. }
  56. for (int xx = 0; xx < dimX; xx++) {
  57. if (bigbreak) {
  58. break;
  59. }
  60. System.out.print(maze[xx][yy]);
  61. if (maze[xx][yy] == 'e') {
  62. findSolution(xx, yy, maze, dimX, dimY);
  63. bigbreak = true;
  64. break;
  65. }
  66. }
  67. }
  68.  
  69. // if (found) {
  70. // for (int yy = 0; yy < dimY; yy++) {
  71. // for (int xx = 0; xx < dimX; xx++) {
  72. // System.out.print(solution[xx][yy]);
  73. // }
  74. // System.out.println("");
  75. // }
  76. // }else{
  77. // System.out.println("no solution found");
  78. // }
  79.  
  80. }
  81.  
  82. public static void findSolution(int x, int y, char[][] maze, int dimX, int dimY) {
  83. //if (!found) {
  84. for (int yy = -1; yy <= 1; yy++) {
  85. for (int xx = -1; xx <= 1; xx++) {
  86. if (xx == 0 || yy == 0 && (xx != yy)) {//only left right up down
  87.  
  88.  
  89. if (x + xx >= 0 && x + xx < dimX && y + yy >= 0 && y + yy < dimY) { //in range
  90.  
  91. if (maze[x + xx][y + yy] != '%' && maze[x + xx][y + yy] != '+') {//valid move
  92. maze[x][y] = '+';
  93.  
  94. //System.out.println("moving dir"+xx+","+yy);
  95. // for (int yt = 0; yt < dimY; yt++) {
  96. // for (int xt = 0; xt < dimX; xt++) {
  97. // System.out.print(maze[xt][yt]);
  98. // }
  99. // System.out.println("");
  100. // }
  101.  
  102.  
  103. if (maze[x + xx][y + yy] == 'x') {
  104. System.out.println("SOLUTIONnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
  105. for (int yt = 0; yt < dimY; yt++) {
  106. for (int xt = 0; xt < dimX; xt++) {
  107. System.out.print(maze[xt][yt]);
  108. }
  109. System.out.println("");
  110. }
  111. xx = 1;
  112. yy = 1;
  113. found = true;
  114. } else {
  115. findSolution(x + xx, y + yy, maze, dimX, dimY);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. //}
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement