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

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int x, y, c, num;
  NODE(int x, int y, int c) : x(x), y(y), c(c) {}
  NODE(int x, int y, int c, int num) : x(x), y(y), c(c), num(num) {}
};
int W, H;

int doit(vector<NODE> v) {
  queue<NODE> Q;
  for (int i = 0; i < v.size(); i++) {
    Q.push(NODE(v[i].x, v[i].y, 0, i));
  }
  int cnt[100][100] = {};
  int ans[100][100];
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      ans[i][j] = 1e9;

  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  int dx[2][6] = {{-1, 0, 1, -1, -1, 0}, {-1, 0, 0, 1, 1, 1}};
  int dy[2][6] = {{0, 1, 0, 1, -1, -1}, {0, 1, -1, 0, 1, -1}};
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  while (Q.size()) {
    NODE q = Q.front();
    Q.pop();
    if (q.x < 0 || q.y < 0 || q.x >= W || q.y >= H)
      continue;
    if (ans[q.y][q.x] < q.c)
      continue;
    if (ans[q.y][q.x] > 10000) {
      ans[q.y][q.x] = q.c;
      cnt[q.y][q.x] = (1 << q.num);
    } else if (ans[q.y][q.x] == q.c) {
      cnt[q.y][q.x] |= (1 << q.num);
      continue;
    }
    for (int i = 0; i < 6; i++) {
      Q.push(NODE(q.x + dx[q.y % 2][i], q.y + dy[q.y % 2][i], q.c + 1, q.num));
    }
  }

  int cntt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      // printf("%3d",cnt[i][j]);
      if (cnt[i][j] == (1 << (v.size() - 1))) {
        cntt++;
      }
    }
    // puts("");
  }
  return cntt;
}
int main() {

  while (cin >> W >> H && W) {
    int s, N;
    cin >> s;
    vector<NODE> alr;
    for (int i = 0; i < s; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      alr.push_back(NODE(x, y, 0));
    }
    cin >> N;
    int ans = 0;
    for (int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      alr.push_back(NODE(x, y, 0));
      ans = max(ans, doit(alr));
      alr.pop_back();
    }

    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int x, y, c, num;
  NODE(int x, int y, int c) : x(x), y(y), c(c) {}
  NODE(int x, int y, int c, int num) : x(x), y(y), c(c), num(num) {}
};
int W, H;

int doit(vector<NODE> v) {
  queue<NODE> Q;
  for (int i = 0; i < v.size(); i++) {
    Q.push(NODE(v[i].x, v[i].y, 0, i));
  }
  int cnt[100][100] = {};
  int ans[100][100];
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      ans[i][j] = 1e9;

  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  int dx[2][6] = {{-1, 0, 1, -1, -1, 0}, {-1, 1, 0, 1, 1, 1}};
  int dy[2][6] = {{0, 1, 0, 1, -1, -1}, {0, 0, -1, 0, 1, -1}};
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  // kore Shindoinen!!!!!!!!!!!!!
  while (Q.size()) {
    NODE q = Q.front();
    Q.pop();
    if (q.x < 0 || q.y < 0 || q.x >= W || q.y >= H)
      continue;
    if (ans[q.y][q.x] < q.c)
      continue;
    if (ans[q.y][q.x] > 10000) {
      ans[q.y][q.x] = q.c;
      cnt[q.y][q.x] = (1 << q.num);
    } else if (ans[q.y][q.x] == q.c) {
      cnt[q.y][q.x] |= (1 << q.num);
    }
    for (int i = 0; i < 6; i++) {
      Q.push(NODE(q.x + dx[q.y % 2][i], q.y + dy[q.y % 2][i], q.c + 1, q.num));
    }
  }
  int cntt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (cnt[i][j] == (1 << (v.size() - 1))) {
        cntt++;
      }
    }
  }
  return cntt;
}
int main() {

  while (cin >> W >> H && W) {
    int s, N;
    cin >> s;
    vector<NODE> alr;
    for (int i = 0; i < s; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      alr.push_back(NODE(x, y, 0));
    }
    cin >> N;
    int ans = 0;
    for (int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      alr.push_back(NODE(x, y, 0));
      ans = max(ans, doit(alr));
      alr.pop_back();
    }

    cout << ans << endl;
  }
}