Skip to content

N-Queens II #373

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Leetcode/CPP/52. N-Queens II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//problem link:https://leetcode.com/problems/n-queens-ii/
//Time complexity : best and average case O(n*n)
//worst case:O(n!)
class Solution {
public:
bool issafe(vector<string> &board,int n,int row,int col)
{
for(int i=0;i<col;i++)
{
if(board[row][i]=='Q')
{
return false;
}
}
for(int i=row,j=col; i>=0 && j>=0; i--,j--)
{
if(board[i][j]=='Q')
{
return false;
}
}
for(int i=row,j=col; i<n && j>=0; i++,j--)
{
if(board[i][j]=='Q')
{
return false;
}
}
return true;
}
void nqueen(vector<vector<string>> &ans,vector<string> &board,int n,int col)
{
if(col==n)
{
ans.push_back(board);
}
for(int row=0;row<n;row++)
{
if(issafe(board,n,row,col))
{
board[row][col]='Q';
nqueen(ans,board,n,col+1);
board[row][col]='.';
}
}
}
int totalNQueens(int n) {
vector<vector<string>>ans;
vector<string>board(n);
string s(n,'.');
for (int i = 0; i < n; i++)
{
board[i]=s;
}
nqueen(ans,board,n,0);
return ans.size();
}
};