-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnQueenProblem.cpp
More file actions
179 lines (153 loc) · 3.99 KB
/
nQueenProblem.cpp
File metadata and controls
179 lines (153 loc) · 3.99 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printSolution(vector<vector<char>> board, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl
<< endl;
}
bool isSafe(int row, int col, vector<vector<char>> &board, int n)
{
// check karna chahte h , k kya main
// current cell [row,col] pr QUEEN rakh
// sakta hu ya nahi
int i = row;
int j = col;
// check row
while (j >= 0)
{
if (board[i][j] == 'Q')
{
return false;
}
j--;
}
// check upper left diaglnol
i = row;
j = col;
while (i >= 0 && j >= 0)
{
if (board[i][j] == 'Q')
{
return false;
}
i--;
j--;
}
// check bottom left diagnol
i = row;
j = col;
while (i < n && j >= 0)
{
if (board[i][j] == 'Q')
{
return false;
}
i++;
j--;
}
// kahin pr bhi queen nahi mili
// iska matlab ye position safe hai
// iska matlab eturn kardo true
return true;
}
void solve(vector<vector<char>> &board, int col, int n)
{
// base case
if (col >= n)
{
printSolution(board, n);
return;
}
// 1 case solve karna h , bakki recursion sambhal lega
for (int row = 0; row < n; row++)
{
if (isSafe(row, col, board, n))
{
board[row][col] = 'Q';
// recursion
solve(board, col + 1, n);
// backtraking
board[row][col] = '-';
}
}
}
int main()
{
int n = 6;
vector<vector<char>> board(n, vector<char>(n, '-'));
int col = 0;
// - => empty cell
// 'Q' => Queen at the cell
solve(board, col, n);
return 0;
}
// // OPTIMISED SOLUTION
// class Solution {
// public:
// unordered_map<int,bool> rowCheck;
// unordered_map<int,bool> upperLeftDiagnolCheck;
// unordered_map<int,bool> bottomLeftDiagnolCheck;
// void storeSolution(vector<vector<char>> &board, int n, vector<vector<string> > &ans) {
// vector<string> temp;
// for(int i=0; i<n; i++) {
// string output = "";
// for(int j=0; j<n ;j++) {
// output.push_back(board[i][j]);
// }
// temp.push_back(output);
// }
// ans.push_back(temp);
// }
// bool isSafe(int row, int col, vector<vector<char>> &board, int n) {
// if(rowCheck[row] == true )
// return false;
// if(upperLeftDiagnolCheck[n-1+col-row] == true)
// return false;
// if(bottomLeftDiagnolCheck[row+col] == true)
// return false;
// return true;
// }
// void solve(vector<vector<char>> &board, int col, int n, vector<vector<string> > &ans ) {
// //base case
// if(col >= n) {
// storeSolution(board, n, ans);
// return ;
// }
// //1 case solve karna h , baaki recursion sambhal lega
// for(int row = 0; row <n; row++) {
// if(isSafe(row, col, board, n)) {
// //rakh do
// board[row][col] = 'Q';
// rowCheck[row] = true;
// upperLeftDiagnolCheck[n-1+col-row] = true;
// bottomLeftDiagnolCheck[row+col] = true;
// //recursion solution laega
// solve(board, col+1, n, ans);
// //backtracking
// board[row][col] = '.';
// rowCheck[row] = false;
// upperLeftDiagnolCheck[n-1+col-row] = false;
// bottomLeftDiagnolCheck[row+col] = false;
// }
// }
// }
// vector<vector<string>> solveNQueens(int n) {
// vector<vector<char>> board(n, vector<char>(n,'.'));
// vector<vector<string> > ans;
// int col = 0;
// //0 -> empty cell
// //1 -> Queen at the cell
// solve(board, col, n, ans);
// return ans;
// }
// };