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

#include <iostream>
#include <queue>
#include <set>

#define INV (-1)
#define MAX 128
#define MAX_ENEMY 10
#define MAX_STORE 21

using namespace std;

const int even_di[] = {-1, -1, 0, 0, 1, 1};
const int even_dj[] = {0, 1, -1, 1, 0, 1};
const int odd_di[] = {-1, -1, 0, 0, 1, 1};
const int odd_dj[] = {-1, 0, -1, 1, -1, 0};
const int infty = 1 << 26;

class Area {
public:
  int store;
  int dist[MAX_STORE];
  bool valid;
  Area() : store(INV), valid(false) {
    for (int i = 0; i < MAX_STORE; ++i) {
      dist[i] = infty;
    }
  }
};

class Map {
public:
  int n, m;
  Area T[MAX][MAX];
  Map() {}
  Map(int n, int m) : n(n), m(m) {
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < m + 1; ++j) {
        T[i][j].valid = true;
        for (int k = 0; k < MAX_STORE; ++k) {
          T[i][j].dist[k] = infty;
        }
      }
    }
  }
  int occupied(int store) {
    int ret = 0;
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < m + 1; ++j) {
        int minimum = infty;
        int id = -1;
        int cnt = 0;
        for (int k = 0; k < MAX_STORE; ++k) {
          if (k >= MAX_ENEMY && k != store)
            continue;
          minimum = min(minimum, T[i][j].dist[k]);
        }
        for (int k = 0; k < MAX_STORE; ++k) {
          if (k >= MAX_ENEMY && k != store)
            continue;
          if (minimum == T[i][j].dist[k]) {
            ++cnt;
            id = k;
          }
        }
        if (cnt == 1 && id == store) {
          ++ret;
        }
      }
    }
    return ret;
  }
};

class State {
public:
  int i, j;
  int cost;
  State() : i(0), j(0), cost(infty) {}
  State(int i, int j, int cost) : i(i), j(j), cost(cost) {}
};

void store_bfs(int i, int j, int store, Map &M) {
  queue<State> qpi;
  set<pair<int, int>> vis;
  qpi.push(State(i, j, 0));

  vis.insert(make_pair(i, j));
  while (!qpi.empty()) {
    State now = qpi.front();
    qpi.pop();

    M.T[now.i][now.j].dist[store] = now.cost;

    for (int i = 0; i < sizeof(even_di) / sizeof(*even_di); ++i) {
      int ti = now.i + (now.i % 2 == 0 ? even_di[i] : odd_di[i]);
      int tj = now.j + (now.i % 2 == 0 ? even_dj[i] : odd_dj[i]);

      if (!M.T[ti][tj].valid)
        continue;
      if (vis.find(make_pair(ti, tj)) != vis.end())
        continue;

      vis.insert(make_pair(ti, tj));
      qpi.push(State(ti, tj, now.cost + 1));
    }
  }
}

int main() {
  while (true) {
    int m, n;
    int ans = 0;
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    if (n == 0)
      break;
    int st;
    Map M(n, m);

    cin >> st;
    for (int i = 0; i < st; ++i) {
      int y, x;
      cin >> x >> y;
      //++x;++y;
      M.T[y][x].store = i;
      store_bfs(y, x, i, M);
    }

    int cands;
    cin >> cands;
    for (int i = 0; i < cands; ++i) {
      int y, x;
      cin >> x >> y;
      //++x;++y;
      M.T[y][x].store = MAX_ENEMY + i;
      store_bfs(y, x, MAX_ENEMY + i, M);
    }

    /*
    for(int i = 0; i < n+2; ++i){
      for(int j = 0; j < m+2; ++j){
        cout << "(" << j << " , " << i << ") = ";
        for(int k = 0; k < MAX_STORE; ++k){
          cout << M.T[i][j].dist[k] << ' ';
        }
        cout << endl;
      }
      cout << "--------" << endl;
    }
    */

    for (int i = 0; i < cands; ++i) {
      ans = max(ans, M.occupied(MAX_ENEMY + i));
    }

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

#define INV (-1)
#define MAX 128
#define MAX_ENEMY 10
#define MAX_STORE 21

using namespace std;

const int even_di[] = {-1, -1, 0, 0, 1, 1};
const int even_dj[] = {0, 1, -1, 1, 0, 1};
const int odd_di[] = {-1, -1, 0, 0, 1, 1};
const int odd_dj[] = {-1, 0, -1, 1, -1, 0};
const int infty = 1 << 26;

class Area {
public:
  int store;
  int dist[MAX_STORE];
  bool valid;
  Area() : store(INV), valid(false) {
    for (int i = 0; i < MAX_STORE; ++i) {
      dist[i] = infty;
    }
  }
};

class Map {
public:
  int n, m;
  Area T[MAX][MAX];
  Map() {}
  Map(int n, int m) : n(n), m(m) {
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < m + 1; ++j) {
        T[i][j].valid = true;
        for (int k = 0; k < MAX_STORE; ++k) {
          T[i][j].dist[k] = infty;
        }
      }
    }
  }
  int occupied(int store) {
    int ret = 0;
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < m + 1; ++j) {
        int minimum = infty;
        int id = -1;
        int cnt = 0;
        for (int k = 0; k < MAX_STORE; ++k) {
          // if( k >= MAX_ENEMY && k != store ) continue;
          minimum = min(minimum, T[i][j].dist[k]);
        }
        for (int k = 0; k < MAX_STORE; ++k) {
          // if( k >= MAX_ENEMY && k != store ) continue;
          if (minimum == T[i][j].dist[k]) {
            ++cnt;
            id = k;
          }
        }
        if (cnt == 1 && id == store) {
          ++ret;
        }
      }
    }
    return ret;
  }
};

class State {
public:
  int i, j;
  int cost;
  State() : i(0), j(0), cost(infty) {}
  State(int i, int j, int cost) : i(i), j(j), cost(cost) {}
};

void store_bfs(int i, int j, int store, Map &M) {
  queue<State> qpi;

  qpi.push(State(i, j, 0));
  while (!qpi.empty()) {
    State now = qpi.front();
    qpi.pop();

    // if( M.T[now.i][now.j].dist[store] < infty ) continue;
    M.T[now.i][now.j].dist[store] = now.cost;

    for (int i = 0; i < sizeof(even_di) / sizeof(*even_di); ++i) {
      int ti = now.i + (now.i % 2 == 0 ? even_di[i] : odd_di[i]);
      int tj = now.j + (now.i % 2 == 0 ? even_dj[i] : odd_dj[i]);

      if (!M.T[ti][tj].valid)
        continue;
      if (M.T[ti][tj].dist[store] < infty)
        continue;
      // M.T[ti][tj].dist[store] = now.cost+1;

      qpi.push(State(ti, tj, now.cost + 1));
    }
  }
}

int main() {
  while (true) {
    int m, n;
    int ans = 0;
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    if (n == 0)
      break;
    int st;
    Map M(n, m);

    cin >> st;
    for (int i = 0; i < st; ++i) {
      int y, x;
      cin >> x >> y;
      //++x;++y;
      M.T[y][x].store = i;
      store_bfs(y, x, i, M);
    }

    int cands;
    cin >> cands;
    for (int i = 0; i < cands; ++i) {
      int y, x;
      cin >> x >> y;
      //++x;++y;
      M.T[y][x].store = MAX_ENEMY + i;
      store_bfs(y, x, MAX_ENEMY + i, M);
    }

    /*
    for(int i = 0; i < n+2; ++i){
      for(int j = 0; j < m+2; ++j){
        cout << "(" << j << " , " << i << ") = ";
        for(int k = 0; k < MAX_STORE; ++k){
          cout << M.T[i][j].dist[k] << ' ';
        }
        cout << endl;
      }
      cout << "--------" << endl;
    }
    */

    for (int i = 0; i < cands; ++i) {
      ans = max(ans, M.occupied(MAX_ENEMY + i));
    }

    cout << ans << endl;
  }
  return 0;
}