done 3 sorting problems
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
// Chessboard and Queens
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool is_not_attacking(vector<pair<int, int>> &p, pair<int, int> n) {
|
||||
for (auto &[x, y]: p) {
|
||||
if (x == n.first || y == n.second) return false;
|
||||
if (abs(x - n.first) == abs(y - n.second)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int queens(vector<string> &input, int row, vector<pair<int, int>> &placed) {
|
||||
if (row == input.size()) return 1;
|
||||
int possibilities = 0;
|
||||
for (int i{}; i < 8; i++) {
|
||||
if (input[row][i] != '*' && is_not_attacking(placed, {i, row})) {
|
||||
placed.push_back({i, row});
|
||||
possibilities += queens(input, row + 1, placed);
|
||||
placed.pop_back();
|
||||
}
|
||||
}
|
||||
return possibilities;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<string> input;
|
||||
for (int i{}; i < 8; i++) {
|
||||
string line;
|
||||
cin >> line;
|
||||
input.push_back(line);
|
||||
}
|
||||
vector<pair<int, int>> placed;
|
||||
cout << queens(input, 0, placed) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user