Advertisement
pb_jiang

ABC283E WA

Dec 28th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19.  
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24. int h, w;
  25.  
  26. bool check(const vector<vector<int>> &v, int i, int op1, int op2, int op3)
  27. {
  28.     bool ret = true;
  29.     int dir[3][2] = {{0, 1}, {1, 0}, {-1, 0}};
  30.     for (int j = 0; j < w && ret; ++j) {
  31.         bool jflag = false;
  32.         for (int p = 0; p < 3; ++p) {
  33.             int nx = i + dir[p][0], ny = j + dir[p][1];
  34.             if (nx >= 0 && nx < w && ny >= 0 && ny <= h) {
  35.                 if ((v[i][j] ^ op1) == (v[nx][ny] ^ (ny == i + 1 ? op2 : op1))) {
  36.                     jflag = true;
  37.                 }
  38.             }
  39.         }
  40.         if ((v[i][j] ^ op1) == (v[i - 1][j] ^ op3))
  41.             jflag = true;
  42.         ret = ret & jflag;
  43.     }
  44.     return ret;
  45. }
  46.  
  47. int main(int argc, char **argv)
  48. {
  49.     cin >> h >> w;
  50.     auto a = vv<int>(h, w);
  51.     for (auto &row : a)
  52.         for (auto &col : row)
  53.             cin >> col;
  54.  
  55.     a.insert(a.begin(), vi(w, -1));
  56.     a.push_back(vi(w, -1));
  57.  
  58.     // the minimum operations needed to have i-th row legit
  59.     // j: flip flag on i-th row
  60.     // k: flip flag on (i+1)-th row
  61.     // dp[i][j][k];
  62.  
  63.     vector<array<int, 4>> dp(h + 2, array<int, 4>{INT_MAX / 2, INT_MAX / 2, INT_MAX / 2, INT_MAX / 2});
  64.     dp[0][0] = 0;
  65.     dp[0][1] = 1;
  66.     dp[0][2] = 1;
  67.     dp[0][3] = 2;
  68.  
  69.     for (int i = 1; i <= h; ++i) {
  70.         for (int s = 0; s < 4; ++s) {
  71.             int op1 = s / 2;
  72.             int op2 = s % 2;
  73.             for (int op3 = 0; op3 <= 1; ++op3) {
  74.                 if (check(a, i, op2, op3, op1))
  75.                     dp[i][op2 * 2 + op3] = min(dp[i][op2 * 2 + op3], dp[i - 1][op1 * 2 + op2] + op3);
  76.             }
  77.         }
  78.     }
  79.     int min_val = min({dp[h][0], dp[h][1], dp[h][2], dp[h][3]});
  80.     cout << (min_val == INT_MAX / 2 ? -1 : min_val) << endl;
  81.     return 0;
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement