Advertisement
Guest User

Untitled

a guest
May 24th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void escriure_sol (int n, const vector<char>& sol) {
  6.  
  7. for (int i = 0; i < n; ++i) {
  8. cout << sol[i];
  9. }
  10. cout << endl;
  11. }
  12.  
  13. void backtrack (int n, vector<char> &sol, int i, vector<bool> &bol) {
  14.  
  15. if (i == n) escriure_sol(n,sol);
  16. else {
  17. int j = 0;
  18. while (j < n) {
  19. if (not bol[j] and ((i > 0 and sol[i-1] != 'a' + j-1) or i == 0)) {
  20. sol[i] = 'a' + j;
  21. bol[j] = true;
  22. backtrack(n,sol,i+1,bol);
  23. bol[j] = false;
  24. }
  25. ++j;
  26. }
  27. }
  28. }
  29.  
  30. int main() {
  31.  
  32. int n;
  33. cin >> n;
  34. vector<char> sol(n);
  35. vector<bool> bol(n,false);
  36. backtrack(n,sol,0,bol);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement