john2323

PERMUTARI backtracking - algoritm

Mar 9th, 2021
515
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("permutari.in");
  5. ofstream fout("permutari.out");
  6. int n, x[11];
  7. int ok(int k) {
  8.   for (int i = 1; i < k; i++)
  9.     if (x[i] == x[k])
  10.       return false;
  11.   return true;
  12.  
  13. }
  14.  
  15. void afisare(int k) {
  16.   for (int i = 1; i <= k; i++)
  17.     fout << x[i] << " ";
  18.   fout << "\n";
  19. }
  20. void Back(int k) {
  21.   for (int i = 1; i <= n; i++) {
  22.     x[k] = i;
  23.     if (ok(k))
  24.       if (k == n)
  25.         afisare(k);
  26.       else
  27.         Back(k + 1);
  28.   }
  29. }
  30.  
  31. int main() {
  32.   fin >> n;
  33.   Back(1);
  34.  
  35. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×