Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m, s;
  6. char a[20][20][3], suita[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  7. char suitb[] = {'C', 'D', 'H', 'S'}, x[2], y[2];
  8. bool seen[1000];
  9.  
  10. struct Poz
  11. {
  12.     int lin, col;
  13. } joker1, joker2, sq[100000], c1, c2, c3, c4;
  14.  
  15. unordered_map < string, bool > check;
  16.  
  17. bool Verif(int lin, int col)
  18. {
  19.     int i, j;
  20.     int ok = 1;
  21.  
  22.     for(i = lin ; i <= lin + 2 && ok == 1 ; i++)
  23.         for(j = col ; j <= col + 2 && ok == 1 ; j++)
  24.             if(a[lin][col][1] != a[i][j][1])
  25.                 ok = 0;
  26.  
  27.     if(ok == 1)
  28.         return true;
  29.  
  30.     for(i = lin ; i <= lin + 2 ; i++)
  31.         for(j = col ; j <= col + 2 ; j++)
  32.             if(seen[ a[i][j][0] ]==1)
  33.                 return false;
  34.             else
  35.                 seen[ a[i][j][0] ] = 1;
  36.  
  37.     return true;
  38. }
  39.  
  40. int main()
  41. {
  42.     int i, j, i1, j1, i2, j2;
  43.  
  44.     cin >> n >> m;
  45.  
  46.     for(i = 1 ; i <= n ; i++)
  47.         for(j = 1 ; j <= m ; j++)
  48.         {
  49.             cin >> a[i][j];
  50.  
  51.             if(a[i][j][0] == 'J')
  52.             {
  53.                 if(joker1.lin == 0)
  54.                     joker1 = {i, j};
  55.                 else
  56.                     joker2 = {i, j};
  57.             }
  58.             else
  59.                 check[ a[i][j] ] = 1;
  60.         }
  61.  
  62.     if(joker1.lin == 0)
  63.     {
  64.         for(i = 1 ; i <= n - 2 ; i++)
  65.             for(j = 1 ; j <= m - 2 ; j++)
  66.             {
  67.                 memset(seen, 0, sizeof(seen));
  68.  
  69.                 if(Verif(i, j) == true)
  70.                     sq[++s] = {i, j};
  71.             }
  72.  
  73.         for(i = 1 ; i < s ; i++)
  74.             for(j = i + 1 ; j <= s ; j++)
  75.             {
  76.                 if(abs(sq[j].lin - sq[i].lin) >= 2 || abs(sq[j].col - sq[i].col) >= 2)
  77.                 {
  78.                     cout << "Solution exists." << '\n';
  79.                     cout << "There are no jokers." << '\n';
  80.                     cout << "Put the first square to (" << sq[i].lin << ", " << sq[i].col << ")." << '\n';
  81.                     cout << "Put the second square to (" << sq[j].lin << ", " << sq[j].col << ")." << '\n';
  82.  
  83.                     return 0;
  84.                 }
  85.             }
  86.  
  87.         cout << "No solution.";
  88.  
  89.         return 0;
  90.     }
  91.  
  92.     if(joker2.lin == 0)
  93.     {
  94.         int sz = strlen(suita), sz2 = 4;
  95.  
  96.         for(i = 0 ; i < sz ; i++)
  97.         {
  98.             for(j = 0 ; j < sz2 ; j++)
  99.             {
  100.                 x[0] = suita[i];
  101.                 x[1] = suitb[j];
  102.  
  103.                 if(check[x] == 0)
  104.                 {
  105.                     strcpy(a[joker1.lin][joker1.col], x);
  106.  
  107.                     s = 0;
  108.  
  109.                     for(i1 = 1 ; i1 <= n - 2 ; i1++)
  110.                         for(j1 = 1 ; j1 <= m - 2 ; j1++)
  111.                         {
  112.                             memset(seen, 0, sizeof(seen));
  113.  
  114.                             if(Verif(i1, j1) == true)
  115.                                 sq[++s] = {i1, j1};
  116.                         }
  117.  
  118.                     for(i1 = 1 ; i1 < s ; i1++)
  119.                         for(j1 = i1 + 1 ; j1 <= s ; j1++)
  120.                         {
  121.                             if(abs(sq[j1].lin - sq[i1].lin) >= 2 || abs(sq[j1].col - sq[i1].col)  >= 2)
  122.                             {
  123.                                 cout << "Solution exists." << '\n';
  124.                                 cout << "Replace J1 with " << x << '.' << '\n';
  125.                                 cout << "Put the first square to (" << sq[i1].lin << ", " << sq[i1].col << ")." << '\n';
  126.                                 cout << "Put the second square to (" << sq[j1].lin << ", " << sq[j1].col << ")." << '\n';
  127.  
  128.                                 return 0;
  129.                             }
  130.                         }
  131.                 }
  132.             }
  133.         }
  134.  
  135.         cout << "No solution";
  136.         return 0;
  137.     }
  138.  
  139.  
  140.     int sz = strlen(suita), sz2 = 4;
  141.  
  142.     for(i = 0 ; i < sz ; i++)
  143.     {
  144.         for(j = 0 ; j < sz2 ; j++)
  145.         {
  146.             x[0] = suita[i];
  147.             x[1] = suitb[j];
  148.  
  149.             if(check[x] == 0)
  150.             {
  151.                 check[x] = 1;
  152.                 strcpy(a[joker1.lin][joker1.col], x);
  153.  
  154.                 for(i2 = 0 ; i2 < sz ; i2++)
  155.                     for(j2 = 0 ; j2 < sz2 ; j2++)
  156.                     {
  157.                         y[0] = suita[i2];
  158.                         y[1] = suitb[j2];
  159.  
  160.                         if(check[y] == 0)
  161.                         {
  162.                             s = 0;
  163.                             strcpy(a[joker2.lin][joker2.col], y);
  164.  
  165.                             for(i1 = 1 ; i1 <= n - 2 ; i1++)
  166.                                 for(j1 = 1 ; j1 <= m - 2 ; j1++)
  167.                                 {
  168.                                     memset(seen, 0, sizeof(seen));
  169.  
  170.                                     if(Verif(i1, j1) == true)
  171.                                         sq[++s] = {i1, j1};
  172.                                 }
  173.  
  174.                             for(i1 = 1 ; i1 < s ; i1++)
  175.                                 for(j1 = i1 + 1 ; j1 <= s ; j1++)
  176.                                 {
  177.                                     if(abs(sq[j1].lin - sq[i1].lin) >= 2 || abs(sq[j1].col - sq[i1].col) >= 2)
  178.                                     {
  179.                                         cout << "Solution exists." << '\n';
  180.                                         cout << "Replace J1 with " << x[0] << x[1];
  181.                                         cout << " and J2 with " << y[0] << y[1] << '.' << '\n';
  182.                                         cout << "Put the first square to (" << sq[i1].lin << ", " << sq[i1].col << ")." << '\n';
  183.                                         cout << "Put the second square to (" << sq[j1].lin << ", " << sq[j1].col << ")." << '\n';
  184.  
  185.                                         return 0;
  186.                                     }
  187.                                 }
  188.                         }
  189.                     }
  190.  
  191.  
  192.                 check[x] = 0;
  193.             }
  194.         }
  195.     }
  196.  
  197.     cout << "No solution";
  198.  
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement