예제 #1
0
파일: Solver.java 프로젝트: lukhar/coursera
  private List<Board> solve(Board initial) {
    Board twin = initial.twin();
    MinPQ<SearchNode> mainQueue = new MinPQ<>();
    MinPQ<SearchNode> twinQueue = new MinPQ<>();
    mainQueue.insert(new SearchNode(initial, 0, null));
    twinQueue.insert(new SearchNode(twin, 0, null));

    while (true) {
      SearchNode mainSearch = mainQueue.delMin();
      SearchNode twinSearch = twinQueue.delMin();

      if (mainSearch.board.isGoal()) {
        return retraceSteps(mainSearch);
      }

      if (twinSearch.board.isGoal()) {
        return null;
      }

      for (Board board : mainSearch.board.neighbors()) {
        SearchNode mainPrevious = mainSearch.previous;
        if (mainPrevious == null || !board.equals(mainPrevious.board)) {
          mainQueue.insert(new SearchNode(board, mainSearch.moves + 1, mainSearch));
        }
      }

      for (Board board : twinSearch.board.neighbors()) {
        SearchNode twinPrevious = twinSearch.previous;
        if (twinPrevious == null || !board.equals(twinPrevious.board)) {
          twinQueue.insert(new SearchNode(board, twinSearch.moves + 1, twinSearch));
        }
      }
    }
  }
예제 #2
0
  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);
    }
  }
예제 #3
0
  public static void main(String[] args) {
    WeightedQuickUnionUF uf = new WeightedQuickUnionUF(10);
    MinPQ<String> minPQ = new MinPQ<>(17, new Prob1().new myComparator());
    String prob =
        "F-A    16    G-A    13    B-A     3     C-B     6    H-B     5    G-B     2    C-D    12    C-I     9    C-H     1    D-I    14    J-D     8    D-E     7    J-E    17    F-G    15    G-H     4    I-H    11    I-J    10";
    String[] temp = prob.trim().split("\\s+");
    for (int i = 0; i < temp.length; i += 2) {
      String record = temp[i] + "," + temp[i + 1];
      minPQ.insert(record);
    }

    int sizeOfMST = 0;
    while (!minPQ.isEmpty() && sizeOfMST <= 9) {
      String minEdgeStr = minPQ.delMin();
      String[] vertexStr = minEdgeStr.split(",")[0].split("-");
      int v = vertexStr[0].charAt(0) - 'A';
      int w = vertexStr[1].charAt(0) - 'A';
      if (!uf.connected(v, w)) {
        uf.union(v, w);
        System.out.println(minEdgeStr);
        sizeOfMST++;
      }
    }
  }
예제 #4
0
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    MinPQ<Transaction> pq = new MinPQ<>(M + 1);
    while (StdIn.hasNextLine()) {
      pq.insert(new Transaction(StdIn.readLine()));
      if (pq.size() > M) {
        pq.delMin();
      }
    }

    Stack<Transaction> stack = new Stack<Transaction>();
    while (!pq.isEmpty()) stack.push(pq.delMin());
    for (Transaction t : stack) StdOut.println();
  }
예제 #5
0
 private void visit(EdgeWeightedGraph G, int v) {
   marked[v] = true;
   for (Edge e : G.adj(v)) {
     if (!marked[e.other(v)]) pq.insert(e);
   }
 }
예제 #6
0
  // the sweep-line algorithm
  public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    int INFINITY = Integer.MAX_VALUE; // -INFINITY is second smallest integer

    // generate N random h-v segments
    SegmentHV[] segments = new SegmentHV[N];
    for (int i = 0; i < N; i++) {
      int x1 = (int) (Math.random() * 10);
      int x2 = x1 + (int) (Math.random() * 3);
      int y1 = (int) (Math.random() * 10);
      int y2 = y1 + (int) (Math.random() * 5);
      if (Math.random() < 0.5) segments[i] = new SegmentHV(x1, y1, x1, y2);
      else segments[i] = new SegmentHV(x1, y1, x2, y1);
      System.out.println(segments[i]);
    }
    System.out.println();

    // create events
    MinPQ<Event> pq = new MinPQ<Event>();
    for (int i = 0; i < N; i++) {
      if (segments[i].isVertical()) {
        Event e = new Event(segments[i].x1, segments[i]);
        pq.insert(e);
      } else if (segments[i].isHorizontal()) {
        Event e1 = new Event(segments[i].x1, segments[i]);
        Event e2 = new Event(segments[i].x2, segments[i]);
        pq.insert(e1);
        pq.insert(e2);
      }
    }

    // run sweep-line algorithm
    RangeSearch<SegmentHV> st = new RangeSearch<SegmentHV>();

    while (!pq.isEmpty()) {
      Event e = pq.delMin();
      int sweep = e.time;
      SegmentHV segment = e.segment;

      // vertical segment
      if (segment.isVertical()) {
        // a bit of a hack here - use infinity to handle degenerate intersections
        SegmentHV seg1 = new SegmentHV(-INFINITY, segment.y1, -INFINITY, segment.y1);
        SegmentHV seg2 = new SegmentHV(+INFINITY, segment.y2, +INFINITY, segment.y2);
        Iterable<SegmentHV> list = st.range(seg1, seg2);
        for (SegmentHV seg : list) {
          System.out.println("Intersection:  " + segment);
          System.out.println("               " + seg);
        }
      }

      // next event is left endpoint of horizontal h-v segment
      else if (sweep == segment.x1) {
        st.add(segment);
      }

      // next event is right endpoint of horizontal h-v segment
      else if (sweep == segment.x2) {
        st.remove(segment);
      }
    }
  }