示例#1
0
 /** Branch and bound. */
 private SearchNode branch(SearchNode s, int v, int color, SearchNode best) {
   SearchNode newBest, nextTry;
   assert s != null && v >= 0 && color >= 0 && v < V && color < V;
   assert best != null || v == 0 && color == 0; // best only null if first call.
   if (!s.setColor(v, color)) return null;
   if (best != null && s.bound() > best.maxColor()) return best;
   if (best == null || s.maxColor() < best.maxColor()) newBest = s;
   else newBest = best;
   if (!newBest.solved()) {
     for (int w = v; w < V; w++) {
       for (int nextColor : s.nextColors(w, newBest.maxColor())) {
         nextTry = branch(new SearchNode(s), w, nextColor, newBest);
         if (nextTry != null && nextTry.maxColor() < newBest.maxColor()) newBest = nextTry;
       }
     }
   }
   return newBest;
 }