给定左,右和支线轨迹,如下图所示。有N个卡车从值1到N布置在左履带。我们可以将N辆卡车直接移到正确的轨道,但使用支线轨道将卡车移到正确的轨道还有更多的可能性。我们可以将任何卡车移至正轨,然后再移至正确的轨道。任务是打印所有可能的排列顺序,在该排列顺序中,所有N辆卡车都可以从左轨道移到右轨道。
注意:一旦卡车从左卡车移至右/辅助轨道,便无法再次移至左轨道。
例子:
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
方法:此问题是河内塔的一种变体,可以使用递归解决。以下是以下几种情况:
- 情况1:我们可以将卡车从左侧轨道移至支线轨道,然后递归检查左侧和支路轨道上的其余卡车。
- 情况2:我们可以将叉车从支线轨道移到右侧,并检查左侧和支路的其余卡车。
步骤如下:
- 在每一步中,我们都可以将卡车从左履带移动到正轨,也可以从正轨移动到右轨。
- 将一辆卡车从左轨道移动到支线轨道,并递归调用剩余卡车在左和支线轨道上。
- 在任何递归调用中,如果输入轨道为空,则将支路轨道上的每辆卡车移动到正确的轨道,并在正确的轨道上打印当前排列
下面是上述方法的实现:
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Helper function to print all the // possible permutation void printPermute(vector
&input, vector &spur, vector &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 spur; vector output; vector 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); } 输出: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