Other 2010 FR Questions FR other years Be Prepared Home
A-4
Part (a)
  public Actor actorWithMostNeighbors()
  {
    ArrayList<Location> locs = gr.getOccupiedLocations();
    if (locs.size() == 0)
      return null;

    int max = -1;
    Location bestLoc = null;

    for (Location loc : locs)
    {
      int n = gr.getOccupiedAdjacentLocations(loc).size(); 1

      if (n > max)
      {
        max = n;
        bestLoc = loc;
      }
    }

    return gr.get(bestLoc);
  }
Notes:
  1. Or:
      int n = gr.getNeighbors(loc).size();


Part (b)
  public List<Location> getOccupiedWithinTwo(Location loc)
  {
    ArrayList<Location> list = new ArrayList<Location>();

    for (int r = loc.getRow() - 2; r <= loc.getRow() + 2; r++)
    {
      for (int c = loc.getCol() - 2; c <= loc.getCol() + 2; c++)
      {
        Location loc1 = new Location(r, c);
        if (gr.isValid(loc1) && gr.get(loc1) != null && !loc1.equals(loc))
          list.add(loc1);
      }
    }

    return list;
  } 1
Notes:
  1. Alternative solution:
      public List getOccupiedWithinTwo(Location loc)
      {
        ArrayList<Location> list = new ArrayList();
        ArrayList<Location> occupied = gr.getOccupiedLocations();
    
        for (Location loc1 : occupied)
          if (Math.abs(loc.getRow() - loc1.getRow()) <= 2 &&
              Math.abs(loc.getCol() - loc1.getCol()) <= 2 &&
                                               !loc1.equals(loc))
            list.add(loc1);
    
        return list;
      }

Other 2010 FR Questions | Back to Contents

Copyright © 2010 by Skylight Publishing
support@skylit.com