Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package edu.toronto.csc301.warehouse;
  2.  
  3. import edu.toronto.csc301.grid.GridCell;
  4. import edu.toronto.csc301.grid.IGrid;
  5. import edu.toronto.csc301.robot.IGridRobot;
  6.  
  7. import java.util.*;
  8.  
  9. public class GridBreadthFirstSearch {
  10.     private IGrid<Rack> grid;
  11.     public GridBreadthFirstSearch(IGrid<Rack> grid) {
  12.         this.grid = grid;
  13.     }
  14.  
  15.     /**
  16.      * Returns the next step direction on a shortest path from source to destination.
  17.      *
  18.      * NOTE: There may be more than one next step direction that can yield a particular
  19.      * path distance. This method returns an arbitrary direction, specifically the first
  20.      * direction attempted in the BFS yielding this path distance.
  21.      *
  22.      * @param source Source cell in the grid.
  23.      * @param destination Destination cell in the grid.
  24.      * @param otherRobotPositions Positions of other robots on the grid.
  25.      * @return Map of path distances to possible next step.
  26.      */
  27.     public IGridRobot.Direction search(GridCell source, GridCell destination, List<GridCell> otherRobotPositions) {
  28.         Queue<GridCell> unvisited = new LinkedList<GridCell>();
  29.         Queue<GridCell> visited = new LinkedList<GridCell>();
  30.  
  31.         Map<GridCell, IGridRobot.Direction> gridCell2firstStep = new HashMap<>();
  32.         Boolean firstStepDirectionSet = false;
  33.  
  34.         gridCell2firstStep.put(source, null);
  35.         unvisited.add(source);
  36.  
  37.         while (!unvisited.isEmpty()) {
  38.             GridCell currCell = unvisited.poll();
  39.             visited.add(currCell);
  40.             for (IGridRobot.Direction direction: IGridRobot.Direction.values()) {
  41.                 GridCell neighbour = this.getCellNeighbour(currCell, direction);
  42.                 if (this.grid.hasCell(neighbour) && !visited.contains(neighbour) && !unvisited.contains(neighbour) && !otherRobotPositions.contains(neighbour)) {
  43.                     // Check if neighbour is destination.
  44.                     if (neighbour.equals(destination)) {
  45.                         // If this is the first iteration from source, return the current direction.
  46.                         // Otherwise, return the direction of the first step.
  47.                         if (firstStepDirectionSet) {
  48.                             return gridCell2firstStep.get(currCell);
  49.                         } else {
  50.                             return direction;
  51.                         }
  52.                     }
  53.                     unvisited.add(neighbour);
  54.                     if (firstStepDirectionSet) {
  55.                         gridCell2firstStep.put(neighbour, gridCell2firstStep.get(currCell));
  56.                     } else {
  57.                         gridCell2firstStep.put(neighbour, direction);
  58.                     }
  59.                 }
  60.             }
  61.             if (!firstStepDirectionSet) { firstStepDirectionSet = true; }
  62.         }
  63.         return null;
  64.     }
  65.  
  66.     private GridCell getCellNeighbour(GridCell cell, IGridRobot.Direction direction) {
  67.         GridCell neighbour;
  68.         switch(direction) {
  69.             case NORTH:
  70.                 neighbour = GridCell.at(cell.x, cell.y + 1);
  71.                 break;
  72.             case EAST:
  73.                 neighbour = GridCell.at(cell.x + 1, cell.y);
  74.                 break;
  75.             case SOUTH:
  76.                 neighbour = GridCell.at(cell.x, cell.y - 1);
  77.                 break;
  78.             case WEST:
  79.                 neighbour = GridCell.at(cell.x - 1, cell.y);
  80.                 break;
  81.             default:
  82.                 throw new IllegalArgumentException("Invalid direction.");
  83.         }
  84.         return neighbour;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement