Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在河内塔中,如何保持三个阵列(支柱)的有序?_C++_Recursion_Towers Of Hanoi - Fatal编程技术网

C++ 在河内塔中,如何保持三个阵列(支柱)的有序?

C++ 在河内塔中,如何保持三个阵列(支柱)的有序?,c++,recursion,towers-of-hanoi,C++,Recursion,Towers Of Hanoi,我编写了一个Hanoi Tower程序,但由于递归,输出切换了a、B和C支柱。为了制作动画,有没有办法保留这些柱子? 我的代码: #include <iostream> #include <vector> #include <stdlib.h> #include <windows.h> using namespace std; void printTowers(vector<int>& arr1, vector<in

我编写了一个Hanoi Tower程序,但由于递归,输出切换了a、B和C支柱。为了制作动画,有没有办法保留这些柱子? 我的代码:

#include <iostream>
#include <vector>
#include <stdlib.h> 
#include <windows.h> 
using namespace std;

void printTowers(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3)
{
    printOut(arr1); //prints vector using iterator
    printOut(arr2);
    printOut(arr3);
}
//------------
//  hanoi(number of disks, source pillar, spare pillar, target pillar)
void hanoi(int d, vector<int>& a, vector<int>& b, vector<int>& c)
{
    if(d == 1)
    {
        c.push_back(a.back());
        a.pop_back();
        printTowers(a,b,c); 
    }
    else{
    hanoi(d-1,a,c,b);
    hanoi(1,a,b,c);
    hanoi(d-1,b,a,c);
    }
}
//------------
int main()
{
    int n = 3; 
    vector <int> A, B, C;
    A.reserve(n); B.reserve(n); C.reserve(n);

    for(int i=0; i<n; i++)
    {
        A.push_back(n-i);
    }    
    hanoi(n,A,B,C);   
    return 0;
}
期望输出:

 321 | 32 | 3  | 3  |    | 1  | 1  |    |
     |    | 2  | 21 | 21 | 2  |    |    |
     | 1  | 1  |    | 3  | 3  | 32 | 321|

你可以在向量中使用一个额外的单元格来告诉你它是什么支柱。比如说

vector <int> A, B, C;
A.reserve(n+1); B.reserve(n+1); C.reserve(n+1);
A.push_back(-1);
B.push_back(-2);
C.push_back(-3);
向量A、B、C;
A.储备(n+1);B.储备(n+1);C.储备(n+1);
A.推回(-1);
B.推回(-2);
C.推回(-3);
现在

//添加新函数以简化代码
void printTowerId(向量&arr1,向量&arr2,向量&arr3,int-id){
if(arr1[0]==id)打印输出(arr1);
如果(arr2[0]==id)打印输出(arr2);
if(arr3[0]==id)打印输出(arr3);
}
您的原始函数如下所示

void printTowers(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3)
{
    printTowerId(arr1, arr2, arr3, -1);
    printTowerId(arr1, arr2, arr3, -2);
    printTowerId(arr1, arr2, arr3, -3);
}
void打印塔(向量&arr1、向量&arr2、向量&arr3)
{
printTowerId(arr1,arr2,arr3,-1);
printTowerId(arr1,arr2,arr3,-2);
printTowerId(arr1,arr2,arr3,-3);
}

注意在递归中不要占用您添加的单元格

是的,将名称作为每个支柱的一部分。
//add a new function to simplify code
void printTowerId(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3, int id){  
    if(arr1[0] == id) printOut(arr1);
    if(arr2[0] == id) printOut(arr2);
    if(arr3[0] == id) printOut(arr3);
}
void printTowers(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3)
{
    printTowerId(arr1, arr2, arr3, -1);
    printTowerId(arr1, arr2, arr3, -2);
    printTowerId(arr1, arr2, arr3, -3);
}