示例#1
0
T MaxLoading(T w[], T c, int n)
{// Return value of best loading.
 // Use FIFO branch and bound.
   // initialize for level 1 start
   LinkedQueue<T> Q;  // live-node queue
   Q.Add(-1);         // end-of-level marker
   int i = 1;         // level of E-node
   T Ew = 0,          // weight of E-node
     bestw = 0;       // best weight so far

   // search subset space tree
   while (true) {
      // check left child of E-node
      if (Ew + w[i] <= c) // x[i] = 1
         AddLiveNode(Q, Ew + w[i], bestw, i, n);

      // right child is always feasible
      AddLiveNode(Q, Ew, bestw, i, n); // x[i] = 0

      Q.Delete(Ew);     // get next E-node
      if (Ew == -1) {   // end of level
         if (Q.IsEmpty()) return bestw;
         Q.Add(-1);     // end-of-level marker
         Q.Delete(Ew);  // get next E-node
         i++;}          // level number of Ew
      }
}
示例#2
0
void LevelOrder(BinaryTreeNode<T> *t)
{// Level-order traversal of *t.
   LinkedQueue<BinaryTreeNode<T>*> Q;
   while (t) {
      Visit(t);  // visit t

      // put t's children on queue
      if (t->LeftChild) Q.Add(t->LeftChild);
      if (t->RightChild) Q.Add(t->RightChild);

      // get next node to visit
      try {Q.Delete(t);}
      catch (OutOfBounds) {return;}
      }
 }
示例#3
0
void AddLiveNode(LinkedQueue<T> &Q, T wt,
                    T& bestw, int i, int n)
{// Add node weight wt to queue Q if not leaf.
   if (i == n) {// feasible leaf
      if (wt > bestw) bestw = wt;}
   else Q.Add(wt); // not a leaf
}
示例#4
0
int mains() {
   // Demonstrate that our LinkedQueue works.
   LinkedQueue numbers;

   numbers.Add(4);
   numbers.Add(8);
   numbers.Add(15);
   numbers.Add(16);
   numbers.Add(23);
   numbers.Add(42);

   while (numbers.Size() > 0) {
      int data = numbers.Remove();
      cout << "Removed " << data << endl;
   }
   // This seems to work! Yay!
   // But there are sinister bugs hidden from view...


   // First, what happened to the Nodes that got unlinked from the queue?



   // Second, what happens if I Remove from an empty queue?



   // Now that we've fixed those errors...
   // Third, what's up with this code?
   LinkedQueue second;
   second.Add(1);

   LinkedQueue copy = second; // make a copy of second
   copy.Add(2);
   cout << second.Size() << endl; // output: 1
   cout << copy.Size() << endl;   // output: 2

   second.Remove();
   copy.Remove();
   // whoops! What happened?





   // One final bug:
   if (true) {
      LinkedQueue third;
      third.Add(5);
      third.Add(6);

      // What happens when third goes out of scope?
      // What do we need to fix this?
   }

   for (int i = 0; i < 0; i++) {
      LinkedQueue temp;
      temp.Add(1);
      temp.Add(1);
      temp.Add(1);
      temp.Add(1);
      temp.Add(1);
   }

   LinkedQueue empty;
   cout << empty.Remove();
   return 0;
}
示例#5
0
bool FindPath(Position start, Position finish,
             int& PathLen, Position * &path)
{// Find a path from start to finish.
 // Return true if successful, false if impossible.
 // Throw NoMem exception if inadequate space.

   if ((start.row == finish.row) &&
      (start.col == finish.col))
         {PathLen = 0; return true;} // start = finish

   // initialize wall of blocks around grid
   for (int i = 0; i <= m+1; i++) {
      grid[0][i] = grid[m+1][i] = 1; // bottom & top
      grid[i][0] = grid[i][m+1] = 1; // left & right
      }

   // initialize offsets
   Position offset[4];
   offset[0].row = 0; offset[0].col = 1; // right
   offset[1].row = 1; offset[1].col = 0; // down
   offset[2].row = 0; offset[2].col = -1; // left
   offset[3].row = -1; offset[3].col = 0; // up

   int NumOfNbrs = 4; // neighbors of a grid position
   Position here, nbr;
   here.row = start.row;
   here.col = start.col;
   grid[start.row][start.col] = 2; // block
   
   // label reachable grid positions
   LinkedQueue<Position> Q;
   do {// label neighbors of here
      for (int i = 0; i < NumOfNbrs; i++) {
         nbr.row = here.row + offset[i].row;
         nbr.col = here.col + offset[i].col;
         if (grid[nbr.row][nbr.col] == 0) {
             // unlabeled nbr, label it
             grid[nbr.row][nbr.col]
                = grid[here.row][here.col] + 1;
             if ((nbr.row == finish.row) &&
                (nbr.col == finish.col)) break; // done
   	     Q.Add(nbr);} // end of if
         } // end of for
      
      // have we reached finish?
      if ((nbr.row == finish.row) &&
          (nbr.col == finish.col)) break; // done

      // finish not reached, can we move to a nbr?
      if (Q.IsEmpty()) return false; // no path
      Q.Delete(here); // get next position
   } while(true);
            
   // construct path
   PathLen = grid[finish.row][finish.col] - 2;
   path = new Position [PathLen];

   // trace backwards from finish
   here = finish;
   for (int j = PathLen-1; j >= 0; j--) {
      path[j] = here;
      // find predecessor position
      for (int i = 0; i < NumOfNbrs; i++) {
         nbr.row = here.row + offset[i].row;
         nbr.col = here.col + offset[i].col;
         if (grid[nbr.row][nbr.col] == j+2) break;
         }
      here = nbr;  // move to predecessor
      }

   return true;
}