-
Notifications
You must be signed in to change notification settings - Fork 4
/
NQueen.cpp
86 lines (74 loc) · 2.11 KB
/
NQueen.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
/*--------------------------"SABUJ-JANA"------"JADAVPUR UNIVERSITY"--------*/
/*-------------------------------@greenindia-----------------------------------*/
//
// _____ _ _ _
// / ____| | | (_) | |
// | (___ __ _| |__ _ _ _ | | __ _ _ __ __ _
// \___ \ / _` | '_ \| | | | | _ | |/ _` | '_ \ / _` |
// ____) | (_| | |_) | |_| | | | |__| | (_| | | | | (_| |
// |_____/ \__,_|_.__/ \__,_| | \____/ \__,_|_| |_|\__,_|
// _/ |
// |__/
/*---------------------- Magic. Do not touch.-----------------------------*/
/*------------------------------God is Great/\---------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define crap ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
//cout<<fixed<<showpoint<<setprecision(12)<<ans<<endl;
#define int long long int
#define double long double
#define PI acos(-1)
void print1d(const vector<int>& vec) {for (auto val : vec) {cout << val << " ";} cout << endl;}
void print2d(const vector<vector<int>>& vec) {for (auto row : vec) {for (auto val : row) {cout << val << " ";} cout << endl;}}
const int N = 4;
vector<vector<int>> board(N, vector<int>(N, 0));
int cnt = 0;
bool isValid(int n, int row, int col) {
//Col up
for (int i = row - 1; i >= 0; i--) {
if (board[i][col] == 1)
return false;
}
// Left up diagonal
for (int i = row - 1, j = col - 1; i >= 0 and j >= 0; i--, j--) {
if (board[i][j] == 1)
return false;
}
// Right up diagonal
for (int i = row - 1, j = col + 1; i >= 0 and j < n; i--, j++) {
if (board[i][j] == 1)
return false;
}
return true;
}
void helper(int n, int row) {
if (row == n) {
// reached the base
print2d(board);
cnt++;
return;
}
for (int j = 0; j < n; j++) {
if (isValid(n, row, j)) {
board[row][j] = 1;
helper(n, row + 1);
board[row][j] = 0;
}
}
return;
}
void nQueen(int n) {
helper(n, 0);
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
#endif
crap;
int n = 4;
nQueen(n);
cout << cnt << endl;
return 0;
}