Ejemplo n.º 1
0
  public static void main(String[] args) {
    SET<String> set = new SET<String>();

    // read in strings and add to set
    while (!StdIn.isEmpty()) {
      String key = StdIn.readString();
      if (!set.contains(key)) {
        set.add(key);
        StdOut.println(key);
      }
    }
  }
Ejemplo n.º 2
0
 // a nearest neighbor in the set to point p; null if the set is empty
 public Point2D nearest(Point2D p) {
   if (p == null) throw new NullPointerException("Point2D cannot be null!");
   if (set.isEmpty()) return null;
   double minDistance = Double.MAX_VALUE;
   Point2D minPoint = null;
   for (Point2D point : set) {
     double distance = p.distanceSquaredTo(point);
     if (distance < minDistance) {
       minDistance = distance;
       minPoint = point;
     }
   }
   return minPoint;
 }
Ejemplo n.º 3
0
  // Recursive function to find all words on board from partiular tile
  private void recFindAllWordsOnBoard(
      String partial,
      int row,
      int col,
      BoggleBoard board,
      boolean[][] usedTiles,
      Trie dictionary,
      SET<String> wordsFound) {
    // Base Case 0 (if row or col are out of board):
    if (!inBounds(row, col, board)) return;

    // Base Case 1 (if tile was already used previously):
    if (usedTiles[row][col]) return;

    // Go along this path to explore words
    char letter = board.getLetter(row, col);

    if (letter == 'Q') {
      partial += "QU";
    } else {
      partial += letter;
    }

    // If the dictionary does not contain partial prefix
    //    	if (!dictionary.keysWithPrefix(partial).iterator().hasNext()) return;
    if (!dictionary.containsPrefix(partial)) return;

    // Continue exploring
    usedTiles[row][col] = true;

    if (dictionary.containsWord(partial)
        && !wordsFound.contains(partial)
        && partial.length() >= MIN_WORD_SIZE) {
      wordsFound.add(partial);
    }

    // Explore all 8 directions from current tile
    int adjRow = 0;
    int adjCol = 0;

    for (int direction = 0; direction < NUM_TILE_DIRECTIONS; direction++) {
      // Update adjRow and adjCol to all 8 possible directions
      switch (direction) {
          // North West
        case 0:
          adjRow = row - 1;
          adjCol = col - 1;
          break;

          // North
        case 1:
          adjRow = row - 1;
          adjCol = col;
          break;

          // North East
        case 2:
          adjRow = row - 1;
          adjCol = col + 1;
          break;

          // East
        case 3:
          adjRow = row;
          adjCol = col + 1;
          break;

          // South East
        case 4:
          adjRow = row + 1;
          adjCol = col + 1;
          break;

          // South
        case 5:
          adjRow = row + 1;
          adjCol = col;
          break;

          // South West
        case 6:
          adjRow = row + 1;
          adjCol = col - 1;
          break;

          // West
        case 7:
          adjRow = row;
          adjCol = col - 1;
          break;

        default:
          break;
      }

      recFindAllWordsOnBoard(partial, adjRow, adjCol, board, usedTiles, dictionary, wordsFound);
    }

    // Backtrack step
    partial = partial.substring(0, partial.length() - 1);
    usedTiles[row][col] = false;
  }
Ejemplo n.º 4
0
 // does the set contain point p?
 public boolean contains(Point2D p) {
   return points.contains(p);
 }
Ejemplo n.º 5
0
 // add the point to the set (if it is not already in the set)
 public void insert(Point2D p) {
   points.add(p);
 }
Ejemplo n.º 6
0
 // number of points in the set
 public int size() {
   return points.size();
 }
Ejemplo n.º 7
0
 // is the set empty?
 public boolean isEmpty() {
   return points.size() == 0;
 }
Ejemplo n.º 8
0
 // does the set contain point p?
 public boolean contains(Point2D p) {
   if (p == null) throw new NullPointerException("Point2D cannot be null!");
   return set.contains(p);
 }
Ejemplo n.º 9
0
 // add the point to the set (if it is not already in the set)
 public void insert(Point2D p) {
   if (p == null) throw new NullPointerException("Point2D cannot be null!");
   if (!set.contains(p)) set.add(p);
 }
Ejemplo n.º 10
0
 // number of points in the set
 public int size() {
   return set.size();
 }
Ejemplo n.º 11
0
 // is the set empty?
 public boolean isEmpty() {
   return set.isEmpty();
 }