/ * Please design the program to solve the problem of "Poisher Separate" as follows:
Someone has a bottle of 12 pint beer, I want to fall out of 6 pins, but he does not have 6 pint of containers, only one 8 pinting and a 5-pint of container, how can I divide beer into two 6? pint?
Abstract analysis:
B = large container, also represents volume S = small container, also represents volume (f), (h), (e) state f = full, E = empty, h = digital, indicates capacity
Calculation 1: B (f) - s (e) => b (b - s), S (f) C (H) - S (E) => B (h - s), s (f)
Operation 2: B (E) S (f) => B (S), S (E) C (E) S (f) => B (f), S (S - B H)
Export B (F) - S (H) B (h) - s (h)
B (E) S (h) b (h) s (h)
If the number of wine in the bottle is a node, the node can be achieved by more than one calculation. This topic can be converted into a mapped search problem. That is to find. Specify nodes (12, 0, 0) and ( The smallest path between 6, 6, 0).
* / # include
Static int BIG_MAX_VALUE [] = {12, 8, 12}; static int small_max_value [] = {8, 5, 5}; static const Int Big_offset [] = {0, 1, 0}; static const Int small_offset [] = {1, 2, 2};
// node definition class node {unsigned char mbig; unsigned char mmid; unsigned char msmall; / UNSIGNED CHAR MMID;
Public: static void initmaxvalue (int max1, int max2, int max3) {BIG_MAX_VALUE [0] = max1; BIG_MAX_VALUE [1] = Max2; BIG_MAX_VALUE [2] = MAX1;
SMALL_MAX_VALUE [0] = max2; small_max_value [1] = max3; small_max_value [2] = MAX3;
Node (): mbig (0), mmid (0), msmall (0) {}
Node (unsigned char A, unsigned char b, unsigned char C): Mbig (a), mmid (b), msmall (c) {}
ENUM OPCODE {BIG_OP_MIDDLE = 0, MIDDLE_OP_SMALL, BIG_OP_SMALL, OP_LAST};
// minus operation void sub (opcode op) {INT BIG_MAX = BIG_MAX_VALUE [OP]; int small_max = small_max_value [op];
CHAR & BIG = * (ReinterPret_cast
// Add VOID Add (OPCode OP) {INT BIG_MAX = BIG_MAX_VALUE [OP]; int small_max = small_max_value [op];
CHAR & BIG = * (ReinterPret_cast
IF (Small> BIG_MAX - BIG) {small - = BIG_MAX - BIG; BIG = BIG_MAX;} else {BIG = Small; Small = 0;}}
Bool Check (INT Value) {if (mbig == value || mmid == value || msmall == value) {return true;} Return false;
Void print () const {printf ("status [% d] =% 2D, [% D] =% 2D, [% D] =% 2DN", BIG_MAX_VALUE [0], MBIG, BIG_MAX_VALUE [1], MMID, Small_Max_Value [2], msmall);
// Multi-equivalent determination Friend Bool Operator == (Node Const & A, Node Const & B) {Return Memcmp (& A, & B, SIZEOF (Node) == 0;
Friend Bool Operator <(Node Const & A, Node Const & B) {Return Memcmp (& A, & B, SIZEOF (Node)) <0;}};
Template
Typedef std :: map
Nodequeue Searchqueue; NodeValueType Last;
Searchqueue.push (std :: make_pair (start, start);
While (! searchqueue.empty ()) {nodeValueType cur = searchqueue.front (); searchqueue.pop ();
Visited.insert (CUR); if (cur.first.check (value)) {Last = Cur; Break;}
For (int i = 0; i
IF (visited.Find (next) == visited.end ()) {searchqueue.push (std :: make_pair (next1, cur.first);}
Node next2 = cur.first; Next2.add (static_cast
IF (visited.Find (NEXT2) == Visited.end ()) {searchQueue.push (std :: make_pair (next2, cur.first);}}}
NodeSetiter Cur = Visited.Find (last.first);
While (! (Cur-> first == start) {cur-> first.print (); cur = visited.find (cur-> second);} cur-> first.print ();
Int main () {PUTS ("someone has a bottle of beer, I want to fall out of 6 pins, but he does not have 6 pintage containers, N" "There is only one 8 pintment and a 5-pinto Containers, how can we divide beer into two 6 pins? N ");
For (int i = 0; i <12; i ) {printf ("- looking for the minimum step of getting% d, reverse order ------------ n", I); Search (Node (12, 0, 0), I);
Puts ("Solution to a 13-product off beer, but a 9-product off and a 5-product retractable container N");
Node :: initmaxvalue (13, 9, 5); for (int i = 0; i <12; i ) {printf ("- looking for the minimum step, reverse order, reverse order ------- ----- N ", I); Search (Node (13, 0, 0), I);} return 0;} In fact, the result should be (6, 6, 0) but in fact I Only a 6 situation is achieved. The reason is that there are two identical values. The following is the best solution for the 12, 8, 5 I made: someone has 12 pint beer a bottle, want to be Pour out 6 pins, but he does not have 6 pintage containers, only one 8-product off and a 5-product retracted container, how can we divide the beer into two 6 pins?
- - Find the minimum step of getting 0 pins, reverse order ------------ status [12] = 12, [8] = 0, [5] = 0 - - Find 1 product Take the minimum step, reverse order ------------ status [12] = 1, [8] = 8, [5] = 3status [12] = 9, [8] = 0, [5 ] = 3STATUS [12] = 9, [8] = 3, [5] = 0status [12] = 4, [8] = 3, [5] = 5STATUS [12] = 4, [8] = 8, [ 5] = 0status [12] = 12, [8] = 0, [5] = 0 - - Find the minimum step of 2 pins, reverse sequence ------------------------------------------ STATUS [12] = 2, [8] = 5, [5] = 5STATUS [12] = 7, [8] = 5, [5] = 0status [12] = 7, [8] = 0, [5] = 5STATUS [12 ] = 12, [8] = 0, [5] = 0 - - Find the minimum step of 3 pins, reverse order ------------ status [12] = 4, [8] = 3, [5] = 5STATUS [12] = 4, [8] = 8, [5] = 0status [12] = 12, [8] = 0, [5] = 0 - - Find 4 products Minimum step, reverse order ------------ status [12] = 4, [8] = 8, [5] = 0status [12] = 12, [8] = 0, [5] = 0 - - Find the minimum step of 5 pins, reverse sequence ------------ status [12] = 7, [8] = 0, [5] = 5STATUS [12] = 12 [8] = 0, [5] = 0 - - Find the minimum step of 6 pins, reverse order ------------ status [12] = 1, [8] = 6, [5] = 5STATUS [12] = 1, [8] = 8, [5] = 3STATUS [12] = 9, [8] = 0, [5] = 3status [12] = 9, [8] = 3 [5] = 0status [12] = 4, [8] = 3, [5] = 5STATUS [12] = 4, [8] = 8, [5] = 0status [12] = 12, [8] = 0, [5] = 0 --- Find the minimum step of 7 pins, reverse order ------------ Status [12 ] = 7, [8] = 0, [5] = 5STATUS [12] = 12, [8] = 0, [5] = 0 - - Find the minimum step of 8 pintment, reverse order ----- ------- status [12] = 4, [8] = 8, [5] = 0status [12] = 12, [8] = 0, [5] = 0 - - Find 9 products Minimum step, reverse order ------------ status [12] = 9, [8] = 3, [5] = 0status [12] = 4, [8] = 3, [5] = 5STATUS [12] = 4, [8] = 8, [5] = 0status [12] = 12, [8] = 0, [5] = 0 - - Find the minimum step, reverse order of 10 pintment - ----------- Status [12] = 10, [8] =