Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class ShortestPath {
  5.  
  6. private CityMap cityMap;
  7.  
  8. public ShortestPath(CityMap cityMap) {
  9. super();
  10. this.cityMap = cityMap;
  11. }
  12.  
  13. private boolean isValidUpPath(MapCell cell) {
  14. if (cell.isNorthRoad() || cell.isDestination() ||cell.isIntersection()) {
  15. return true;
  16. }
  17.  
  18. return false;
  19.  
  20. }
  21.  
  22. private boolean isValidDownPath(MapCell cell) {
  23. if (cell.isSouthRoad() || cell.isDestination() ||cell.isIntersection()) {
  24. return true;
  25. }
  26.  
  27. return false;
  28.  
  29. }
  30.  
  31. private boolean isValidLeftPath(MapCell cell) {
  32. if (cell.isWestRoad() || cell.isDestination() ||cell.isIntersection()) {
  33. return true;
  34. }
  35.  
  36. return false;
  37.  
  38. }
  39.  
  40. private boolean isValidRightPath(MapCell cell) {
  41. if (cell.isEastRoad() || cell.isDestination() ||cell.isIntersection()) {
  42. return true;
  43. }
  44.  
  45. return false;
  46.  
  47. }
  48.  
  49. private boolean isValidPath(MapCell cell) {
  50. if (cell.isNorthRoad() || cell.isEastRoad() || cell.isSouthRoad() || cell.isWestRoad() || cell.isDestination() ||
  51. cell.isIntersection()) {
  52. return true;
  53. }
  54.  
  55. return false;
  56.  
  57. }
  58.  
  59. public void findShortestPath() {
  60. OrderedCircularArray orderedList = new OrderedCircularArray();
  61. MapCell theCell = cityMap.getStart();
  62. orderedList.insert(theCell, 0);
  63. theCell.markInList();
  64. int distanceToStart = 0;
  65. boolean destFound = false;
  66. MapCell currentCell = null;
  67.  
  68.  
  69. while (!orderedList.isEmpty()) {
  70.  
  71. currentCell = (MapCell) orderedList.getSmallest();
  72.  
  73. if (currentCell.isDestination()) {
  74. destFound = true;
  75. currentCell.markOutList();
  76. break;
  77. }
  78.  
  79. currentCell.markOutList();
  80.  
  81. ArrayList neibourCells = this.nextCells(currentCell);
  82. if (neibourCells != null) {
  83. for (int i = 0; i < neibourCells.size(); i++) {
  84. MapCell neibourCell = (MapCell) neibourCells.get(i);
  85. distanceToStart = currentCell.getDistanceToStart() + 1;
  86. int nerbourDistance = neibourCell.getDistanceToStart();
  87. if(distanceToStart < nerbourDistance) {
  88. nerbourDistance = distanceToStart;
  89. neibourCell.setDistanceToStart(distanceToStart);
  90. neibourCell.setPredecessor(currentCell);
  91. }
  92.  
  93. if(neibourCell.isMarkedInList()) {
  94. int existingDistance = orderedList.getValue(neibourCell);
  95. if (nerbourDistance < existingDistance) {
  96. orderedList.changeValue(neibourCell, nerbourDistance);
  97. }
  98. }
  99. else {
  100. orderedList.insert(neibourCell, nerbourDistance);
  101. neibourCell.markInList();
  102. }
  103.  
  104.  
  105. }
  106. }
  107.  
  108. }
  109.  
  110. if(destFound) {
  111. System.out.println("Destination reached.");
  112. System.out.println("The number of cells in the path from the starting cell to the destination = "+(currentCell.getDistanceToStart()));
  113. }
  114. else {
  115. System.out.println("Destination cannot be reached.");
  116. }
  117.  
  118.  
  119. }
  120.  
  121.  
  122. private MapCell nextCell(MapCell cell) {
  123. MapCell bestCell = null;
  124. int destIndex = -1;
  125. int crossIndex = -1;
  126. int roadIndex = -1;
  127.  
  128. if (cell.isNorthRoad() && cell.getNeighbour(0) != null
  129. ) {
  130. if(isValidUpPath(cell.getNeighbour(0))&& !cell.getNeighbour(0).isMarked())
  131. return cell.getNeighbour(0);
  132. else
  133. return null;
  134. }
  135.  
  136. else if (cell.isEastRoad() && cell.getNeighbour(1) != null ) {
  137.  
  138. if(isValidRightPath(cell.getNeighbour(1))&& !cell.getNeighbour(1).isMarked())
  139. return cell.getNeighbour(1);
  140. else
  141. return null;
  142. }
  143.  
  144.  
  145. else if (cell.isSouthRoad() && cell.getNeighbour(2) != null ) {
  146. if(isValidDownPath(cell.getNeighbour(2))&& !cell.getNeighbour(2).isMarked())
  147. return cell.getNeighbour(2);
  148. else
  149. return null;
  150. }
  151.  
  152. else if (cell.isWestRoad() && cell.getNeighbour(3) != null ) {
  153.  
  154. if(isValidLeftPath(cell.getNeighbour(3))&& !cell.getNeighbour(3).isMarked())
  155. return cell.getNeighbour(3);
  156. else
  157. return null;
  158. }
  159.  
  160. else {
  161. for (int i = 0; i < 4; i ++) {
  162.  
  163. // if cell exists
  164. if (cell.getNeighbour(i) != null) {
  165.  
  166. // if cell is NOT marked
  167. if (!cell.getNeighbour(i).isMarked()) {
  168.  
  169. if (cell.getNeighbour(i).isDestination()) {
  170.  
  171. destIndex = i;
  172. }
  173.  
  174. else if (cell.getNeighbour(i).isIntersection()) {
  175. if (crossIndex == -1)crossIndex = i;
  176. //bestCell = cell.getNeighbour(i);
  177. }
  178.  
  179. else if ((cell.getNeighbour(i).isNorthRoad() && i == 0) || (cell.getNeighbour(i).isSouthRoad() && i == 2) ||
  180. (cell.getNeighbour(i).isWestRoad() && i == 3) || (cell.getNeighbour(i).isEastRoad() && i == 1)) {
  181. if (roadIndex == -1)roadIndex = i;
  182. //bestCell = cell.getNeighbour(i);
  183. }
  184.  
  185.  
  186. }
  187. }
  188. }
  189.  
  190. if (destIndex > -1)
  191. return cell.getNeighbour(destIndex);
  192. else if (crossIndex > -1)
  193. return cell.getNeighbour(crossIndex);
  194. else if (roadIndex > -1)
  195. return cell.getNeighbour(roadIndex);
  196. }
  197. return bestCell;
  198. }
  199.  
  200. private ArrayList<MapCell> nextCells(MapCell cell) {
  201. ArrayList<MapCell> bestCells = new ArrayList<MapCell>();
  202.  
  203. if (cell.isNorthRoad() && cell.getNeighbour(0) != null ) {
  204. if(isValidUpPath(cell.getNeighbour(0))&& !cell.getNeighbour(0).isMarked()) {
  205. bestCells.add(cell.getNeighbour(0));
  206. return bestCells;
  207. }
  208. else
  209. return null;
  210. }
  211.  
  212. else if (cell.isEastRoad() && cell.getNeighbour(1) != null ) {
  213.  
  214. if(isValidRightPath(cell.getNeighbour(1))&& !cell.getNeighbour(1).isMarked()) {
  215. bestCells.add(cell.getNeighbour(1));
  216. return bestCells;
  217. }
  218. else
  219. return null;
  220. }
  221.  
  222.  
  223. else if (cell.isSouthRoad() && cell.getNeighbour(2) != null ) {
  224. if(isValidDownPath(cell.getNeighbour(2))&& !cell.getNeighbour(2).isMarked()) {
  225. bestCells.add(cell.getNeighbour(2));
  226. return bestCells;
  227. }
  228. else
  229. return null;
  230. }
  231.  
  232. else if (cell.isWestRoad() && cell.getNeighbour(3) != null ) {
  233.  
  234. if(isValidLeftPath(cell.getNeighbour(3))&& !cell.getNeighbour(3).isMarked()) {
  235. bestCells.add(cell.getNeighbour(3));
  236. return bestCells;
  237. }
  238.  
  239. else
  240. return null;
  241. }
  242. else {
  243. for (int i = 0; i < 4; i ++) {
  244.  
  245. // if cell exists
  246. if (cell.getNeighbour(i) != null && !cell.getNeighbour(i).isMarked()) {
  247.  
  248. if (cell.getNeighbour(i).isDestination() || cell.getNeighbour(i).isIntersection()) {
  249.  
  250. bestCells.add(cell.getNeighbour(i));
  251.  
  252. }
  253.  
  254. else if ((cell.getNeighbour(i).isNorthRoad() && i == 0) || (cell.getNeighbour(i).isSouthRoad() && i == 2) ||
  255. (cell.getNeighbour(i).isWestRoad() && i == 3) || (cell.getNeighbour(i).isEastRoad() && i == 1)) {
  256.  
  257. bestCells.add(cell.getNeighbour(i));
  258. }
  259.  
  260. }
  261. }
  262. }
  263.  
  264. return bestCells;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement