简体   繁体   English

在int main()中实现递归函数[c ++]

[英]Implement a recursive function in int main() [c++]

I am using a recursive floodfill function in my program. 我在程序中使用了递归泛洪函数。 The program is given below 该程序如下

void floodfill(int x, int y, int &array[100][100])
{
 if (array[x][y]==0)
  {
   array[x][y]=1;
   floodfill(x+1, y, array);
   floodfill(x, y+1, array);
   floodfill(x-1, y, array);
   floodfill(x, y-1, array);
  }
}

int main()
{
   int x= 1;// x and y is the point to start the floodfill
   int y= 10;
   int array[100][100] = {0};

   floodfill(x, y, array); 
}

I was wondering if there is a way to program this recursive function to happen completely inside the int main loop without creating a function? 我想知道是否有一种方法可以编程此递归函数,使其完全在int主循环内发生而不创建函数?

PS:- I am aware that it is good programming practice to define a function and then use it in the main program, but the architecture of the project I am working on does not enable me to do floodfill using function. PS:-我知道先定义一个函数然后在主程序中使用它是一种好的编程习惯,但是我正在从事的项目的体系结构无法使我使用函数进行洪水填充。 hence I want the complete floodfill program in the int main section.Also this is not the complete code. 因此,我需要int主要部分中完整的Floodfill程序。这也不是完整的代码。 the part where the values of the array get initialized are not provided in the code. 代码中未提供初始化数组值的部分。

Short answer: No. 简短答案:不可以。

You need to pass parameters to make your code work. 您需要传递参数以使代码正常工作。 Main takes no parameters. Main不带任何参数。

And I then feel the need to ask why you would want to do that? 然后,我觉得有必要问你为什么要这么做? It's good programming practice to break your code down to small, well defined parts, that carry out a specific task. 将代码分解为定义明确的细小部分,执行特定任务是一种好的编程习惯。 And then you isolate that in a function, and you document/describe that function for everyone else to understand what it does. 然后将其隔离在一个函数中,并为其他所有人记录/描述该函数以了解其功能。

So even if you could do that, you really should refrain from doing it. 因此,即使您可以做到这一点,您也应该避免这样做。

DFS can be implemented without recursion using stack only, so u might want to see this: https://www.codeproject.com/Tips/723337/Depth-First-Search-DFS-Non-recursive 可以仅使用堆栈而无需递归地实现DFS,因此您可能希望看到以下内容: https : //www.codeproject.com/Tips/723337/Depth-First-Search-DFS-Non-recursive

this code is implemented for the tree, but the algorithm is the same. 该代码是为树实现的,但是算法是相同的。

Local non-static variables, including arrays like your array , are by default not initialized. 局部非静态变量,包括像array这样的array ,默认情况下不会初始化。 Their values will be indeterminate and seem almost random. 它们的值是不确定的 ,似乎几乎是随机的。

Using such variables in any way, except to initialize them, leads to undefined behavior . 除初始化变量外,以任何方式使用此类变量都会导致未定义的行为

And that's what happens because you compare a value in your array without it being initialized. 这就是发生的原因,因为您在未初始化的情况下比较了数组中的值。

Furthermore, and also a source of undefined behavior , you have no bounds-checking! 此外,它也是不确定行为的来源,您没有边界检查! That means your function can (and will) go out of bounds of your array. 这意味着您的函数可以(并且将)超出数组的范围。


To solve the first problem you need to initialize your array: 要解决第一个问题,您需要初始化数组:

int array[100][100] = { 0 };  // Set all elements to zero

To solve the second problem you need to add bounds-checking in your function: 要解决第二个问题,您需要在函数中添加边界检查:

if (x < 0 || x >= 100 || y < 0 || y >= 100)
    return;

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

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