コード例 #1
0
ファイル: DeDup.java プロジェクト: imace/zava
  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);
      }
    }
  }
コード例 #2
0
ファイル: PointSET.java プロジェクト: Ramin8or/algorithms
 // 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;
 }
コード例 #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;
  }
コード例 #4
0
 // does the set contain point p?
 public boolean contains(Point2D p) {
   return points.contains(p);
 }
コード例 #5
0
 // add the point to the set (if it is not already in the set)
 public void insert(Point2D p) {
   points.add(p);
 }
コード例 #6
0
 // number of points in the set
 public int size() {
   return points.size();
 }
コード例 #7
0
 // is the set empty?
 public boolean isEmpty() {
   return points.size() == 0;
 }
コード例 #8
0
ファイル: PointSET.java プロジェクト: Ramin8or/algorithms
 // 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);
 }
コード例 #9
0
ファイル: PointSET.java プロジェクト: Ramin8or/algorithms
 // 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);
 }
コード例 #10
0
ファイル: PointSET.java プロジェクト: Ramin8or/algorithms
 // number of points in the set
 public int size() {
   return set.size();
 }
コード例 #11
0
ファイル: PointSET.java プロジェクト: Ramin8or/algorithms
 // is the set empty?
 public boolean isEmpty() {
   return set.isEmpty();
 }