Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void escriure_sol (int n, const vector<char>& sol) {
- for (int i = 0; i < n; ++i) {
- cout << sol[i];
- }
- cout << endl;
- }
- void backtrack (int n, vector<char> &sol, int i, vector<bool> &bol) {
- if (i == n) escriure_sol(n,sol);
- else {
- int j = 0;
- while (j < n) {
- if (not bol[j] and ((i > 0 and sol[i-1] != 'a' + j-1) or i == 0)) {
- sol[i] = 'a' + j;
- bol[j] = true;
- backtrack(n,sol,i+1,bol);
- bol[j] = false;
- }
- ++j;
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- vector<char> sol(n);
- vector<bool> bol(n,false);
- backtrack(n,sol,0,bol);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement