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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
inline int move_x(int x, int y, int r) {
  if (y % 2 == 0) {
    int dx[6] = {-1, 0, 1, 1, 1, 0};
    return x + dx[r];
  } else {
    int dx[6] = {-1, -1, 0, 1, 0, -1};
    return x + dx[r];
  }
}
inline int move_y(int x, int y, int r) {
  int dy[6] = {0, 1, 1, 0, -1, -1};
  return y + dy[r];
}

int main() {
  int W, H;
  while (cin >> W >> H && W) {
    int grid[100][100] = {};
    REP(y, H) REP(x, W) scanf("%d", &grid[y][x]);
    bool searched[100][100] = {};
    bool env[100][100] = {};
    REP(sy, H) REP(sx, W) if (!searched[sy][sx] && grid[sy][sx] == 0) {
      queue<int> qx, qy;
      vector<int> vx, vy;
      qx.push(sx);
      qy.push(sy);
      searched[sy][sx] = true;
      bool is_env = false;
      while (!qx.empty()) {
        int x = qx.front(), y = qy.front();
        qx.pop();
        qy.pop();
        vx.push_back(x), vy.push_back(y);
        if (x == 0 || x == W - 1 || y == 0 || y == H - 1)
          is_env = true;
        REP(r, 6) {
          int nx = move_x(x, y, r);
          int ny = move_y(x, y, r);
          if (nx >= 0 && nx < W && ny >= 0 && ny < H && grid[ny][nx] == 0 &&
              !searched[ny][nx]) {
            searched[ny][nx] = true;
            qx.push(nx);
            qy.push(ny);
          }
        }
      }
      if (is_env) {
        REP(i, vx.size()) env[vy[i]][vx[i]] = true;
      }
    }
    int ans = 0;
    REP(y, H) REP(x, W) if (grid[y][x]) {
      REP(r, 6) {
        int nx = move_x(x, y, r);
        int ny = move_y(x, y, r);
        if (!(nx >= 0 && nx < W && ny >= 0 && ny < H) || (env[ny][nx])) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
inline int move_x(int x, int y, int r) {
  if (y % 2 == 0) {
    int dx[6] = {-1, 0, 1, 1, 1, 0};
    return x + dx[r];
  } else {
    int dx[6] = {-1, -1, 0, 1, 0, -1};
    return x + dx[r];
  }
}
inline int move_y(int x, int y, int r) {
  int dy[6] = {0, 1, 1, 0, -1, -1};
  return y + dy[r];
}

int main() {
  int W, H;
  while (cin >> W >> H && W) {
    int grid[100][100] = {};
    REP(y, H) REP(x, W) scanf("%d", &grid[y][x]);
    bool searched[100][100] = {};
    bool env[100][100] = {};
    REP(sy, H) REP(sx, W) if (!searched[sy][sx] && grid[sy][sx] == 0) {
      queue<int> qx, qy;
      vector<int> vx, vy;
      qx.push(sx);
      qy.push(sy);
      bool is_env = false;
      while (!qx.empty()) {
        int x = qx.front(), y = qy.front();
        qx.pop();
        qy.pop();
        searched[y][x] = true;
        vx.push_back(x), vy.push_back(y);
        if (x == 0 || x == W - 1 || y == 0 || y == H - 1)
          is_env = true;
        REP(r, 6) {
          int nx = move_x(x, y, r);
          int ny = move_y(x, y, r);
          if (nx >= 0 && nx < W && ny >= 0 && ny < H && grid[ny][nx] == 0 &&
              !searched[ny][nx]) {
            qx.push(nx);
            qy.push(ny);
          }
        }
      }
      if (is_env) {
        REP(i, vx.size()) env[vy[i]][vx[i]] = true;
      }
    }
    int ans = 0;
    REP(y, H) REP(x, W) if (grid[y][x]) {
      REP(r, 6) {
        int nx = move_x(x, y, r);
        int ny = move_y(x, y, r);
        if (!(nx >= 0 && nx < W && ny >= 0 && ny < H) || (env[ny][nx])) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}