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 } }
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;} } }
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; }