列车车厢重排问题
一列货运列车有 n 节车厢,每节车厢要停靠在不同的车站。假设 n个车站从 1 到n 编号,而且货运列车按照从n到1的顺序经过车站。车厢的编号与它们要停靠的车站编号相同。为了便于从列车上卸掉相应的车厢,必须按照从前至后、从1到 n 的顺序把车厢重新排列。这样排列之后,在每个车站只需卸掉最后一节车厢即可。车厢重排工作在一个转轨站(shunting yard)上进行,转轨站上有一个入轨道(input track)、一个出轨道(output track)和k个缓冲轨道(holding track)。缓冲轨道位于入轨道和出轨道之间。图 8-6a 显示了一个转轨站,其中有 3 个缓冲轨道 H1、H2 和 H3,即 k=3。开始时,挂有 n 节车厢的货车开始在入轨道,而最后在出轨道上的顺序是从右到左,从1 至n。在图 8-6a 中,n=9,车厢从后至前的初始顺序为5,8,1,7,4,2,9,6,3。图8-6b是按要求的顺序重新排列的结果。
求解策略
为了重排车厢,我们从前至后检查入轨道上的车厢。如果正在检查的车厢是满足排列要求的下一节车厢,就直接把它移到出轨道上。如果不是,就把它移到一个缓冲轨道上,直到它满足排列要求时才将它移到出轨道上。缓冲轨道是按照 LIFO的方式管理的,车厢的进出都在缓冲轨道的顶部进行。在重排车厢过程中,仅允许以下移动:
- 车厢可以从入轨道的前端(即右端)移动到一个缓冲轨道的顶部或出轨道的后端(即左端)。
- 车厢可以从一个缓冲轨道的顶部移到出轨道的后端。
代码
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
/*列车车厢重排全局变量*/
stack<int>* trackStack;//缓冲轨道数组
vector<int> outputTrackStack;//输出数组
int numberOfCarsStack;//需要重排的列车数目
int numberOfTracksStack;//缓冲轨道数目
int smallestCarStack;//在缓冲轨道中编号最小的车厢
int itsTrackStack;//停靠着最小编号车厢的缓冲轨道/*列车车厢重排问题*/
/*将编号最小的车厢从缓冲轨道移到出轨道*/
void outputFromHoldingTrackStack()
{//从栈itsTrack中删除编号最小的车厢outputTrackStack.push_back(trackStack[itsTrackStack].top());trackStack[itsTrackStack].pop();cout << "Move car " << smallestCarStack << " from holding track " << itsTrackStack << " to output track" << endl;//检查所有的栈顶,寻找编号最小的车厢和它所属的栈itsTracksmallestCarStack = numberOfCarsStack + 2;for (int i = 1; i <= numberOfTracksStack; i++){if (!trackStack[i].empty() && (trackStack[i].top() < smallestCarStack)){smallestCarStack = trackStack[i].top();itsTrackStack = i;}}
}
/*将车厢c移到一个缓冲轨道。返回false,当且仅当没有可用的缓冲轨道*/
bool putInHoldingTrackStack(int c)
{//为车厢c寻找最适合的缓冲轨道//初始化int bestTrack = 0;//目前没有适合的缓冲轨道int bestTop = numberOfCarsStack + 1;//取bestTrack中最顶部的车厢,便于比较//扫描缓冲轨道for (int i = 1; i <= numberOfTracksStack; i++){//缓冲轨道i不为空// 是一个单调栈,栈底到栈顶的数据是从小到大if (!trackStack[i].empty()){if (c < trackStack[i].top() && trackStack[i].top() < bestTop){//缓冲轨道i的栈顶具有编号更小的车厢bestTop = trackStack[i].top();bestTrack = i;}}else if (bestTrack == 0) bestTrack = i;}if (bestTrack == 0) return false;//没有可用的缓冲轨道//把车厢c移动到轨道bestTracktrackStack[bestTrack].push(c);cout << "Move car " << c << " from input track to holding track " << bestTrack << endl;//如果需要,更新smallestCar和itsTrackif (c < smallestCarStack){smallestCarStack = c;itsTrackStack = bestTrack;}return true;
}
/*从初始顺序开始重排车厢;如果重排成功,返回true,否则返回false*/
bool railRoadStack(int inputOrder[], int theNumberOfCars, int theNumberOfTracks)
{numberOfCarsStack = theNumberOfCars;numberOfTracksStack = theNumberOfTracks;/*创建用于缓冲轨道的栈*/trackStack = new stack<int>[numberOfTracksStack + 1];smallestCarStack = numberOfCarsStack + 1;//缓冲轨道中无车厢int nextCarToOutput = 1;//当前需要被输出轨道的车厢编号//重排车厢for (int i = 0; i < numberOfCarsStack; i++){if (inputOrder[i] == nextCarToOutput){/*将车厢inputOrder[i]直接移到出轨道*/cout << "Move car " << inputOrder[i] << " from input track to output track" << endl;outputTrackStack.push_back(inputOrder[i]);nextCarToOutput++;/*从缓冲轨道移到出轨道*/while (smallestCarStack == nextCarToOutput){outputFromHoldingTrackStack();nextCarToOutput++;}}else{if(!putInHoldingTrackStack(inputOrder[i]))return false;}}return true;
}int main()
{// 列车车厢重排问题cout << "railRoadStack()*****************" << endl;int inputOrder[9] = { 5, 8, 1, 7, 4, 2, 9, 6, 3 };railRoadStack(inputOrder, 9, 3);for(int& data : outputTrackStack)cout << data << " ";cout << endl;return 0;
}
运行结果
C:\Users\15495\Documents\Jasmine\Work\coding\cmake-build-debug\coding.exe
railRoadStack()*****************
Move car 5 from input track to holding track 1
Move car 8 from input track to holding track 2
Move car 1 from input track to output track
Move car 7 from input track to holding track 2
Move car 4 from input track to holding track 1
Move car 2 from input track to output track
Move car 9 from input track to holding track 3
Move car 6 from input track to holding track 2
Move car 3 from input track to output track
Move car 4 from holding track 1 to output track
Move car 5 from holding track 1 to output track
Move car 6 from holding track 2 to output track
Move car 7 from holding track 2 to output track
Move car 8 from holding track 2 to output track
Move car 9 from holding track 3 to output track
1 2 3 4 5 6 7 8 9Process finished with exit code 0