Alex_tz307

Domino Fundamente XI - 207

Sep 20th, 2020 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define x first
  3. #define y second
  4. #define pi pair < int , int >
  5. #define pb pair < bool , bool >
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(nullptr);
  12.     cout.tie(nullptr);
  13.     int n;
  14.     cin >> n;
  15.     vector < pi > a(n + 1);
  16.     for(int i = 1; i <= n; ++i)
  17.         cin >> a[i].x >> a[i].y;
  18.     vector < pb > dp(n + 1); // dp[i].x - daca se poate obtine un sir pana la i cu i nerotita
  19.                              // dp[i].y - daca se poate obtine un sir pana la i cu i rotita
  20.     vector < pb > t(n + 1); // t[i].x = 0(1) <=> (i - 1) e nerotita(rotita) (i e NEROTITA)
  21.                             // t[i].y = 0(1) <=> (i - 1) e nerotita(rotita) (i e ROTITA)
  22.     dp[1].x = dp[1].y = true;
  23.     for(int i = 2; i <= n; ++i) {
  24.         if((a[i].x == a[i - 1].y || a[i].x + a[i - 1].y == 6) && dp[i - 1].x)
  25.             dp[i].x = true; // i si (i - 1) nerotite
  26.         if((a[i].x == a[i - 1].x || a[i].x + a[i - 1].x == 6) && dp[i - 1].y) {
  27.             dp[i].x = true; // i nerotita si (i - 1) rotita
  28.             t[i].x = true;
  29.         }
  30.         if((a[i].y == a[i - 1].y || a[i].y + a[i - 1].y == 6) && dp[i - 1].x)
  31.             dp[i].y = true; // i rotita si (i - 1) nerotita
  32.         if((a[i].y == a[i - 1].x || a[i].y + a[i - 1].x == 6) && dp[i - 1].y) {
  33.             dp[i].y = true; // i si (i - 1) rotite
  34.             t[i].y = true;
  35.         }
  36.     }
  37.     if(!dp[n].x && !dp[n].y)
  38.         cout << "Imposibil\n";
  39.     else {
  40.         int i = n, j = dp[n].x ^ 1;
  41.         vector < pi > sol;
  42.         while(i > 0) {
  43.             if(j == 1)
  44.                 sol.emplace_back(a[i].x, a[i].y);
  45.             else
  46.                 sol.emplace_back(a[i].y, a[i].x);
  47.             if(j == 0)
  48.                 j = t[i].x;
  49.             else
  50.                 j = t[i].y;
  51.             --i;
  52.         }
  53.         reverse(sol.begin(), sol.end());
  54.         for(auto it : sol)
  55.             cout << it.y << ' ' << it.x << ' ';
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment