Using Stack to achieve // k buffer rails, the car initial sort is P [1: N] BOOL Railroad (int P [], int N, int K) {LinkedStack
// Car rearrange FOR (INT i = 1; i <= n; i ) IF (p [i] == noout) {cout << "move car" << p [i] << "from input to output << Endl; Nowout ; while (minh == Nowout) {OUTPUT (Minh, Mins, H, K, N); Nowout ;}} else {if (! Hold (p [i], minh, mins, h, K, N)) Return False;} Return Ture;}
Void Output (int & minh, int> h [], int k, int n) {INT C; // Remove the number of No. Minh h [mins]. delete (c) from the stack mins. Delete (C); COUT < <"Move car" << minh << "from holding track" << mins << "to output" << endl; // Search for all stack tops, search for new minh and mins minh = n 2; for (INT i = 1; i <= k; i ) {if (! h [i] .Isempty () && (c = h [i] .top ()) // Put in a buffer rail, INT C, INT & Minh, Int & Mins, LinkedStack For (int i = 1; i <= k; i ) IF (! h [i] .Isempty ()) {x = h [i] .top (); if (c Ref: << Data Structure, Algorithm and Applications >> P174 ~ 176 Use Queue to Realize Void Output (INT & MINH, INT & Minq, LinkedQueue INT C; // carriage number // Delete the number of the number of the number in MINQ minh h [minq] .delete (c); cout << "move car" << minh << "from holding track" << "qq <<" to output "<< ENDL; / / Find new Minh and Minq minh = N 2; for (int i = 1; i <= k; i ) IF (! H [i] .Isempty () && (c = H [i] .frist ()) Bool Hold (int C, Int & Minh, Int & minq, LinkedQueue // At present, the optimal rail Int bestTrack = 0; // BestTrack, the last car INT BESTLAST = 0; // Car number INTX; // Scan buffer iron rail for (int i = 1; i <= k; i ) IF (! H [i] .Isempty ()) {x = h [i] .last (); if (c> x && x > BESTLAST = x; besttrack = i;}} else if (! Besttrack) besttrack = i; IF (! besttrack) Return False; H [besttrack] .add (c); cout << "move car" << c << "from infut" << "to holding track" << BestTrack << Endl; // If necessary, modify minh, minq if (c Ref: << Data Structure, Algorithm & Applications >> P197 ~ 199 Do not use Stack and Queue to achieve the car rearrange Void Output (int nowout, int track, int & Last) {// Move the car NOWOUT from the buffer rail to derail, and modify the Last Cout << "Move Car" << nowout << "from Holding track "<< track <<" to output << endl; if (nowout == last) last = 0;} Bool Hold (int C, Int Last [], int track [], int K) {//// Move the carriage C to the buffer rail INT BestTrack = 0; int best = 0; // Scan buffer rail for (INT i = 1; i <= k; i ) IF (Last [i]) {IF (C> Last [i] && last [i] > Bestlast {besttrack = i; bestlast = last [i];}} else if (! Besttrack) besttrack = i; if (! Besttrac) Return False; Track [C] = BESTTRACK; LAST [BESTTRACK] = C; COUT << "Move Car" << c << "from infut" << "to holding track" << besttractrack << end1 Return True;} Bool Railroad (int P [], int N, int K) {// use K buffered railroad track rearrangement, the initial order of the carriage is P [1: n] / / Turn INT * LAST = New Int [K 1]; INT * TRACK = New INT [N 1]; for (int i = 1; i <= k; i ) last [i ] = 0; for (int i = 1; i <= n; i ) TRACK [I] = 0; INT NOWOUT = 1; // Output carriage for (int i = 1; i <= n; i ) IF (P [i] == Nowout) {cout << "" from input to Output "<< Endl; Nowout ; // Output from the buffer rail (Nowout <= N && track [Nowout]) {Output (Nowout, Track [Nowout], Last [TRACK [NOWout]]); Nowout }} else {if (!! I], Last, Track, K) RETURN FALSE;} Return True;} Ref: << Data Structure, Algorithm & Applications >> P199 ~ 201