Problem p00492 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int X, Y;
  int ans = 0;
  cin >> X >> Y;
  int a[Y + 2][X + 2];

  for (int i = 0; i < Y + 2; i++) {
    for (int j = 0; j < X + 2; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 1; i <= Y; i++) {
    for (int j = 1; j <= X; j++) {
      cin >> a[i][j];
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  while (q.empty() == false) {
    if (a[q.front().first][q.front().second] == 0) {
      if (q.front().first % 2 == 0) {
        if (q.front().first - 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second - 1));
        }
        if (q.front().first - 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second));
        }
        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X &&
            a[q.front().first][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second - 1));
        }
        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second + 1));
        }
        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second - 1));
        }
        if (q.front().first + 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second));
        }
        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 1) {
          ans++;
        }
      } else {
        if (q.front().first + 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second + 1));
        }
        if (q.front().first + 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second));
        }
        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second - 1));
        }
        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second + 1));
        }
        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second + 1));
        }
        if (q.front().first - 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second));
        }
        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 1) {
          ans++;
        }
      }
    }
    // cout << endl;
    // for(int i = 0; i < Y + 2; i++) {
    //     if(i % 2 != 0)
    //         cout << " ";
    //     for(int j = 0; j < X + 2; j++) {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << "                             " << ans;
    a[q.front().first][q.front().second] = 2;
    q.pop();
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int X, Y;
  int ans = 0;
  cin >> X >> Y;
  int a[Y + 2][X + 2];
  for (int i = 0; i < X + 2; i++) {
    a[0][i] = 0;
  }
  for (int i = 1; i <= Y; i++) {
    a[i][0] = 0;
    for (int j = 1; j <= X; j++) {
      cin >> a[i][j];
    }
    a[i][X] = 0;
  }
  for (int i = 0; i < X + 2; i++) {
    a[Y + 1][i] = 0;
  }
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  a[0][0] = 2;
  while (q.empty() == false) {
    if (a[q.front().first][q.front().second] == 0) {
      if (q.front().first % 2 == 0) {
        if (q.front().first - 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second - 1));
        }
        if (q.front().first - 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second));
        }
        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X &&
            a[q.front().first][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second - 1));
        }
        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second + 1));
        }
        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second - 1));
        }
        if (q.front().first + 1 >= 0 && q.front().second - 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second));
        }
        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 1) {
          ans++;
        }
      } else {
        if (q.front().first + 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second + 1));
        }
        if (q.front().first + 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first + 1][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second));
        }
        if (q.front().first - 1 >= 0 && q.front().second >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first - 1][q.front().second] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second - 1));
        }
        if (q.front().first >= 0 && q.front().second - 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second - 1 <= X + 1 &&
            a[q.front().first][q.front().second - 1] == 1) {
          ans++;
        }

        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first, q.front().second + 1));
        }
        if (q.front().first >= 0 && q.front().second + 1 >= 0 &&
            q.front().first <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first - 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second + 1] == 0) {
          q.push(make_pair(q.front().first - 1, q.front().second + 1));
        }
        if (q.front().first - 1 >= 0 && q.front().second + 1 >= 0 &&
            q.front().first - 1 <= Y + 1 && q.front().second + 1 <= X + 1 &&
            a[q.front().first - 1][q.front().second + 1] == 1) {
          ans++;
        }

        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 0) {
          q.push(make_pair(q.front().first + 1, q.front().second));
        }
        if (q.front().first + 1 >= 0 && q.front().second >= 0 &&
            q.front().first + 1 <= Y + 1 && q.front().second <= X + 1 &&
            a[q.front().first + 1][q.front().second] == 1) {
          ans++;
        }
      }
      // cout << endl;
      // for(int i = 0; i < Y + 2; i++) {
      //     if(i % 2 != 0)
      //         cout << " ";
      //     for(int j = 0; j < X + 2; j++) {
      //         cout << a[i][j] << " ";
      //     }
      //     cout << endl;
      // }
      // cout << "                             " << ans;
      a[q.front().first][q.front().second] = 2;
      q.pop();
    }
  }
  cout << ans << endl;
  return 0;
}