【问题标题】:All paths in the matrix that pass through given point矩阵中通过给定点的所有路径
【发布时间】:2013-11-04 16:01:03
【问题描述】:

有人可以帮忙解答以下面试问题吗:

a 成为MxN 矩阵。 使用以下原型编写一个函数:

void PrintAllPaths(int** a, int M, int N, int x, int y) {};

打印矩阵中从左下角(0,0) 到右下角(M-1,N-1) 的所有路径,这些路径沿途通过点(x,y)。在路径的每一步,一个人只能向右或向下走。 示例:

PrintAllPaths (a, 3, 3, 1, 2);

应该打印:

(0,0)(0,1)(0,2)(1,2)(2,2)
(0,0)(0,1)(1,1)(1,2)(2,2)
(0,0)(1,0)(1,1)(1,2)(2,2)

我有几个想法:

  1. 查找从(0,0)(x,y) 的所有路径以及从(x,y)(M-1,N-1) 的所有路径。以某种方式从这两个路径中生成笛卡尔积。

  2. 使用矩阵a 进行回溯。我想它可以使用,因为该函数的打印输出不依赖于 a 中的数据。通过向右递归求解(a,M,N-1,x,y-1),向下递归求解(a,M-1,N,x-1,y),求解(a,M,N,x,y)

但是,我在实现这一点时遇到了问题。我无法正确跟踪路径。 有人可以提供代码(或更好的想法)吗?

【问题讨论】:

    标签: matrix path


    【解决方案1】:

    与此同时,我终于设法将想法 2 转换为工作代码。我没有使用矩阵a 进行路径回溯,因为它不是必需的。 它仍然相当复杂,所以如果有人有更优雅的解决方案,请发布!

    #include <stdio.h>
    #include <string.h>
    
    void PrintAllPaths(int **a, int M, int N, int x, int y) {
        static char Path[500]="(0,0)"; //keeps path for currently processed recursion
        static int m=M, n=N; //keep initial values of M and N
        static bool passedXY=false; //have we passed (x,y) along current path?
    
        if ((x<0 || y<0) && (passedXY==false)) { return;} //missed (X,Y), so skip
        if (x==0 && y==0) passedXY=true;
    
        if ((M==1 && N==1)) {puts(Path); return;};  //we have made N-1 steps down and M-1 steps right, so we reached (M-1,N-1) and we print path
    
        //try to go right   
        if (N>1) {
            char temp[10];
            sprintf(temp,"(%d,%d)",m-M,n-N+1); //add point to the right to the path
            strcat(Path,temp);
            PrintAllPaths(a, M, N-1, x, y-1);  //go right
            Path[strlen(Path)-5]='\0';  //remove last point from the path, since it is processed
            if ((x>0 && y>=0)||(x>=0 && y>0)) passedXY=false; //reset passedXY value if needed
        };
    
        //try to go down
        if (M>1) {
            char temp[10];
            sprintf(temp,"(%d,%d)",m-M+1,n-N);
            strcat(Path,temp);
            PrintAllPaths(a, M-1, N, x-1, y); 
            Path[strlen(Path)-5]='\0';
            if ((x>0 && y>=0)||(x>=0 && y>0)) passedXY=false;
        }
    
    };
    
    
    int main() {
    int **a=0;
    PrintAllPaths(a, 3, 3, 1, 2);
    char ch='a'; scanf("%c",ch);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2019-01-14
      • 2021-11-27
      • 1970-01-01
      • 2012-03-30
      相关资源
      最近更新 更多