火车车厢重排问题

问题描述

 火车车厢重排算法伪代码如下

1. 分别对k个队列初始化;
2. 初始化下一个要输出的车厢编号nowOut = 1; 
3. 依次取入轨中的每一个车厢的编号;
3.1 如果入轨中的车厢编号等于nowOut,则
    3.1.1 输出该车厢;
    3.1.2  nowOut++;
3.2 否则,考察每一个缓冲轨队列
    for (j=1; j<=k; j++)
 3.2.1 取队列 j 的队头元素c;
 3.2.2 如果c=nowOut,则
      3.2.2.1 将队列 j 的队头元素出队并输出;
      3.2.2.2  nowOut++; 
3.3 如果入轨和缓冲轨的队头元素没有编号为nowOut的车厢,则
    3.3.1 求小于入轨中第一个车厢编号的最大队尾元素所在队列编号j;
    3.3.2 如果 j 存在,则把入轨中的第一个车厢移至缓冲轨 j;

问题分析

        一列火车的每个车厢按顺序入轨进入不同的缓冲轨道,缓冲轨道重排后的进入出轨,重新编排成一列火车。比如:编号为3的车厢进入缓冲轨道1,则下一个编号小于3的车厢则必须进入下一个缓冲轨道,而编号大于3的车厢则进入缓冲轨道1,排在3号车厢后面,这样,出轨的时候才可以按照从小到大的顺序重新编排。  

代码实现

#include<iostream>
using namespace std;

typedef struct queue {
int data[100];
int top, tail;
}queue;

void Initqueue(queue*& a) {
a = (queue*)malloc(sizeof(queue));
a->top = a->tail = -1;
}

void DestoryQueue(queue*& a) {
free(a);
}

int GetTop(queue* a) {
return a->data[a->top + 1];
}

void Push(queue*& a, int n) {
a->tail++;
a->data[a->tail] = n;
}

int Pop(queue*& a) {
a->top++;
return a->data[a->top];
}

void disp(queue* a) {
cout << endl;
int i = a->top + 1;
for (i; i <= a->tail; i++)
{
cout << a->data[i] << " ";
if (i == a->tail)
{
cout << endl;
}
}
cout << endl;
}

void disp_all(queue* a[], int num)
{
cout << "**************************************" << endl;
for (int i = 0; i < num; i++)
{
cout << "第" << i + 1 << "个缓冲轨道的现状为:" << endl;
disp(a[i]);
}
cout << "**************************************" << endl;
}

bool Empty(queue* a) {
return a->top == a->tail;
}

bool search(queue* a[], int num, int res, queue*& res_queue) {         //在所有队列中寻找头为res值的队列,找到则返回true,否则返回flase
for (int i = 0; i < num; i++)
{
if (!Empty(a[i]) && GetTop(a[i]) == res)
{
res_queue = a[i];
return true;
}
}
return false;
}

bool seek(queue* a[], int num, int res, queue*& resqueue) {          //求小于入轨中第一个车厢编号的最大队尾元素所在的队列
int max = -1;
for (int i = 0; i < num; i++)
{
if (!Empty(a[i]) && a[i]->data[a[i]->tail] < res)
{
if (max < a[i]->data[a[i]->tail])
{
max = a[i]->data[a[i]->tail];
resqueue = a[i];
}
}
}
return max == -1 ? false : true;
}

bool find(queue* a[], int num, queue*& res_queue) {          // 寻找空队列
for (int i = 0; i < num; i++)
{
if (Empty(a[i]))
{
res_queue = a[i];
return true;
}
}
return false;
}

int main()
{
int n, k;
cout << "请输入车厢数以及缓冲铁轨数:";
cin >> n >> k;
queue* hc[100], * input, * output;
for (int i = 0; i < k; i++)
{
Initqueue(hc[i]);
}
Initqueue(input);
Initqueue(output);
input->tail = input->top = output->tail = output->top = -1;
cout << "输入n节车厢入轨顺序";
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
Push(input, num);
}
//disp(input);
int nowout = 1;
while (!Empty(input) || nowout <= n)
{
queue* res;
if (!Empty(input) && nowout <= n)
{

int now = Pop(input);
if (now == nowout)
{
Push(output, now);
nowout++;

}
else if (search(hc, k, nowout, res))
{
Push(output, Pop(res));

nowout++;
if (seek(hc, k, now, res))
{
Push(res, now);

}
else if (find(hc, k, res))
{
Push(res, now);

}
else
{
cout << "无法重排!";
exit(0);
}
}
else
{
if (seek(hc, k, now, res))
{
Push(res, now);

}
else if (find(hc, k, res))
{
Push(res, now);

}
else
{
cout << "无法重排!";
exit(0);
}
}
}
else if (Empty(input) && nowout <= n)
{

if (search(hc, k, nowout, res))
{
Push(output, Pop(res));

nowout++;
}
}
}
cout << "最后出轨顺序(由左向右出轨):";
disp(output);
for (int i = 0; i < k; i++)
{
DestoryQueue(hc[i]);
}
DestoryQueue(input);
DestoryQueue(output);
}

运行结果

 如有同校学弟学妹们看到这篇博客,请务必认真自己思考,切忌不要随意CV。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不开心就喝水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值