zoukankan      html  css  js  c++  java
  • nullnullC++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题

    首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

        null和null

        

        Look at figure 1. The railroad cars  in the "Input track" is not sorted. We need them in a sorted way  in "Output track" like cars in figure2.

        

        null和null

        

        There are three holding tracks we can use to rearranging roailroad cars. A picture is worth than a thousand words, so look at the gif picture below. You can see how the process acts.

        

        

        

        null和null

        

        

        So, how we use code to solve the problem? Here three holding tracks in the picture, so we can use three stacks to hold the number.

        

        The Whole Code

        每日一道理
    即使青春是一枝娇艳的花,但我明白,一枝独放永远不是春天,春天该是万紫千红的世界。 即使青春是一株大地伟岸的树,但我明白,一株独秀永远不是挺拔,成行成排的林木,才是遮风挡沙的绿色长城。即使青春是一叶大海孤高的帆,但我明白,一叶孤帆很难远航,千帆竞发才是大海的壮观。
    // RailRoad.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    template <class T>
    void PrintfNum(T a[], const int & n);
    
    // move cars from holding track to output track
    void OutPut(stack<int> t[],int n, int totalStack,int& min){
    			 //move car from holding track
    			 for(int x = 0;x < totalStack; ++x){
    				 if(!t[x].empty() && t[x].top() == min){
    					 cout << "Move car " << t[x].top() << " from holding track " << x << " to output" << endl;
    					 t[x].pop();
    					 ++min;
    					 x = -1; // find next car from the first holding track 0
    				 }
    			 }
    }
    
    // move cars from input track to holding track
    bool Hold(stack<int> t[],int n , int totalStack){
    	for(int i = 0;i < totalStack; ++i){
    				if(t[i].empty() || (!t[i].empty() && t[i].top() > n)){
    					cout << "holding track " << i << " hold car " << n  << endl;
    					t[i].push(n);
    					return true; // we already find a holding track, so break the loop. 
    				}
    	}
    	return false;
    }
    
    int main(int argc, char* argv[])
    {
    	const int NUM = 9;
    	const int STACKNUM = 3;
    	stack<int> t[STACKNUM];
    	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){// try to move cars from input track or holding track
    			cout << "Move car " << a[i] << " from input to output" << endl;
    			 ++min;
                OutPut(t,a[i],STACKNUM,min); 			 
    		}else{// move cars from input track to holding track
    			
                if(!Hold(t,a[i],STACKNUM)){
                    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;
    }

        null和null

        

        While three holding tracks are sufficient to rearrange the cars from the initial ordering of Figure 1, other initial arrangements may need more tracks. For example, the initial arrangement 1, 9, 8, 7, 6, 5, 4, 3, 2 requires 8 holding tracks.

        http://www.waitingfy.com/?p=508

    文章结束给大家分享下程序员的一些笑话语录: 一程序员告老还乡,想安度晚年,于是决定在书法上有所造诣。省略数字……,准备好文房4宝,挥起毛笔在白纸上郑重的写下:Hello World

  • 相关阅读:
    About me
    新blog!!!
    卡常技巧
    考试策略
    Bzoj 1260: [CQOI2007]涂色paint (区间DP)
    Bzoj 1081 [Ahoi2009] chess 中国象棋
    NOIP2018 全国热身赛 第二场 (不开放)
    AT2386 Colorful Hats (乱搞题,思维题)
    模拟赛2
    CF873B Balanced Substring (前缀和)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3065785.html
Copyright © 2011-2022 走看看