I am trying to solve the "N Queen Problem" using backtracking but due to some error it is showing runtime error.
On compilation it displays runtime error and gives the message dynamic-stack-buffer-overflow on address ******
Checked the code many times but was unable to find the source of the problem
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
vector<string> temp;
int arr[n]={0};
arr[0]=99;
vector<int> queen;
calc(result,n,temp,arr,0,queen);
return result;
}
void calc(
vector<vector<string>>& result,
int n, vector<string>& temp,
int * arr,
int count,
vector<int>& queen
) {
if(count==n)
{
for(int k=0;k<queen.size();k++)
{
string s="";
for(int m=1;m<=n;m++)
{
if(m==queen[k])
s=s+'Q';
else
s=s+'.';
}
temp.push_back(s);
}
result.push_back(temp);
}
else{
for(int i=1;i<=n;i++)
{
if(arr[i]==0)
{
int temp1[n]={0};
temp1[0]=99;
for(int j=1;j<=n;j++)
{
temp1[j]=arr[j]+temp1[j];
if(arr[j]!=0)
temp1[j+1]++;
}
queen.push_back(i);
temp1[i]++;
calc(result,n,temp,temp1,count+1,queen);
queen.pop_back();
}
}
}
}
};