Advertisement
haser__

Untitled

Jul 16th, 2021
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <random>
  7. using namespace  std;
  8. #define int long long
  9. #define pb push_back
  10. #define sqr(x) (x)*(x)
  11. #define IOS ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
  12. #define amas(a) for (auto&c:a) cout<<c<<" ";
  13. #define double long double
  14. int ma[10][10];
  15. int ans[10][10];
  16. int n, m, c;
  17. //int dp[3][10];//цвет.одинакового цвета в строке (уже выбранных)
  18. //int dp2[3][10];// одинакового цвета в столбце(уже выбранных)
  19. bool allf = false;
  20. void rec(int i, int j) {
  21.     if (i == n) {
  22.         for (int i1 = 0; i1 < n; i1++) {
  23.             for (int j1 = 0; j1 < m; j1++) cout << ma[i1][j1] << " ";
  24.             cout << "\n";
  25.         }
  26.         //char c;
  27.         //cin >> c;
  28.         //ans = ma;
  29.         allf = true;
  30.     }
  31.     else {
  32.         bool ex = true;
  33.         int kus = 0;
  34.         vector<int>ch(4);
  35.         while (1) {
  36.             if (kus == 3) break;
  37.             int g = rand() % c + 1;
  38.             while (ch[g]) {
  39.                 g = rand() % c + 1;
  40.             }
  41.             ch[g] = 1; kus++;
  42.             // выбираем цвет
  43.             bool f = true;
  44.             for (int k = j - 1; k > -1; k--) {
  45.                 if (ma[i][k] == g) {
  46.                     // ещё 1 сторона
  47.                     for (int k1 = i - 1; k1 > -1; k1--) {
  48.                         if (ma[k1][k] == g) {
  49.                             if (ma[k1][j] == g) f = false;
  50.                         }
  51.                         if (!f) break;
  52.                     }
  53.                     if (!f) break;
  54.                 }
  55.                 if (!f) break;
  56.             }
  57.             if (f) {
  58.                 ex = false;
  59.                 ma[i][j] = g;
  60.                 if (j + 1 == m) rec(i + 1, 0);
  61.                 else rec(i, j + 1);
  62.                 ma[i][j] = 0;
  63.             }
  64.             if (allf) return;
  65.         }
  66.         if (ex) return;
  67.         // проверка
  68.     }// o(n*m*c-выбор)*c^(n*m) (найс скорость)
  69. }
  70. void rec1(int i, int j) {
  71.     if (i == n) {
  72.         for (int i1 = 0; i1 < n; i1++) {
  73.             for (int j1 = 0; j1 < m; j1++) cout << ma[i1][j1] << " ";
  74.             cout << "\n";
  75.         }
  76.         //char c;
  77.         //cin >> c;
  78.         //ans = ma;
  79.         allf = true;
  80.     }
  81.     else {
  82.         bool ex = true;
  83.         for (int g = 1; g <= c; g++) {
  84.             // выбираем цвет
  85.             bool f = true;
  86.             for (int k = j - 1; k > -1; k--) {
  87.                 if (ma[i][k] == g) {
  88.                     // ещё 1 сторона
  89.                     for (int k1 = i - 1; k1 > -1; k1--) {
  90.                         if (ma[k1][k] == g) {
  91.                             if (ma[k1][j] == g) f = false;
  92.                         }
  93.                         if (!f) break;
  94.                     }
  95.                     if (!f) break;
  96.                 }
  97.                 if (!f) break;
  98.             }
  99.             if (f) {
  100.                 ex = false;
  101.                 ma[i][j] = g;
  102.                 if (j + 1 == m) rec1(i + 1, 0);
  103.                 else rec1(i, j + 1);
  104.                 ma[i][j] = 0;
  105.             }
  106.             if (allf) return;
  107.         }
  108.         if (ex) return;
  109.         // проверка
  110.     }// o(n*m*c-выбор)*c^(n*m) (найс скорость)
  111. }
  112. int32_t main()
  113. {
  114.     IOS;
  115.     mt19937 rand;
  116.    // rand.seed(std::time(nullptr));
  117.     //for (int i1 = 2; i1 < 10; i1++) {
  118.         //for (int j1 = 2; j1 < 10; j1++) {
  119.             //n = i1;
  120.         //  m = i1;
  121.         //  c = 3;
  122.     cin >> n >> m >> c;
  123.     for (int i = 0; i < 10; i++) {
  124.         for (int j = 0; j < 10; j++) ma[i][j] = 0;
  125.     }
  126.     //cin >> n >> m >> c;
  127.     // создадим все подходящие (посмотреть)
  128.     if (c==3)rec(0, 0);
  129.     else rec1(0, 0);
  130.     //  for (int i = 0; i < n; i++) {
  131.     //      for (int j = 0; j < m; j++) cout << ans[i][j] << " ";
  132.     //      cout << "\n";
  133.     //  }
  134.         //char c;
  135.     //  cin >> c;
  136. //  }
  137. //}
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement