Advertisement
STANAANDREY

pb4 2/11/2020

Nov 2nd, 2020 (edited)
2,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. using namespace std;
  4. ofstream fout("matrice.out");
  5. int st[20], n;
  6.  
  7. void tipar(int k) {
  8.     for (int i = 1; i <= k; i++) {
  9.         for (int j = 1; j <= n; j++)
  10.             if (j == st[i])
  11.                 fout << 1 << ' ';
  12.             else
  13.                 fout << 0 << ' ';
  14.         fout << endl;
  15.     }
  16.     fout << endl;
  17. }
  18.  
  19. int valid(int k) {
  20.     for (int i = 1; i < k; i++)
  21.         if (st[i] == st[k])
  22.             return 0;
  23.     return 1;
  24. }
  25.  
  26. int sol(int k) {
  27.     return k == n;
  28. }
  29.  
  30. void bktr() {
  31.     int k = 1;
  32.     st[k] = 0;
  33.     while (k) {
  34.         if (st[k] < n) {
  35.             st[k]++;
  36.             if (valid(k)) {
  37.                 if (sol(k))
  38.                     tipar(k);
  39.                 else {
  40.                     k++;
  41.                     st[k] = 0;
  42.                 }
  43.             }
  44.         }
  45.         else
  46.             k--;
  47.     }
  48. }
  49.  
  50. int main() {
  51.     cin >> n;
  52.     bktr();
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement