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
  // 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.º 3
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.º 4
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);
 }