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

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

#define INF 1010001000

typedef pair<int, int> i_i;
typedef long long int ll;

int n, m;
int dom[6] = {1, 1, 1, 0, -1, 0};
int don[6] = {-1, 0, 1, -1, 0, 1};
int dem[6] = {-1, -1, -1, 0, 1, 0};
int den[6] = {-1, 0, 1, -1, 0, 1};

int bfs(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos);
int solve(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos);

int main() {
  while (cin >> m >> n, (n | m)) {
    int s, t;
    vector<vector<int>> g(n, vector<int>(m, 0));
    vector<vector<bool>> pos(n, vector<bool>(m, false));
    cin >> s;
    for (int i = 0; i < s; i++) {
      int x, y;
      cin >> x >> y;
      pos[y - 1][x - 1] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // cout << "===" << i << j << "===" << endl;
        g[i][j] = bfs(i, j, g, pos);
        // cout << g[i][j];
      }
      // cout << endl;
    }
    cin >> t;
    int ans = -INF;
    for (int i = 0; i < t; i++) {
      int x, y;
      cin >> x >> y;
      ans = max(solve(y - 1, x - 1, g, pos), ans);
    }
    cout << ans << endl;
  }
  return 0;
}

int solve(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos) {
  int ret = 0, dist = 0;
  vector<vector<bool>> ch(n, vector<bool>(m, false));
  queue<i_i> a, b, c;
  a.push(make_pair(x, y));
  while (!a.empty()) {
    while (!a.empty()) {
      i_i tmp = a.front();
      a.pop();
      x = tmp.first;
      y = tmp.second;
      if (ch[x][y]) {
        continue;
      } else {
        ch[x][y] = true;
      }
      if (dist < g[x][y]) {
        ret++;
        for (int i = 0; i < 6; i++) {
          if (x % 2) {
            int nx = x + don[i];
            int ny = y + dom[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          } else {
            int nx = x + den[i];
            int ny = y + dem[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          }
        }
      }
    }
    a = b;
    b = c;
    dist++;
  }
  return ret;
}

int bfs(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos) {
  int ret = 0;
  queue<i_i> a, b, c;
  vector<vector<bool>> ch(n, vector<bool>(m, false));
  a.push(make_pair(x, y));
  while (!a.empty()) {
    while (!a.empty()) {
      i_i tmp = a.front();
      a.pop();
      x = tmp.first;
      y = tmp.second;
      if (ch[x][y]) {
        continue;
      } else {
        ch[x][y] = true;
      }
      if (pos[x][y]) {
        return ret;
      } else {
        for (int i = 0; i < 6; i++) {
          if (x % 2) {
            int nx = x + don[i];
            int ny = y + dom[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          } else {
            int nx = x + den[i];
            int ny = y + dem[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          }
        }
      }
    }
    ret++;
    a = b;
    b = c;
  }
  return ret;
}

Error: MLE

Fixed submission:

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

#define INF 1010001000

typedef pair<int, int> i_i;
typedef long long int ll;

int n, m;
int dom[6] = {1, 1, 1, 0, -1, 0};
int don[6] = {-1, 0, 1, -1, 0, 1};
int dem[6] = {-1, -1, -1, 0, 1, 0};
int den[6] = {-1, 0, 1, -1, 0, 1};

int bfs(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos);
int solve(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos);

int main() {
  while (cin >> m >> n, (n | m)) {
    int s, t;
    vector<vector<int>> g(n, vector<int>(m, 0));
    vector<vector<bool>> pos(n, vector<bool>(m, false));
    cin >> s;
    for (int i = 0; i < s; i++) {
      int x, y;
      cin >> x >> y;
      pos[y - 1][x - 1] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // cout << "===" << i << j << "===" << endl;
        g[i][j] = bfs(i, j, g, pos);
        // cout << g[i][j];
      }
      // cout << endl;
    }
    cin >> t;
    int ans = -INF;
    for (int i = 0; i < t; i++) {
      int x, y;
      cin >> x >> y;
      ans = max(solve(y - 1, x - 1, g, pos), ans);
    }
    cout << ans << endl;
  }
  return 0;
}

int solve(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos) {
  int ret = 0, dist = 0;
  vector<vector<bool>> ch(n, vector<bool>(m, false));
  queue<i_i> a, b, c;
  a.push(make_pair(x, y));
  while (!a.empty()) {
    while (!a.empty()) {
      i_i tmp = a.front();
      a.pop();
      x = tmp.first;
      y = tmp.second;
      if (ch[x][y]) {
        continue;
      } else {
        ch[x][y] = true;
      }
      if (dist < g[x][y]) {
        ret++;
        for (int i = 0; i < 6; i++) {
          if (x % 2) {
            int nx = x + don[i];
            int ny = y + dom[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          } else {
            int nx = x + den[i];
            int ny = y + dem[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          }
        }
      }
    }
    a = b;
    b = c;
    dist++;
  }
  return ret;
}

int bfs(int x, int y, vector<vector<int>> &g, vector<vector<bool>> &pos) {
  int ret = 0;
  queue<i_i> a, b, c;
  a.push(make_pair(x, y));
  while (!a.empty()) {
    while (!a.empty()) {
      i_i tmp = a.front();
      a.pop();
      x = tmp.first;
      y = tmp.second;
      if (pos[x][y]) {
        return ret;
      } else {
        for (int i = 0; i < 6; i++) {
          if (x % 2) {
            int nx = x + don[i];
            int ny = y + dom[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          } else {
            int nx = x + den[i];
            int ny = y + dem[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < m) {
              b.push(make_pair(nx, ny));
            }
          }
        }
      }
    }
    ret++;
    a = b;
    b = c;
  }
  return ret;
}