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

#include <iostream>
#include <queue>

#pragma warning(disable : 4996)

using namespace std;

int H, W, R[100][100];

const int dx0[6] = {0, 1, 1, 1, 0, -1};
const int dx1[6] = {-1, 0, 1, 0, -1, -1};

const int dy[6] = {-1, -1, 0, 1, 1, 0};

int main() {
  scanf("%d", &W);
  scanf("%d", &H);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &R[i][j]);
    }
  }

  int count_ = 3;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (R[i][j] == 0) {
        queue<pair<int, int>> que;
        que.push(make_pair(j, i));

        while (!que.empty()) {
          pair<int, int> now = que.front();
          que.pop();

          int x = now.first;
          int y = now.second;

          if (R[y][x] == 0) {
            R[y][x] = count_;

            for (int dir = 0; dir < 6; dir++) {
              int sx, sy = y + dy[dir];

              if (y % 2 == 0) {
                sx = x + dx0[dir];
              } else {
                sx = x + dx1[dir];
              }

              if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (R[sy][sx] == 0) {
                  que.push(make_pair(sx, sy));
                }
              }
            }
          }
        }

        bool ok = false;

        for (int k = 0; k < H; k++) {
          for (int l = 0; l < W; l++) {
            if (R[k][l] == count_) {
              for (int dir = 0; dir < 6; dir++) {
                int sx, sy = k + dy[dir];

                if (k % 2 == 0) {
                  sx = l + dx0[dir];
                } else {
                  sx = l + dx1[dir];
                }

                if (!(0 <= sx && sx < W && 0 <= sy && sy < H)) {
                  ok = true;
                }
              }
            }
          }
        }

        if (ok) {
          for (int k = 0; k < H; k++) {
            for (int l = 0; l < W; l++) {
              if (R[k][l] == count_) {
                R[k][l] = 2;
              }
            }
          }
        }

        count_++;
      }
    }
  }

  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int dir = 0; dir < 6; dir++) {
        int sx, sy = i + dy[dir];

        if (i % 2 == 0) {
          sx = j + dx0[dir];
        } else {
          sx = j + dx1[dir];
        }

        if (0 <= sx && sx < W && 0 <= sy && sy < H) {
          if (R[i][j] == 2 && R[sy][sx] == 1) {
            ret++;
          }
        } else if (R[i][j] == 1) {
          ret++;
        }
      }
    }
  }

  printf("%d\\n", ret);

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

#pragma warning(disable : 4996)

using namespace std;

int H, W, R[100][100];

const int dx0[6] = {0, 1, 1, 1, 0, -1};
const int dx1[6] = {-1, 0, 1, 0, -1, -1};

const int dy[6] = {-1, -1, 0, 1, 1, 0};

int main() {
  scanf("%d", &W);
  scanf("%d", &H);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &R[i][j]);
    }
  }

  int count_ = 3;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (R[i][j] == 0) {
        queue<pair<int, int>> que;
        que.push(make_pair(j, i));

        while (!que.empty()) {
          pair<int, int> now = que.front();
          que.pop();

          int x = now.first;
          int y = now.second;

          R[y][x] = count_;

          for (int dir = 0; dir < 6; dir++) {
            int sx, sy = y + dy[dir];

            if (y % 2 == 0) {
              sx = x + dx0[dir];
            } else {
              sx = x + dx1[dir];
            }

            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
              if (R[sy][sx] == 0) {
                que.push(make_pair(sx, sy));
              }
            }
          }
        }

        bool ok = false;

        for (int k = 0; k < H; k++) {
          for (int l = 0; l < W; l++) {
            if (R[k][l] == count_) {
              for (int dir = 0; dir < 6; dir++) {
                int sx, sy = k + dy[dir];

                if (k % 2 == 0) {
                  sx = l + dx0[dir];
                } else {
                  sx = l + dx1[dir];
                }

                if (!(0 <= sx && sx < W && 0 <= sy && sy < H)) {
                  ok = true;
                }
              }
            }
          }
        }

        if (ok) {
          for (int k = 0; k < H; k++) {
            for (int l = 0; l < W; l++) {
              if (R[k][l] == count_) {
                R[k][l] = 2;
              }
            }
          }
        }

        count_++;
      }
    }
  }

  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int dir = 0; dir < 6; dir++) {
        int sx, sy = i + dy[dir];

        if (i % 2 == 0) {
          sx = j + dx0[dir];
        } else {
          sx = j + dx1[dir];
        }

        if (0 <= sx && sx < W && 0 <= sy && sy < H) {
          if (R[i][j] == 2 && R[sy][sx] == 1) {
            ret++;
          }
        } else if (R[i][j] == 1) {
          ret++;
        }
      }
    }
  }

  printf("%d\\n", ret);

  return 0;
}