简体   繁体   English

在C中打印nxn矩阵的所有可能路径

[英]Printing the all possible paths of the n x n matrix in C

I have a problem in the printing of all possible paths in the nxn matrix. 我在打印nxn矩阵中所有可能的路径时遇到问题。

Here is my code: 这是我的代码:

#include <stdio.h>
#include <string.h> 

void printPath(int x, int y, char path[], int n);  

char path[] = "";

int main(void) {
    int n = 2;
    printPath(0, 0, path, n);
    return 0;
}  

void printPath(int x, int y, char path[], int n) {
    char buff[12];
    sprintf(buff, "( %d , %d )", x, y);
    strcat(path, buff);
    if (x == n - 1 && y == n - 1) {
        printf("%s", path);
        printf("\n");
        return;
    }  

    if (x == n - 1)
        printPath(x, y + 1, path, n);
    else
    if (y == n - 1)
        printPath(x + 1, y, path, n);
    else {
        printPath(x + 1, y, path, n);
        printPath(x, y + 1, path, n);
    }
}

I need output as 我需要输出为

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

But am getting this output 但是正在得到这个输出

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

Please let me know what might be problem in my code 请让我知道我的代码中可能有什么问题

Maybe the below code would help. 也许下面的代码会有所帮助。

The problem with your code, was you were not removing the element from the string after the recursive call . 您的代码存在问题,因为您没有在递归调用之后从字符串中删除元素

Hence the path still contained the previous value. 因此,路径仍包含先前的值。

The last line in the printPath function does exactly the same. printPath函数的最后一行完全相同。 Though it might not be the effective way of doing it. 尽管这可能不是有效的方法。 But it should work. 但是应该可以。

#include <stdio.h>
#include<string.h>  
void printPath(int x,int y,char path[],int n);  

char path[500]="";
int main(void) {
int n=2;
printPath(0,0,path,n);
return 0;
}  

void printPath(int x,int y,char path[],int n){
char buff[12];
sprintf(buff,"( %d , %d )",x,y);
strcat(path,buff);
    if(x==n-1 && y==n-1){
        printf("%s",path);
        printf("\n");

    }  

   else if(x==n-1)
     printPath(x,y+1,path,n);
   else if(y==n-1)
    printPath(x+1,y,path,n);
   else{
    printPath(x+1,y,path,n);
    printPath(x,y+1,path,n);
    }

    int len1 = strlen(path);
    int len2 = strlen(buff);

    path[len1 - len2] = '\0';
}

There are multiple problems in your code: 您的代码中存在多个问题:

  • The global variable path is too small: char path[] = ""; 全局变量path太小: char path[] = ""; defines it to have a single element. 将其定义为具有单个元素。 The code invokes undefined behavior. 该代码调用未定义的行为。 Note that path does not need to be global. 请注意, path不必是全局的。 Global variables should be avoided if possible. 如果可能,应避免使用全局变量。

  • You append the current cell coordinates at each recursion step and they remain there for all subsequent paths. 您在每个递归步骤中附加当前单元格坐标,并且所有后续路径都将其保留在那里。 You should instead pass the offset for the next element in the output string. 相反,您应该为输出字符串中的下一个元素传递偏移量。

  • You do not test for potential buffer overflows, which will happen if n is sufficiently large. 您无需测试潜在的缓冲区溢出,如果n足够大,则会发生溢出。

Here is a corrected and simplified version: 这是一个经过纠正和简化的版本:

#include <stdio.h>
#include <stdlib.h>

static void printPath(int x, int y, int n,
                      char path[], size_t size, size_t pos)
{
    pos += snprintf(path + pos, size - pos, "( %d , %d )", x, y);
    if (pos >= size) {
        fprintf(stderr, "path is too small\n");
        exit(1);
    }
    if (x == n - 1 && y == n - 1) {
        printf("%s\n", path);
    } else {
        if (x < n - 1)
            printPath(x + 1, y, n, path, size, pos);
        if (y < n - 1)
            printPath(x, y + 1, n, path, size, pos);
    }
}

int main(void) {
    char path[1024];

    printPath(0, 0, 2, path, sizeof path, 0);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM