forked from vineethm1627/SDE_Sheet_Striver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_Queens.cpp
59 lines (54 loc) · 1.94 KB
/
N_Queens.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Problem Link: https://leetcode.com/problems/n-queens/
*/
// helper function to check if the queen is safe to be placed
bool isQueenSafe(vector<string> &chess, int row, int col) {
// check vertically upwards
for(int i = row - 1, j = col; i >= 0; i--) {
// a queen is already there to attack
if(chess[i][j] == 'Q')
return false;
}
// check left upward diagonal
for(int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
// a queen is already there to attack
if(chess[i][j] == 'Q')
return false;
}
// check right upward diagonal
for(int i = row - 1, j = col + 1; i >= 0 && j < chess[0].size(); i--, j++) {
// a queen is already there to attack
if(chess[i][j] == 'Q')
return false;
}
// it is safe to place the queen at chess[row][col]
return true;
}
// helper function to place 1 Queen in one row
void printNQueens(vector<string> &chess, vector<vector<string>> &results, int row, int n) {
// base case: all rows are processed ie. all the queens are placed
if(row == n) {
results.push_back(chess);
return;
}
// go through all the options of placing queen in a column of a row
for(int col = 0; col < n; col++) {
// place the queen if it is a safe position
if(isQueenSafe(chess, row, col)) {
// place the queen
chess[row][col] = 'Q';
// recursive call: place queen in the next row
printNQueens(chess, results, row + 1, n);
// restore original config: remove the Queen for further iterations: backtracking
chess[row][col] = '.';
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> results;
// chess board of nxn with all values: '.'
vector<string> chess(n, string(n, '.'));
// start placing queen in 0th ie. first row
printNQueens(chess, results, 0, n);
return results;
}