-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueens.cpp
More file actions
130 lines (120 loc) · 2.56 KB
/
NQueens.cpp
File metadata and controls
130 lines (120 loc) · 2.56 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
/*Question:
NQueens
Asked in:
Qualcomm
Google
Amazon
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
N Queens: Example 1
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]*/
int sol[1000][1000];
int n;
bool issafe(int ri, int ci)
{
if(ri>=n||ci>=n)
return false;
int a=ri,b=ci;
for(int i=0;i<n;i++)
{
if(sol[a][i]!=0)
return false;
}
for(int j=0;j<n;j++)
{
if(sol[j][b]!=0)
return false;
}
a=ri;b=ci;
while(a>=0&&b>=0)
{
if(sol[a][b]!=0)
return false;
a--;b--;
}
a=ri;b=ci;
while(a<n&&b<n)
{
if(sol[a][b]!=0)
return false;
a++;b++;
}
a=ri;b=ci;
while(a>=0&&b<n)
{
if(sol[a][b]!=0)
return false;
a--;b++;
}
a=ri;b=ci;
while(a<n&&b>=0)
{
if(sol[a][b]!=0)
return false;
a++;b--;
}
return true;
}
vector<vector<string>> ret;
void foo(int ri, int ci, int k)
{
//cout<<k<<" ";
if (k == n )
{
vector<string> v;
for(int i=0;i<n;i++)
{
string s;
for(int j=0;j<n;j++)
{
if(sol[i][j]==1)
s.push_back('Q');
else
s.push_back('.');
//cout<<s[j]<<" ";
}
//cout<<endl;
v.push_back(s);
}
ret.push_back(v);
return ;
}
for (int i = 0; i < n; i++)
{
if (issafe(ri , i))
{
sol[ri][i] = 1;
foo(ri + 1, 0, k + 1);
sol[ri][i] = 0;
}
}
}
vector<vector<string> > Solution::solveNQueens(int A) {
memset(sol,0,sizeof(sol));
n=A;
ret.clear();
if(A==1)
{
ret.push_back({"Q"});
return ret;
}
foo(0,0,0);
sort(ret.begin(),ret.end());
return ret;
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
}