コード例 #1
0
  public FastCollinearPoints(
      Point[] points) { // finds all line segments containing 4 or more points
    if (points == null) throw new NullPointerException();
    for (int i = 0; i < points.length; i++) {
      if (points[i] == null) throw new NullPointerException();
    }

    lineSegments = new Queue<LineSegment>();

    for (int i = 0; i < points.length; i++) {
      // copy natural sorted array
      Point[] cPoints = new Point[points.length];
      for (int j = 0; j < points.length; j++) {
        if (i != j && points[i].compareTo(points[j]) == 0) throw new IllegalArgumentException();
        cPoints[j] = points[j];
      }

      // sort by slope with points[i]
      Arrays.sort(cPoints, points[i].slopeOrder());

      Point largest = cPoints[0];
      Point smallest = cPoints[0];
      int ccount = 1;
      for (int j = 1; j < points.length; j++) {
        if (points[i].slopeTo(cPoints[j - 1]) == points[i].slopeTo(cPoints[j])) {
          if (smallest.compareTo(cPoints[j]) > 0) smallest = cPoints[j];
          else if (largest.compareTo(cPoints[j]) < 0) largest = cPoints[j];
          ccount++;
        } else {
          if (ccount >= 3 && points[i].compareTo(smallest) < 0) {
            lineSegments.enqueue(new LineSegment(points[i], largest));
            segmentCount++;
          }
          ccount = 1;
          smallest = cPoints[j];
          largest = cPoints[j];
        }
      }
      if (ccount >= 3 && points[i].compareTo(smallest) < 0) {
        lineSegments.enqueue(new LineSegment(points[i], largest));
        segmentCount++;
      }
    }
  }
コード例 #2
0
ファイル: Task_43.java プロジェクト: HatlessFox/SelfStudy
 public static void listFiles(File file, Queue<? super String> f_names, int indent) {
   if (file.isDirectory()) {
     File[] files = file.listFiles();
     if (files == null) return;
     Arrays.stream(files).forEach(f -> listFiles(f, f_names, indent + 2));
   } else if (file.isFile()) {
     String format_str = String.format("%%%ds%%s\n", indent);
     f_names.enqueue(String.format(format_str, "", file.getName()));
   }
 }
コード例 #3
0
ファイル: SAP.java プロジェクト: fabiano-arantes/algs4
  private ShortestPath findShortestPath(int v, int w) {
    if (!isValid(v)) {
      throw new java.lang.NullPointerException("Invalid v parameter");
    }

    if (!isValid(w)) {
      throw new java.lang.NullPointerException("Ivalid w parameter");
    }

    Queue<Integer> q = new Queue<Integer>();
    boolean[] marked = new boolean[G.V()];
    int[] distTo = new int[G.V()];

    q.enqueue(v);
    q.enqueue(w);

    marked[v] = true;
    marked[w] = true;
    distTo[v] = 0;
    distTo[w] = 0;

    while (!q.isEmpty()) {
      int a = q.dequeue();
      for (int b : G.adj(a)) {
        if (marked[b]) {
          int length = distTo[b] + distTo[a] + 1;
          ShortestPath sp = new ShortestPath();

          sp.ancestor = b;
          sp.length = length;

          return sp;
        }

        distTo[b] = distTo[a] + 1;
        marked[b] = true;
        q.enqueue(b);
      }
    }

    return null;
  }
コード例 #4
0
ファイル: Board.java プロジェクト: SmartJuneThx/Algs4
  // all neighboring boards
  public Iterable<Board> neighbors() {
    Queue<Board> result = new Queue<>();

    int[][] directions = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // up, down, left, right

    for (int[] direc : directions) {
      Board board = new Board(this);
      boolean success =
          swap(
              board.copy,
              board.blankX,
              board.blankY,
              board.blankX + direc[0],
              board.blankY + direc[1]);
      if (success) {
        result.enqueue(board);
      }
    }

    return result;
  }
コード例 #5
0
ファイル: KdTree.java プロジェクト: aklotos/algorithms
  /**
   * Returns the keys in the BST in level order (for debugging).
   *
   * @return the keys in the BST in level order traversal
   */
  private Iterable<Point2D> levelOrder() {

    Queue<Point2D> keys = new Queue<Point2D>();
    Queue<Node> queue = new Queue<Node>();
    queue.enqueue(root);
    while (!queue.isEmpty()) {
      Node x = queue.dequeue();
      if (x == null) {
        continue;
      }
      keys.enqueue(x.key);
      queue.enqueue(x.left);
      queue.enqueue(x.right);
    }
    return keys;
  }
コード例 #6
0
ファイル: LazyPrimMST.java プロジェクト: phycept/algs4code
  public LazyPrimMST(EdgeWeightedGraph G) {
    pq = new MinPQ<Edge>(G.E());
    mst = new Queue<Edge>();
    marked = new boolean[G.V()];
    visit(G, 0);

    while (!pq.isEmpty()) {
      Edge e = pq.delMin();
      int v = e.either();
      int w = e.other(v);
      if (marked[v] && marked[w]) continue;
      mst.enqueue(e);
      if (!marked[v]) visit(G, v);
      else visit(G, w);
    }
  }
コード例 #7
0
ファイル: KdTree.java プロジェクト: aklotos/algorithms
  private void range(
      final Node node, final RectHV rect, final Queue<Point2D> ranged, final boolean vertical) {

    if (node == null) {
      return;
    }

    if (rect.contains(node.key)) {
      ranged.enqueue(node.key);
      range(node.left, rect, ranged, !vertical);
      range(node.right, rect, ranged, !vertical);
    } else {
      if (onTheLeft(node.key, rect, vertical)) {
        range(node.left, rect, ranged, !vertical);
      } else if (onTheRight(node.key, rect, vertical)) {
        range(node.right, rect, ranged, !vertical);
      } else {
        range(node.left, rect, ranged, !vertical);
        range(node.right, rect, ranged, !vertical);
      }
    }
  }