-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN-Queens.cpp
More file actions
64 lines (64 loc) · 1.15 KB
/
N-Queens.cpp
File metadata and controls
64 lines (64 loc) · 1.15 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
class Solution
{
public:
void DFS(vector<string> &result, vector<vector<string> > &ans, int depth, int n, int X[], int Y[])
{
int x=depth, y;
int tx, ty, i,j;
if(depth==n)
{
ans.push_back(result);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%c",result[i][j]);
printf("\n");
}
printf("\n");
return ;
}
for(y=0;y<n;y++)
{
for(tx=x-1;tx>=0;tx--)
{
if(result[tx][y]=='Q')
break;
}
if(tx>=0)
continue;
for(i=0;i<4;i++)
{
tx=x, ty=y;
while(tx>=0&&tx<n&&ty>=0&&ty<n)
{
if(result[tx][ty]=='Q')
break;
tx+=X[i];
ty+=Y[i];
}
if(tx>=0&&tx<n&&ty>=0&&ty<n)
break;
}
if(i<4)
continue;
result[x][y]='Q';
DFS(result, ans, depth+1, n, X, Y);
result[x][y]='.';
}
}
vector<vector<string> > solveNQueens(int n)
{
int X[]={-1, -1, 1, 1};
int Y[]={-1, 1, 1,-1};
string empty;
vector<string> result;
vector<vector<string> > ans;
int i;
for(i=0;i<n;i++)
empty+='.';
for(i=0;i<n;i++)
result.push_back(empty);
DFS(result, ans, 0, n, X, Y);
return ans;
}
};