Who can tell me why my code will crash when n=8?


  • 0
    C
    class Solution {
    public:
        bool check_valid(int *A,int n){
            for(int i=0;i<n-1;i++)
                for(int j=i+1;j<n;j++){
                    if(A[i]-A[j]==i-j||A[i]-A[j]==j-i){
                        return false;
                    }
                }
            return true;
        }
        void generate_result(int *A,int n,vector<vector<string> > &result){
            string s(n,'.');
    	    vector<string> solution(n,s);
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++)
                    solution[i][A[i]]='Q';
            }
            result.push_back(solution);
        }
        void all_sort(int *A,int n,int start_index,vector<vector<string> >&result ){
            if(n==0)
                return;
            if(n==start_index+1){
                if(check_valid(A,n))
                    generate_result(A,n,result);
            }else{
                for(int i=start_index;i<n;i++){
                    swap(A[start_index],A[i]);
                    all_sort(A,n,start_index+1,result);
                    swap(A[start_index],A[i]);
                }
            }
        }
        vector<vector<string> > solveNQueens(int n) {
            int *array=new int(n);
            for(int i=0;i<n;i++)
                array[i]=i;
            vector<vector<string> > out;
            all_sort(array,n,0,out);
    	    delete[] array;
            return out;
        }
    };
    

    it can run in VC,but will crash when use g++.......


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.