Possible permutations at a railway track

Given a left, right, and a spur track as shown in the below figure. There are N trucks from value 1 to N arranged in the left track. We can move directly N trucks to the right track but there can be more possibilities of moving the trucks to the right track using the spur track. We can move any truck to spur track and then move it to the right track. The task is to print all the possible permutation order in which all the N trucks can be moved from left track to right track.
Note: Once a truck is moved from left truck to right/spur track then it can’t be moved to left track again.

Examples:

Input: N = 2
Output:
1 2
2 1
Explanation:
For the first permutation:
left[] = {1, 2} right[] = {}, and spur[] = {}

The truck with value 2 moved to the right track, then
left[] = {1} right[] = {2}, and spur[] = {}

Now moving with value 1 to the right track, then
left[] = {} right[] = {1, 2}, and spur[] = {}



For the second permutation:
left[] = {1, 2} right[] = {}, and spur[] = {}

The truck with value 2 move to the spur track, then
left[] = {1} right[] = {}, and spur[] = {2}

The truck with value 1 move to the right track, then
left[] = {} right[] = {1}, and spur[] = {2}

The truck with value 2 in the spur track move to the right track, then
left[] = {} right[] = {2, 1}, and spur[] = {}

Input: N = 3
Output:
1 2 3
2 1 3
3 2 1
3 1 2
2 3 1

Approach: This problem is a variation of Tower Of Hanoi and can be solved using Recursion. Below are the following cases:

  • Case 1: We can move the truck from left track to the spur track and recursively check for the remaining trucks on the left and spur tracks.
  • Case 2: We can move the truck from the spur track to right track and check for the remaining trucks of the left and spur tracks.

Below are the steps:

  1. At each step we can move either truck from left track to spur track or from spur track to right track.
  2. Move one truck from left track to spur track and call recursively for remaining trucks on left and spur track.
  3. At any recursive call, if the input track is empty then, move every truck on the spur track to right track and print the current permutation on the right track
  4. Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program for the above approach
    #include "bits/stdc++.h"
    using namespace std;
      
    // Helper function to print all the
    // possible permutation
    void printPermute(vector<int>&input,
                      vector<int>&spur,
                      vector<int>&output)
    {
      
        // If at any recursive call input
        // array is empty, then we got our
        // one of the permutation
        if(input.empty())
        {
              
            // Print the right track trucks
            for(auto &it : output) {
                cout << it << ' ';
            }
              
            // Print the spur track trucks
            for(auto &it : spur) {
                cout << it << ' ';
            }
              
            cout << endl;
        }
        else
        {
            int temp;
              
            // Pop the element from input
            // track and move it to spur
            temp=input.back();
            input.pop_back();
              
            // Case 1
            // Push the popped truck from
            // input to spur track
            spur.push_back(temp);
              
            // Recursive call for remaining
            // trucks on input, spur and
            // output track
            printPermute(input,spur,output);
              
            // remove the top truck from spur
            // track and push it in input for
            // Case 2 iteration
            spur.pop_back();
            input.push_back(temp);
              
            // Case 2
            if(!spur.empty()) {
                  
                // Remove the truck from the spur
                // track and move it to the 
                // output track
                temp=spur.back();
                spur.pop_back();
                output.push_back(temp);
                  
                // Recursive call for remaining
                // truck on input, spur and
                // output track
                printPermute(input,spur,output);
                  
                // Remove the top truck from the
                // output track and move it to
                // the spur track for the next
                // iteration
                output.pop_back();
                spur.push_back(temp);
            }
        }
    }
      
    // Function to print all the possible
    // permutation of trucks
    void possiblePermute(int n)
    {
        // Array for left, spur and right track
        vector<int>spur;
        vector<int>output;
        vector<int>input;
          
        // Insert all truck value 1 to N
        for(int i = 1; i <= n; i++) {
            input.push_back(i);
        }
          
        // Helper function to find
        // possible arrangement
        printPermute(input, spur, output);
    }
      
    // Driver Code
    int main()
    {
        // Input number of truck
        int N = 4;
          
        // Function Call
        possiblePermute(N);
    }

    chevron_right

    
    

    Output:

    1 2 3 4 
    2 1 3 4 
    3 2 1 4 
    4 3 2 1 
    3 1 2 4 
    2 3 1 4 
    4 2 3 1 
    4 3 1 2 
    2 4 3 1 
    4 1 2 3 
    2 4 1 3 
    3 2 4 1 
    3 4 1 2 
    2 3 4 1 
    

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.