C++ Queue Example Rearranging RailRoad Cars 火车车厢重排问题
来源:互联网 发布:汽车数据分析 编辑:程序博客网 时间:2024/05/01 12:33
Before in article C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题 we use three stacks to solve the "Rearranging RailRoad Cars" problem. In this article, we will use two queues to solve the problem.
The Whole Code
// RailRoadQueue.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <queue>using namespace std;template <class T>void PrintfNum(T a[], const int & n);bool Hold(queue<int> q[], int n,int totalQueue){ for(int i=0;i<totalQueue;++i){if(q[i].empty() || (!q[i].empty() && q[i].back() < n)){cout << "holding track " << i << " hold car " << n << endl;q[i].push(n);return true;// we already find a holding track, so break the loop. }}return false;}void OutPut(queue<int> q[], int n, int totalQueue,int& min){for(int i = 0;i< totalQueue;++i){if(!q[i].empty() && q[i].front() == min){cout << "Move car " << q[i].front() << " from holding track " << i << " to output" << endl;q[i].pop();++min;i = -1;// find next car from the first holding track 0}}}int main(int argc, char* argv[]){const int NUM = 9;const int QUEUENUM = 2; queue<int> q[QUEUENUM];int min = 1;int a[NUM] = {5,8,1,7,4,2,9,6,3};PrintfNum(a,NUM);for(int i = NUM - 1; i >=0 ;--i){if(a[i] == min){cout << "Move car " << a[i] << " from input to output" << endl;++min;//move cars from holding tracksOutPut(q,a[i],QUEUENUM,min);}else{// move cars to holding tracksif(!Hold(q, a[i] ,QUEUENUM)){ cout << "Not enough holding track" << endl; break;}}}return 0;}template <class T>void PrintfNum(T a[], const int & n){for(int i = 0; i < n; ++i){cout << a[i] << ",";}cout << endl;}
While two holding track are sufficient to rearrange the cars from the initial ordering of gif picture. However, other initial arrangements may need more tracks. For example, the inital arrangement 1, 2, 3, 4, 5, 6, 7, 8, 9 requires 8 holding tracks.
http://www.waitingfy.com/?p=515
- C++ Queue Example Rearranging RailRoad Cars 火车车厢重排问题
- C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
- 火车车厢重排问题
- 火车车厢重排
- 火车车厢重排
- 火车车厢重排/队列实现
- 火车车厢重排(链队列)
- 队列的应用--火车车厢重排列
- C#(链栈)实现火车重排问题
- 数据结构(2)——链表形式的堆栈,以及火车车厢重排问题
- 数据结构_链队列实验——火车车厢重排问题
- 数据结构与算法C++描述(7)---堆栈及其在“火车车厢重排问题”中的应用
- 火车车厢重新排列问题
- 火车车厢重排(栈式实现与队列实现)
- C++--车厢排序问题
- 列车车厢重排
- 火车车厢分布图
- 火车车厢分布图
- javascript 鼠标事件
- ServletResponse 中getOUtStreatm 和getWriter 混合使用
- 中小SAP项目中的人员编制
- POJ 1008
- 可用免费中文版Photoshop CS3
- C++ Queue Example Rearranging RailRoad Cars 火车车厢重排问题
- Android学习笔记之不同activity之间滑动切换
- AndroidManifest.xml文件解读
- SAP进阶:再论SAP权限
- 我只是在找资料2
- 如何从Eclipse导入github上的项目源码
- System.Windows.Markup.IQueryAmbient 在未被应用的程序集中定义
- c++ 封装的单链表
- storm集群的监控