The exhaustive algorithm (Poisson wine)

Exhaustive method

  Also known as brute force method is to put all the conditions into account all relevant circumstances, let the computer to retrieve, to guide the outcome with all the conditions are met

  (However, serious-consuming brute force method of computer resources, if the conditions are too complex, slow operation, in order to solve this problem, we can not advance with the relevant conditions to limit and reduce the amount of computation computer)

Poisson wine

There are three containers, capacities of 12 liters, 8 liters, 5 liters. Filled with 12 liters of alcohol, the other two empty.
We ask you to only three container operations, and finally making a container in exactly six liters of wine.

Rule 1 min Wine -> 2 -> 3 -> 1

  1. Only large bottle poured into the bottle
  2. Only in the bottle into a small bottle
  3. Only a small bottle poured into a large bottle
  4. Only in the case of small bottles have been filled to pour large bottle
  5. If the small bottle is emptied, regardless of whether the bottle is full, they should immediately from the bottle into a small bottle

The reason for pouring the provisions of the order is to prevent duplicate state. According to these five rules, each large bottle of wine in the bottle is poured into 8 l always, every small bottle poured into a large bottle of wine is always 5 liters.

Code

cn.itcast.recursion Package; 

public  class ShareWine { 

    public  static  void main (String [] args) { 
        ShareWine shareWine = new new ShareWine (); 
        shareWine.backBottle ( 12 is , 0 , 0 ); 
    } 

    // definition of three bottles capacity 
    Private  int B1 = 12 is ;
     Private  int B2 = . 8 ;
     Private  int B3 = . 5 ;
     Private  int m = . 6 ; // target drinker

    // assuming a start, 12,0,0 
    public  void backBottle ( int BB1, int BB2, int BB3) { 
        the System. OUT .println ( " BB1 drinker: " + + BB1 "    BB2 drinker: " + BB2 + "    BB3 drinker : " + BB3);
         IF (m || == BB1 BB2 BB3 == == m || m) { 
            . the System OUT .println ( " Find The bottle " );
             return ; 
        } 
        // bottle and the wine && 2 3 bottle is not full 
        if(! BB2 = 0 ! && BB3 = B3) {
             // not to 5 liters, down dissatisfaction 
            IF (+ BB2 BB3 <= B3) { 
                backBottle (BB1, 0 , + BB2 BB3); 
            } the else {
                 // second parameters: the beginning bb2 there is so much wine, then pour bb3, bb3 full, bb2 is equivalent bb2 before - (b3- bb3 did not fall until the full after) 
                backBottle (BB1, bb2 - (b3 - BB3) , B3); 
            } 
            // bottle 3 is full, bottle 1 is inverted to 
        } the else  IF (BB3 == B3) {
             // not to 12 liters, down dissatisfaction 
            IF (BB1 + BB3 <= B1) { 
                backBottle (BB1BB3 +, BB2, 0 ); 
            } the else { 
                backBottle (B1, BB2, BB3 - (B1 - BB1)); 
            } 
            // into the bottle pouring from the bottle 2. 1 
        } the else  IF (BB2 == 0 ) {
             IF (BB1 > = B2) { 
                backBottle (BB1 - B2, B2, BB3); 
            } the else { 
                backBottle ( 0 , BB1, BB3); 
            } 
        } 
    } 
}

Print Results:

bb1酒量:12     bb2酒量:0    bb3酒量:0
bb1酒量:4       bb2酒量:8    bb3酒量:0
bb1酒量:4       bb2酒量:3    bb3酒量:5
bb1酒量:9       bb2酒量:3    bb3酒量:0
bb1酒量:9       bb2酒量:0    bb3酒量:3
bb1酒量:1       bb2酒量:8    bb3酒量:3
bb1酒量:1       bb2酒量:6    bb3酒量:5

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/11747172.html