Advertisement
BaoJIaoPisu

Untitled

Jun 21st, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33.     bool minimize(X &x, const Y &y) {
  34.         if (x > y)
  35.         {
  36.             x = y;
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. template<class X, class Y>
  42.     bool maximize(X &x, const Y &y) {
  43.         if (x < y)
  44.         {
  45.             x = y;
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54.     void add(X &x, const Y &y) {
  55.         x = (x + y);
  56.         if(x >= MOD) x -= MOD;
  57.     }
  58.  
  59. template<class X, class Y>
  60.     void sub(X &x, const Y &y) {
  61.         x = (x - y);
  62.         if(x < 0) x += MOD;
  63.     }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e9;
  68. const int N = 1e5 + 10;
  69.  
  70. int a[30][30], b[30][30], state[30];
  71. int dp[(1 << 22) + 10];
  72.  
  73. void solve() {
  74.     int n, m;
  75.     cin >> n >> m;
  76.     for(int i = 1; i <= n; i++) {
  77.         for(int j = 1; j <= m; j++) {
  78.             char c; cin >> c;
  79.             a[i][j] = (c == 'X' ? 1 : 0);
  80.         }
  81.     }
  82.  
  83.     auto turn = [&](int i, int j) -> void {
  84.         b[i][j] ^= 1;
  85.         if(i > 1) b[i - 1][j] ^= 1;
  86.         if(j > 1) b[i][j - 1] ^= 1;
  87.         if(i < n) b[i + 1][j] ^= 1;
  88.         if(j < m) b[i][j + 1] ^= 1;
  89.     };
  90.  
  91.     auto calc = [&](int msk) -> pii {
  92.         for(int i = 1; i <= n; i++) {
  93.             for(int j = 1; j <= m; j++) b[i][j] = a[i][j];
  94.         }  
  95.  
  96.         int steps = 0;
  97.         for(int i = 0; i < n; i++) {
  98.             if(msk >> i & 1) {
  99.                 turn(i + 1, 1);
  100.                 steps++;
  101.             }
  102.         }
  103.  
  104.         for(int j = 2; j <= m; j++) {
  105.             for(int i = 1; i <= n; i++) {
  106.                 if(b[i][j - 1]) {
  107.                     turn(i, j);
  108.                     steps++;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         int ans = 0;
  114.         for(int i = 1; i <= n; i++) {
  115.             if(b[i][m]) ans |= (1 << (i - 1));
  116.         }
  117.  
  118.         return mp(ans, steps);
  119.     };
  120.  
  121.     int ans = INF;
  122.     pii g = calc(0);
  123.     dp[0] = g.fi;
  124.     if(g.fi == 0) minimize(ans, g.se);
  125.  
  126.     for(int i = 0; i < n; i++) {
  127.         dp[1 << i] = calc(1 << i).fi;
  128.         state[i] = dp[1 << i] ^ dp[0];
  129.     }
  130.  
  131.     for(int msk = 1; msk < (1 << n); msk++) {
  132.         for(int i = 0; i < n; i++) {
  133.             if(msk >> i & 1) {
  134.                 dp[msk] = dp[msk ^ (1 << i)] ^ state[i];
  135.                 break;
  136.             }
  137.         }
  138.  
  139.         if(dp[msk] == 0) minimize(ans, calc(msk).se);
  140.     }
  141.  
  142.     if(ans == INF) {
  143.         cout << "Damaged billboard.";
  144.         return;
  145.     }
  146.  
  147.     cout << "You have to tap " << ans << " tiles.";
  148. }
  149.  
  150. int main()
  151. {
  152.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  153.     #ifndef ONLINE_JUDGE
  154.     freopen("input.txt", "r", stdin);
  155.     freopen("output.txt", "w", stdout);
  156.     #else
  157.     //online
  158.     freopen("billboard.inp", "r", stdin);
  159.     freopen("billboard.out", "w", stdout);
  160.     #endif
  161.  
  162.     int tc = 1, ddd = 0;
  163.     // cin >> tc;
  164.     while(tc--) {
  165.         //ddd++;
  166.         //cout << "Case #" << ddd << ": ";
  167.         solve();
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement