Submission #37495152


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> A[i][j];
    }
  }
  vector<vector<vector<int>>> dp(H + 1, vector<vector<int>>(2, vector<int>(2, INF)));
  dp[0][0][0] = 0;
  dp[0][0][1] = 1;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < 2; j++){
      for (int k = 0; k < 2; k++){
        for (int l = 0; l < 2; l++){
          bool ok = true;
          for (int y = 0; y < W; y++){
            bool ok2 = false;
            if (i > 0){
              if ((A[i][y] ^ k) == (A[i - 1][y] ^ j)){
                ok2 = true;
              }
            }
            if (i < H - 1){
              if ((A[i][y] ^ k) == (A[i + 1][y] ^ l)){
                ok2 = true;
              }
            }
            if (y > 0){
              if ((A[i][y] ^ k) == (A[i][y - 1] ^ k)){
                ok2 = true;
              }
            }
            if (y < W - 1){
              if ((A[i][y] ^ k) == (A[i][y + 1] ^ k)){
                ok2 = true;
              }
            }
            if (!ok2){
              ok = false;
            }
          }
          if (ok){
            dp[i + 1][k][l] = min(dp[i + 1][k][l], dp[i][j][k] + l);
          }
        }
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      ans = min(ans, dp[H][i][j]);
    }
  }
  if (ans == INF){
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

Submission Info

Submission Time
Task E - Don't Isolate Elements
User SSRS
Language C++ (GCC 9.2.1)
Score 500
Code Size 1641 Byte
Status AC
Exec Time 142 ms
Memory 7420 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 30
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All example0.txt, example1.txt, example2.txt, hand0.txt, hand1.txt, hand2.txt, hand3.txt, random0.txt, random1.txt, random10.txt, random11.txt, random12.txt, random13.txt, random14.txt, random15.txt, random16.txt, random17.txt, random18.txt, random19.txt, random2.txt, random20.txt, random21.txt, random22.txt, random3.txt, random4.txt, random5.txt, random6.txt, random7.txt, random8.txt, random9.txt
Case Name Status Exec Time Memory
example0.txt AC 15 ms 3388 KiB
example1.txt AC 6 ms 3536 KiB
example2.txt AC 2 ms 3500 KiB
hand0.txt AC 2 ms 3472 KiB
hand1.txt AC 3 ms 3472 KiB
hand2.txt AC 2 ms 3580 KiB
hand3.txt AC 124 ms 7140 KiB
random0.txt AC 124 ms 7116 KiB
random1.txt AC 126 ms 7120 KiB
random10.txt AC 130 ms 7420 KiB
random11.txt AC 135 ms 7336 KiB
random12.txt AC 131 ms 7196 KiB
random13.txt AC 135 ms 7088 KiB
random14.txt AC 142 ms 7144 KiB
random15.txt AC 137 ms 7236 KiB
random16.txt AC 139 ms 7308 KiB
random17.txt AC 135 ms 7096 KiB
random18.txt AC 131 ms 7044 KiB
random19.txt AC 135 ms 7208 KiB
random2.txt AC 124 ms 7052 KiB
random20.txt AC 130 ms 7020 KiB
random21.txt AC 135 ms 7120 KiB
random22.txt AC 134 ms 7136 KiB
random3.txt AC 133 ms 7148 KiB
random4.txt AC 132 ms 7396 KiB
random5.txt AC 133 ms 7280 KiB
random6.txt AC 131 ms 7328 KiB
random7.txt AC 128 ms 7276 KiB
random8.txt AC 130 ms 7232 KiB
random9.txt AC 126 ms 7096 KiB