Excuse me, my primary language isn`t English.
I wrote a recursion function to resolve the "Queens Problem".
int* Queen_Recursion(int n,int nowRow = 1, int nowColumn = 1, int* map = nullptr)
{
if (map == nullptr)
map = new int[n + 1]{0};
if (nowRow > n)
{
std::cout << "No." << ++map[0] << " Answer:";
for (int i = 1; i < n + 1; i++)
std::cout << '\t' << '(' << i << ", " << map[i] << ')';
std::cout << std::endl;
return Queen_Recursion(n, n, map[n] + 1, map);
}
else if (nowColumn > n)
{
if (nowRow == 1)
return map;
map[nowRow] = 0;
return Queen_Recursion(n, nowRow - 1, map[nowRow - 1] + 1, map);
}
bool CanPlace = true;
for (int i = 1; i < nowRow; i++)
if (map[i] == nowColumn || i - nowRow == map[i] - nowColumn || i - nowRow == nowColumn - map[i])
CanPlace = false;
if (CanPlace)
{
map[nowRow] = nowColumn;
return Queen_Recursion(n, nowRow + 1, 1, map);
}
else
return Queen_Recursion(n, nowRow, nowColumn + 1, map);
}
int main()
{
int* temp = Queen_Recursion(8);
delete temp;
return 0;
}
When I choose the "debug", it shows me only 5 answers.
When I choose the "Release", it shows me 92 answers. Certainly, it`s correct.
Can someone tell me the reason?
By the way, I tried to set initial values of the "map", and I think there isn`t an out of bounds array access in this function.
Unhandled exception at 0x00007FF73D68298D in 04-Some Recursion Problems.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000FA88903EC0). occurred