Train car rearrangement

xiaoxiao2021-03-05  23

Using Stack to achieve // ​​k buffer rails, the car initial sort is P [1: N] BOOL Railroad (int P [], int N, int K) {LinkedStack * h; h = new linkedstack [k 1]; // The next time the carriage INT NOWout = 1; // Buffer INT minh = N 1; // minh is the buffer rail INT MINS;

// 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 h [], int K, int N) {// current optimal rail INT bestTrack = 0; // Optimal rails INT besttop = n 1; // compartment index int x;

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 H [], INT K, INT N) {// From Buck Rail Move to derailment and modify minh and minq

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 h [], int K) {// Moves the compartment C to the buffer rail // If there is no available buffer rail, return false, otherwise returns True / / Find the best railroad track for the car C

// 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

转载请注明原文地址:https://www.9cbs.com/read-32230.html

New Post(0)