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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

int m, n;
int s, t;
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int INF = 10000000;
int field[101][101];
int field2[101][101];
int x[11], y[11];
int nx[11], ny[11];
int dx1[6] = {0, -1, 0, 1, 1, 1};
int dx2[6] = {-1, -1, -1, 0, 1, 0};
int dy[6] = {-1, 0, 1, -1, 0, 1};

void bfs(int sx, int sy) {
  queue<PP> que;
  que.push(PP(P(sx, sy), 0));
  field[sx][sy] = 0;
  while (que.size()) {
    PP q = que.front();
    que.pop();
    int cx = q.first.first, cy = q.first.second;
    int c = q.second;
    for (int i = 0; i < 6; i++) {
      int py = cy + dy[i];
      int px = cx;
      if (cy % 2 == 0)
        px += dx1[i];
      if (cy % 2 == 1)
        px += dx2[i];
      if (px >= 1 && px <= m && py >= 1 && py <= n && field[px][py] > c + 1) {
        field[px][py] = c + 1;
        que.push(PP(P(px, py), c + 1));
      }
    }
  }
}

int bfs2(int sx, int sy) {
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j <= 100; j++) {
      field2[i][j] = field[i][j];
    }
  }
  queue<PP> que;
  int res = 1;
  if (field2[sx][sy] == 0)
    return 0;
  field2[sx][sy] = 0;
  que.push(PP(P(sx, sy), 0));
  while (que.size()) {
    PP q = que.front();
    que.pop();
    int cx = q.first.first, cy = q.first.second;
    int c = q.second;
    for (int i = 0; i < 6; i++) {
      int py = cy + dy[i];
      int px = cx;
      if (cy % 2 == 0)
        px += dx1[i];
      if (cy % 2 == 1)
        px += dx2[i];
      if (px >= 1 && px <= m && py >= 1 && py <= n && field2[px][py] > c + 1) {
        res++;
        field2[px][py] = c + 1;
        que.push(PP(P(px, py), c + 1));
      }
    }
  }
  return res;
}

int main(void) {
  while (1) {
    int ans = 0;
    for (int i = 0; i <= 100; i++) {
      for (int j = 0; j <= 100; j++) {
        field[i][j] = INF;
      }
    }
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    cin >> s;
    for (int i = 0; i < s; i++) {
      cin >> x[i] >> y[i];
      bfs(x[i], y[i]);
    }
    cin >> t;
    for (int i = 0; i < t; i++) {
      cin >> nx[i] >> ny[i];
      ans = max(ans, bfs2(nx[i], ny[i]));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

int m, n;
int s, t;
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int INF = 10000000;
int field[101][101];
int field2[101][101];
int x[11], y[11];
int nx[11], ny[11];
int dx1[6] = {0, -1, 0, 1, 1, 1};
int dx2[6] = {-1, -1, -1, 0, 1, 0};
int dy[6] = {-1, 0, 1, -1, 0, 1};

void bfs(int sx, int sy) {
  queue<PP> que;
  que.push(PP(P(sx, sy), 0));
  field[sx][sy] = 0;
  while (que.size()) {
    PP q = que.front();
    que.pop();
    int cx = q.first.first, cy = q.first.second;
    int c = q.second;
    for (int i = 0; i < 6; i++) {
      int py = cy + dy[i];
      int px = cx;
      if (cy % 2 == 0)
        px += dx1[i];
      if (cy % 2 == 1)
        px += dx2[i];
      if (px >= 1 && px <= m && py >= 1 && py <= n && field[px][py] > c + 1) {
        field[px][py] = c + 1;
        que.push(PP(P(px, py), c + 1));
      }
    }
  }
}

int bfs2(int sx, int sy) {
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j <= 100; j++) {
      field2[i][j] = field[i][j];
    }
  }
  queue<PP> que;
  int res = 1;
  if (field2[sx][sy] == 0)
    return 0;
  que.push(PP(P(sx, sy), 0));
  while (que.size()) {
    PP q = que.front();
    que.pop();
    int cx = q.first.first, cy = q.first.second;
    int c = q.second;
    for (int i = 0; i < 6; i++) {
      int py = cy + dy[i];
      int px = cx;
      if (cy % 2 == 0)
        px += dx1[i];
      if (cy % 2 == 1)
        px += dx2[i];
      if (px >= 1 && px <= m && py >= 1 && py <= n && field2[px][py] > c + 1) {
        res++;
        que.push(PP(P(px, py), c + 1));
      }
    }
  }
  return res;
}

int main(void) {
  while (1) {
    int ans = 0;
    for (int i = 0; i <= 100; i++) {
      for (int j = 0; j <= 100; j++) {
        field[i][j] = INF;
      }
    }
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    cin >> s;
    for (int i = 0; i < s; i++) {
      cin >> x[i] >> y[i];
      bfs(x[i], y[i]);
    }
    cin >> t;
    for (int i = 0; i < t; i++) {
      cin >> nx[i] >> ny[i];
      ans = max(ans, bfs2(nx[i], ny[i]));
    }
    cout << ans << endl;
  }
  return 0;
}