forked from kritsid/competetive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktrack-nQueen.cpp
More file actions
45 lines (43 loc) · 1.48 KB
/
backtrack-nQueen.cpp
File metadata and controls
45 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
bool corrPos(vector<string>&board,int row,int col,int A)
{
for(int r=0;r<row;r++)//in this line we chcek the rows below the current row. If Q present then false i.e we can not take this row
if(board[r][col]=='Q')
return false;
for(int r=row,c=col;r>=0 and c>=0;r--,c--)//in this line we chcek by going in left below
if(board[r][c]=='Q')
return false;
for(int r=row,c=col;r>=0 and c<A;r--,c++)//in this line we chcek by going in right below
if(board[r][c]=='Q')
return false;
return true;//if there is no false then we can place Q in our current row and coloumn
}
void queen(vector<vector <string> >&ans,vector<string> &board,int row,int A)
{
if(row==A)
{
ans.push_back(board);
return;
}
for(int col=0;col<A;col++)//we will go each coloum of each row. like 0th row: 0,1,2,3 <-coloumns
{
if(corrPos(board,row,col,A))
{
board[row][col]='Q';//each time we have 2 choice whether we take Q or not. Here we take Q as consideration
queen(ans,board,row+1,A);
board[row][col]='.';
}
}
return;
}
vector<vector <string> >Solution::solveNQueens(int A) {
vector<vector<string> >ans;
string rows;
vector<string> board;
for(int i=0;i<A;i++)
rows.push_back('.');
for(int i=0;i<A;i++)
board.push_back(rows);
queen(ans,board,0,A);
return ans;
}