Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ShortestPath {
  4. private static Map map = new Map();
  5. private static Character[][] modifiedMap = map.getMap();
  6. private static boolean[][] visited = new boolean[Map.HEIGHT][Map.WIDTH];
  7.  
  8. public static int getCoordinateX(String coordinate){
  9. String x = coordinate.substring(0, 2);
  10. int coor = (x.charAt(0) - 'A') * 10 + Character.getNumericValue(x.charAt(1));
  11. return coor;
  12. }
  13.  
  14. public static int getCoordinateY(String coordinate){
  15. String y = coordinate.substring(2, 4);
  16. int coor = (y.charAt(0) - 'Q') * 10 + Character.getNumericValue(y.charAt(1));
  17. return coor;
  18. }
  19.  
  20. public static int calculateDistance(String from, String to){
  21. int xStart = getCoordinateX(from);
  22. int xEnd = getCoordinateX(to);
  23. int yStart = getCoordinateY(from);
  24. int yEnd = getCoordinateY(to);
  25.  
  26. int dist = 0;
  27. Point p = getPathBFS(xStart, yStart, xEnd, yEnd);
  28. while(p.getParent() != null) {
  29. modifiedMap[p.x][p.y] = '.';
  30. dist++;
  31. p = p.getParent();
  32. }
  33. modifiedMap[xEnd][yEnd] = 'F';
  34. printModifiedMap();
  35. System.out.println(dist);
  36. return dist;
  37. }
  38.  
  39. private static class Point {
  40. int x;
  41. int y;
  42. Point parent;
  43.  
  44. public Point(int x, int y, Point parent) {
  45. this.x = x;
  46. this.y = y;
  47. this.parent = parent;
  48. }
  49.  
  50. public Point getParent() {
  51. return this.parent;
  52. }
  53. }
  54.  
  55. public static Queue<Point> q = new LinkedList<Point>();
  56. public static Point getPathBFS(int x, int y, int xEnd, int yEnd) {
  57. for (int i=0;i<Map.HEIGHT;++i){
  58. for (int j=0;j<Map.WIDTH;++j){
  59. visited[i][j]=false;
  60. }
  61. }
  62. q.add(new Point(x,y, null));
  63.  
  64. modifiedMap[xEnd][yEnd] = 'F';
  65. visited[x][y] = true;
  66. modifiedMap[x][y] = 'S';
  67. while(!q.isEmpty()) {
  68. Point p = q.remove();
  69. if (modifiedMap[p.x][p.y] == 'F') {
  70. System.out.println("Exit is reached!");
  71. return p;
  72. }
  73.  
  74. if(isFree(p.x+1,p.y)) {
  75. Point nextP = new Point(p.x+1,p.y, p);
  76. visited[nextP.x][nextP.y] = true;
  77. q.add(nextP);
  78. }
  79.  
  80. if(isFree(p.x-1,p.y)) {
  81. Point nextP = new Point(p.x-1,p.y, p);
  82. visited[nextP.x][nextP.y] = true;
  83. q.add(nextP);
  84. }
  85.  
  86. if(isFree(p.x,p.y+1)) {
  87. Point nextP = new Point(p.x,p.y+1, p);
  88. visited[nextP.x][nextP.y] = true;
  89. q.add(nextP);
  90. }
  91.  
  92. if(isFree(p.x,p.y-1)) {
  93. Point nextP = new Point(p.x,p.y-1, p);
  94. visited[nextP.x][nextP.y] = true;
  95. q.add(nextP);
  96. }
  97.  
  98. }
  99. return null;
  100. }
  101.  
  102. public static boolean isFree(int x, int y) {
  103. if((x >= 0 && x < Map.HEIGHT) && (y >= 0 && y < Map.WIDTH)
  104. && (modifiedMap[x][y] == ' ' || modifiedMap[x][y] == 'F') && (!visited[x][y])) {
  105. return true;
  106. }
  107. return false;
  108. }
  109.  
  110. public static void printModifiedMap(){
  111. map.print();
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement