-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path04_n_queen_using_backtracking.cpp
155 lines (131 loc) · 3.44 KB
/
04_n_queen_using_backtracking.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
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
/*
Topic - N-Queen Using Backtracking
We have (N x N) chess board. We have to place N-Queens such that all queens are safe & no queens
cuts each other.
- print the cess board after placing N-Queens on it.
- print all possible ways in which N-Queens can be placed on chess board.
- count the number of ways in which N-Queens can be placed on chess board.
*/
#include <iostream>
using namespace std;
int count = 0; // global variable
// function to check for safe cell when placing queens on board
bool isSafe(char board[][20], int row, int col, int n)
{
//check for column
for(int i=0; i<row; i++)
{
if(board[i][col] == 1)
{
return false;
}
}
// check for left diagonal
int x = row;
int y = col;
while(x>=0 && y>=0)
{
if(board[x][y] == 1)
{
return false;
}
x--;
y--;
}
// check for right diagonal
x = row;
y = col;
while(x>=0 && y<n)
{
if(board[x][y] == 1)
{
return false;
}
x--;
y++;
}
// The position is now safe, after checking with the columns & both diagnols
return true;
}
// function to place N-queens on NxN board
bool solveNQueens(char board[][20], int row, int n)
{
// Base Case
if(row == n)
{
// you have successfully placed queens in n rows (0...n-1);
count ++;
// print the board
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(board[i][j] == 1)
{
cout << "Q ";
}
else
{
cout << "_ ";
}
}
cout << endl;
}
cout << endl;
return false; // when finding all ways in which N-queen can be placed on board
// return true; // when finding single way in which N -queen can be placed on board
}
// Recursive Case
// try to place the queen on current row and call on the remaining part
for(int col=0; col<n; col++)
{
// check if (row,col)th position is safe to place the queen or not
if(isSafe(board, row, col, n))
{
// place the queen - assuming row,col is the right position
board[row][col] = 1;
bool didNextQueenGotPlacedOnRemainingBoard = solveNQueens(board, row+1, n);
if(didNextQueenGotPlacedOnRemainingBoard)
{
return true;
}
// when (row,col) is not the right position i.e above assumption is wrong
board[row][col] = 0; // backtrack
}
}
// when tried for all position in the current row but couldn't place a queen
return false;
}
// function to drive code
int main()
{
int n;
cout << "Enter Number of Queens: ";
cin >> n;
char board[20][20] = {0};
solveNQueens(board, 0, n);
cout << "Total number of ways to place N-Queens: " << count;
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter Number of Queens: 4
_ Q _ _
_ _ _ Q
Q _ _ _
_ _ Q _
_ _ Q _
Q _ _ _
_ _ _ Q
_ Q _ _
Total number of ways to place N-Queens: 2
Case 2:
Enter Number of Queens: 3
Total number of ways to place N-Queens: 0
Case 3:
Enter Number of Queens: 1
Q
Total number of ways to place N-Queens: 1
*/