Guest User

Untitled

a guest
Oct 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. /**
  2. * Created by Ilya Gazman on 10/17/2018.
  3. */
  4. public class BFS {
  5.  
  6. private static final boolean DEBUG = false;
  7.  
  8. public ArrayList<Point> findPath(int[][] map, Point position, Point destination) {
  9. if (isOutOfMap(map, position)) {
  10. return null;
  11. }
  12. if (isOutOfMap(map, destination)) {
  13. return null;
  14. }
  15. if (isBlocked(map, position)) {
  16. return null;
  17. }
  18. if (isBlocked(map, destination)) {
  19. return null;
  20. }
  21. LinkedList<Point> queue1 = new LinkedList<>();
  22. LinkedList<Point> queue2 = new LinkedList<>();
  23.  
  24. queue1.add(position);
  25. int stepCount = 1;
  26. while (!queue1.isEmpty()) {
  27. for (Point point : queue1) {
  28. map[point.y][point.x] = -stepCount;
  29. }
  30.  
  31. for (Point point : queue1) {
  32. if (point.x == destination.x && point.y == destination.y) {
  33. ArrayList<Point> optimalPath = new ArrayList<>();
  34. computeSolution(map, point.x, point.y, stepCount, optimalPath);
  35. resetMap(map);
  36. return optimalPath;
  37. }
  38. LinkedList<Point> finalQueue = queue2;
  39. lookAround(map, point, (x, y) -> {
  40. if (isBlocked(map, x, y)) {
  41. return;
  42. }
  43. finalQueue.add(new Point(x, y));
  44. });
  45. }
  46.  
  47. if (DEBUG) {
  48. printMap(map);
  49. }
  50.  
  51. queue1 = queue2;
  52. queue2 = new LinkedList<>();
  53. stepCount++;
  54. }
  55. resetMap(map);
  56. return null;
  57. }
  58.  
  59. private void resetMap(int[][] map) {
  60. for (int y = 0; y < map.length; y++) {
  61. for (int x = 0; x < map[0].length; x++) {
  62. if (map[y][x] < 0) {
  63. map[y][x] = 0;
  64. }
  65. }
  66. }
  67. }
  68.  
  69. private boolean isBlocked(int[][] map, Point p) {
  70. return isBlocked(map, p.x, p.y);
  71. }
  72.  
  73. private boolean isBlocked(int[][] map, int x, int y) {
  74. int i = map[y][x];
  75. return i < 0 || i == 1;
  76. }
  77.  
  78. private void printMap(int[][] map) {
  79. for (int y = 0; y < map[0].length; y++) {
  80. for (int x = 0; x < map.length; x++) {
  81. System.out.print(map[y][x] + "t");
  82. }
  83. System.out.println();
  84. }
  85. System.out.println("****************************************");
  86. }
  87.  
  88. private void computeSolution(int[][] map, int x, int y, int stepCount, ArrayList<Point> optimalPath) {
  89. if (isOutOfMap(map, x, y)) {
  90. return;
  91. }
  92.  
  93. if (optimalPath.size() - stepCount != map[y][x]) {
  94. return;
  95. }
  96.  
  97. Point p = new Point(x, y);
  98. optimalPath.add(p);
  99. lookAround(map, p, (x1, y1) -> computeSolution(map, x1, y1, stepCount, optimalPath));
  100. }
  101.  
  102. private void lookAround(int[][] map, Point p, Callback callback) {
  103. callback.look(map, p.x + 1, p.y);
  104. callback.look(map, p.x - 1, p.y);
  105. callback.look(map, p.x, p.y + 1);
  106. callback.look(map, p.x, p.y - 1);
  107. callback.look(map, p.x + 1, p.y + 1);
  108. callback.look(map, p.x - 1, p.y + 1);
  109. callback.look(map, p.x - 1, p.y - 1);
  110. callback.look(map, p.x + 1, p.y - 1);
  111. }
  112.  
  113. private static boolean isOutOfMap(int[][] map, Point p) {
  114. return isOutOfMap(map, p.x, p.y);
  115. }
  116.  
  117. private static boolean isOutOfMap(int[][] map, int x, int y) {
  118. if (x < 0 || y < 0) {
  119. return true;
  120. }
  121. return map.length == y || map[0].length == x;
  122. }
  123.  
  124. private interface Callback {
  125. default void look(int[][] map, int x, int y) {
  126. if (isOutOfMap(map, x, y)) {
  127. return;
  128. }
  129. onLook(x, y);
  130. }
  131.  
  132. void onLook(int x, int y);
  133. }
  134. }
Add Comment
Please, Sign In to add comment