C++ 在这个递归中我错在哪里

C++ 在这个递归中我错在哪里,c++,algorithm,recursion,C++,Algorithm,Recursion,先生,这些天我在提高我的递归技能,但我陷入了《破解编码技能》一书中的一个递归问题。 问题编号8.2 问题陈述是——想象一个机器人坐在N*N网格的上角。机器人只能向两个方向移动:向右和向下。机器人有多少种可能的路径 我试了很多,但它只给我指明了一条路。我的代码是(从书的解决方案中获取帮助) 您的问题是,当x>=1时,您正在递减x,并将success设置为true。这会阻止代码探索递减y 正确的递归规则是: 如果x==0&&y==0,则返回1(空路径是唯一的路径) 将路径数初始化为0 如果x>0,则

先生,这些天我在提高我的递归技能,但我陷入了《破解编码技能》一书中的一个递归问题。 问题编号8.2
问题陈述是——想象一个机器人坐在N*N网格的上角。机器人只能向两个方向移动:向右和向下。机器人有多少种可能的路径

我试了很多,但它只给我指明了一条路。我的代码是(从书的解决方案中获取帮助)


您的问题是,当
x>=1
时,您正在递减
x
,并将
success
设置为true。这会阻止代码探索递减
y

正确的递归规则是:

  • 如果
    x==0&&y==0
    ,则返回1(空路径是唯一的路径)
  • 将路径数初始化为0
  • 如果
    x>0
    ,则添加由
    x-1
    产生的路径数(递归调用)
  • 如果
    y>0
    ,则添加由
    y-1
    产生的路径数(递归调用)
  • 返回添加的路径总数
  • 请注意,您必须同时执行步骤2和步骤3。您的代码只执行2(这将成功并阻止执行步骤3)

    编辑

    如果需要输出路径本身,则需要在递归时累积路径,并仅在到达目标时将其打印出来。(当你在打印的过程中打印每一步都不起作用。考虑从(2,2)到(0,0)经过的所有路径(1,1)。每个路径段从(2,2)到(1,1)需要被打印多次:一次从(1,1)到(0,0)的每个路径段。如果你在递归时打印,你不能这样做。 一种方法是将路径编码为一个长度等于预期路径长度的数组(所有路径的长度都正好是
    x+y
    步长),并用移动方向的代码进行填充。这里有一个方法:

    static const int DOWN = 0;
    static const int RIGHT 1;
    int findPath(int x, int y, int *path, int len) {
        if (x == 0 && y == 0) {
            printPath(path, len);
            return 1;
        }
        int n = 0;
        if (x > 0) {
            path[len] = DOWN;
            n += findPath(x-1, y, path, len + 1);
        }
        if (y > 0) {
            path[len] = RIGHT;
            n += findPath(x, y-1, path, len + 1);
        }
        return n;
    }
    
    void printPath(int *path, int len) {
        if (len == 0) {
            cout << "empty path";
        } else {
            cout << (path[0] == DOWN ? " d" : " r");
            for (int i = 1; i < len; ++i) {
                cout << " -> " << (path[i] == DOWN ? " d" : " r";
            }
        }
        cout << endl;
    }
    
    static const int DOWN=0;
    静态常数int右1;
    int findPath(int x,int y,int*path,int len){
    如果(x==0&&y==0){
    打印路径(路径,len);
    返回1;
    }
    int n=0;
    如果(x>0){
    路径[len]=向下;
    n+=findPath(x-1,y,path,len+1);
    }
    如果(y>0){
    路径[len]=右;
    n+=findPath(x,y-1,path,len+1);
    }
    返回n;
    }
    无效打印路径(int*path,int len){
    如果(len==0){
    
    难道我不明白,你能不能从零级解释我,因为我在递归问题上很弱。请。@algoh-现在我考虑一下,如果
    x==0&&y==0
    ,你应该返回1。(毕竟,空路径是一条路径。)这是基态。我会更新我的答案来说明这一点。然后,从,比方说,(0,1),步骤2测试失败,因此路径保持为0。步骤3测试成功,因此它将递归地添加(0,1-1)=(0,0)中的路径数,即1。事实上,从(0,1)到(0,0)只有一条路径。其他案例也可以进行类似的分析。我希望这现在更有意义。我已经按照你说的做了,但我有一个新问题。请用这样的递归算法检查更新后的问题,你不能边走边打印路径。你需要递归地累积路径,并在结束时打印每个路径。我将修改我的ans我们将展示如何做到这一点。
    int findPath(int x, int y) {
        if(x == 0 && y == 0) { cout << endl; return 1; }
        int path = 0;
        if(x > 0) { cout << "d -> ";path = path + findPath(x-1, y);  } // d = down
        if(y > 0) { cout << "r ->  ";path = path + findPath(x, y-1);  } // r = right
        return path;
    }
    
    d -> d ->r -> r ->
    r -> d -> r ->
    r -> d->
    r -> d -> d -> r ->
    r -> d ->
    r -> d -> d ->
    
    static const int DOWN = 0;
    static const int RIGHT 1;
    int findPath(int x, int y, int *path, int len) {
        if (x == 0 && y == 0) {
            printPath(path, len);
            return 1;
        }
        int n = 0;
        if (x > 0) {
            path[len] = DOWN;
            n += findPath(x-1, y, path, len + 1);
        }
        if (y > 0) {
            path[len] = RIGHT;
            n += findPath(x, y-1, path, len + 1);
        }
        return n;
    }
    
    void printPath(int *path, int len) {
        if (len == 0) {
            cout << "empty path";
        } else {
            cout << (path[0] == DOWN ? " d" : " r");
            for (int i = 1; i < len; ++i) {
                cout << " -> " << (path[i] == DOWN ? " d" : " r";
            }
        }
        cout << endl;
    }
    
    int *path = new int[x + y];
    cout << "Number of paths: " << findPath(x, y, path, 0) << endl;
    delete [] path;