forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN Queen Problem.cpp
95 lines (82 loc) · 1.69 KB
/
N Queen Problem.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
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
#include<iostream>
#define MAX 100
using namespace std;
int board[MAX][MAX] = {0};
bool checkup(int i, int j)
{
for(int row = i-1; row >= 0; row--)
if(board[row][j] == 1)
return false;
return true;
}
bool checkleft(int i, int j)
{
int x = i-1;
int y = j-1;
while((x >=0) && (y >=0))
{
if(board[x][y] == 1)
return false;
x--;
y--;
}
return true;
}
bool checkright(int i, int j, int n)
{
int row = i-1;
int col = j+1;
while((col < n) && (row >=0))
{
if(board[row][col] == 1)
return false;
row--;
col++;
}
return true;
}
bool solveNQueen(int i, int j)
{
//Base Case - All row completed
if(i == j)
{
for(int a = 0; a <j; a++)
{
for(int b = 0; b<j; b++)
{
if(board[a][b] == 0)
cout<<" _ ";
else
cout<<" Q ";
}
cout<<endl;
}
return true;
}
//Recursive Case
//Trying placing queen in each block
for(int col = 0; col <j; col++)
{
//Step 1:- To check check if the queen can be placed there
if(checkup(i,col) && checkleft(i,col) && checkright(i,col,j))
{
//place the queen
board[i][col] = 1;
if(solveNQueen(i+1, j))
{
cout<<"\n\n\n";
}
//remove the queen
board[i][col] = 0;
}
}
return false; //Backtrack
}
int main()
{
int n;
cout<<"Enter the size of the chess board:- ";
cin>>n;
int board[n][n] = {0};
solveNQueen(0, n);
}