繁体   English   中英

如果结构作为递归函数中的参数传递,我该如何初始化结构的成员变量?

[英]How do i initialize member variables of a struct if the struct is passed as an argument in a recursive function?

请帮忙! 我将作为参数传递给函数的结构是:

在我的查询上下文中的摘要代码

struct mine_index
{
    int row, col;
};
struct miner
{
    bool up, down, right, left;
};

can_solve(mine_index start, mine_index end, miner the_miner)
{
    can_solve(start(row+1,col), end, miner the_miner);
    return ;
}

下面给出的是递归函数的完整代码

bool can_solve(mine_index start, mine_index end, miner the_miner)
{

    bool solution[size-1][size-1]
    for (int i=0; i<size; i++)
    {
        for (int j=0; j<size; j++)
        {
            solution[i][j]=0;
        }
    }
    if(start.row==size-1 && start.col==size-1)//base case 
    {
        solution[start.row][start.row]=1
        return true;
        {
            if(start.row>=0 && start.row<size-1 && start.col>=0 && start.col<size-1) //
            {
                solution[start.row][start.col]=1;
            }
            if(can_solve(start(row+1,col), end, miner the_miner))
            {
                return true;
            }   
            if(can_solve(start(row,col+1), end, miner the_miner))
            {
                return true;
            }
            if(can_solve(start(row-1,col), end, miner the_miner))
            {
                return true;
            }
            if(can_solve(start(start.row,start.col-1), end, miner the_miner))
            {
                return true;
            }   
        }

该声明是否有效(如果无效),那么还有另一种方法可以声明它。 我正在使用递归函数。 整个功能如下。 我现在需要处理逻辑,但是我只想知道我的语法是否正确?

您的代码中有一些琐碎的语法错误。 例如:

  • 使用关键字结尾
  • 错误的开合大括号。
  • 等等...

首先,我认为您应该尝试使用某些C ++ IDE(例如CodeBlocks)编写代码。 IDE会注意到您的语法错误。 在纠正所有语法错误之前,可以考虑在下一步中使用function。

除了start(r, c) ,您只需要{r, c}即可创建mine_index 因此,而不是:

        if(can_solve(start(row+1,col), end, miner the_miner))

你要:

        if(can_solve({row+1,col}, end, the_miner))

但是我怀疑can_solve每次调用都有自己的solution副本。 我认为外部可见函数需要创建solution (一次),然后将其传递给内部函数。 然后内部函数是递归的。 (实际上,这是递归函数的一种非常常见的模式:外部函数先进行设置,然后调用内部递归函数。)

暂无
暂无

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

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