I am having a problem with my function made to find a path in a maze of 1s and 0s, return true if it is on that path or has found the exit, and return false if the maze is unsolvable. I am getting a stack overflow error any time I try checking for the "- 1s" of my variables but my base cases should be preventing that. Is there a way to use less stack space with recursion? Here is my code
bool Pathfinder::check(string& maze, stack<string>& path, int x, int y, int z)
{int checking = 0;
if ((x == 4) && (y == 4) && (z == 4))
{
path.push(this->createCoords(x, y, z));
return true;
}
else
{
if ((x + 1) < 1 || (x + 1) > columns)
{
return false;
}
if ((y + 1) < 1 || (y + 1) > rows)
{
return false;
}
if ((z + 1) < 1 || (z + 1) > floors)
{
return false;
}
if ((x < 0) || (y < 0) || (z < 0))
{
return false;
}
if (this->getValue(maze, x, y, z) == 1)
{
this->setValue(maze, x, y, z, 2);
}
else
{
return false;
}
}
if (this->check(maze, path, x + 1, y, z) ||
this->check(maze, path, x, y + 1, z) ||
this->check(maze, path, x, y, z + 1))
{
checking++;
}
if (this->check(maze, path, x - 1, y, z) && checking == 1) //Overflow error comes from here
{
checking++;
}
if (this->check(maze, path, x, y - 1, z) && checking == 2)
{
checking++;
}
if (this->check(maze, path, x, y, z - 1) && checking == 3)
{
path.push(this->createCoords(x, y, z));
return true;
}
return false;
}
ifstatements have way too many parentheses. You don’t need any of the inner ones.