-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueens_2.cpp
48 lines (42 loc) · 1.21 KB
/
queens_2.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
#include <iostream>
#include <vector>
#include <string>
/* A utility function to print solution */
void print_solution(std::vector<std::vector<int>> &board, int num_queens)
{
static int k = 1;
printf("%d-\n",k++);
for (int i = 0; i < num_queens; i++)
{
for (int j = 0; j < num_queens; j++)
if(board[i][j] == 1)
printf(" Q ");
else
printf(" . ");
printf("\n");
}
printf("\n");
}
bool is_safe(std::vector<std::vector<int>> &board, int row, int current_iteration, int num_queens){
}
bool backtrack(std::vector<std::vector<int>> &board, int current_iteration, int num_queens){
if(current_iteration == num_queens){
print_solution(board, num_queens)
}
bool result = false;
for(int i = 0; i < num_queens; i++){
if(is_safe(board,i, current_iteration, num_queens)){
board[i][current_iteration] = 1;
result = is_safe(board,i, current_iteration, num_queens) || result;
board[i][current_iteration] = 0;
}
}
return result;
}
int main()
{
int n = 8;
std::vector<std::vector<int>> board(n, std::vector<int>(n, 0));
backtrack(board, 0, n);
return 0;
}