Your runtime beats 90.86% of cpp submissions


  • 0
    L
    bool dfs(vector<vector<string>>& result, int n, vector<int>& rtol,	vector<int>& ltor,
    	vector<int>& column, int qu, vector<string>&data)
    {
    	int i; bool flag = false;
    	if( n > qu)
    	{	
    		result.push_back(data);
    		return true;
    	}
    	for( i = 1; i <= qu; ++i)
    	{
    		if( rtol[i + n] && ltor[i - n + qu] && column[i])
    		{
    			rtol[i + n] = ltor[i - n + qu] = column[i] = 0;
    			data[n-1][i-1] = 'Q';
    			flag |= dfs(result, n + 1, rtol, ltor, column, qu, data);
    			rtol[i + n] = ltor[i - n + qu] = column[i] = 1;
    			data[n-1][i-1] = '.';
    		}
    	}
    
    	return flag;
    	
    }
    
    vector<vector<string>> solveNQueens(int n) {
    	vector<int> rtol(2*n+1, 1);
    	vector<int> ltor(2*n+1, 1);
    	vector<int>	column(n+1, 1);
    	vector<int> pos(n+1);
    	vector<vector<string>> result, re;
    	string tmp(n,'.');
    	vector<string> data(n,tmp);
    
    	return dfs(result,1,rtol, ltor,column ,n, data) ? result : re;
        
    }

Log in to reply
 

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