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

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

struct node {
  int x, y, bit, t;
  node(int x, int y, int bit, int t) : x(x), y(y), bit(bit), t(t) {}
};

int x, y;
int sx, sy;
int n;
char c[22][22];
int num[10], d[10], s[10], e[10];
int value[1 << 10];
int done[22][22][1 << 8][102];
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};

int main(void) {
  while (1) {
    scanf("%d%d", &x, &y);
    if (x == 0 && y == 0)
      break;
    memset(value, 0, sizeof(value));
    queue<node> que;
    for (int i = 0; i <= 21; i++) {
      for (int j = 0; j <= 21; j++) {
        c[i][j] = '#';
      }
    }
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> c[j][i];
        if (c[j][i] == 'P') {
          que.push(node(j, i, 0, 0));
          c[j][i] = '.';
        }
      }
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      num[a] = i;
      scanf("%d%d%d", &d[i], &s[i], &e[i]);
    }
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        value[i] += (i >> j & 1) * d[j];
      }
      // cout << value[i] << " " << i << endl;
    }
    memset(done, 0, sizeof(done));
    int ans = 0;
    while (que.size()) {
      node q = que.front();
      que.pop();
      if (q.t > 100)
        continue;
      if (c[q.x][q.y] != '.')
        continue;
      for (int i = 0; i < 4; i++) {
        int tx = q.x + dx[i];
        int ty = q.y + dy[i];
        if (c[tx][ty] >= '0' && c[tx][ty] <= '9') {
          int idx = num[c[tx][ty] - '0'];
          if (s[idx] <= q.t && q.t < e[idx])
            q.bit |= 1 << idx;
        }
      }
      if (done[q.x][q.y][q.bit][q.t]++)
        continue;
      ans = max(ans, value[q.bit]);
      for (int i = 0; i < 5; i++) {
        que.push(node(q.x + dx[i], q.y + dy[i], q.bit, q.t + 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

struct node {
  int x, y, bit, t;
  node(int x, int y, int bit, int t) : x(x), y(y), bit(bit), t(t) {}
};

int x, y;
int sx, sy;
int n;
char c[22][22];
int num[10], d[10], s[10], e[10];
int value[1 << 10];
int done[22][22][1 << 10][102];
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};

int main(void) {
  while (1) {
    scanf("%d%d", &x, &y);
    if (x == 0 && y == 0)
      break;
    queue<node> que;
    for (int i = 0; i <= 21; i++) {
      for (int j = 0; j <= 21; j++) {
        c[i][j] = '#';
      }
    }
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> c[j][i];
        if (c[j][i] == 'P') {
          que.push(node(j, i, 0, 0));
          c[j][i] = '.';
        }
      }
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      scanf("%d%d%d", &d[a], &s[a], &e[a]);
    }
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        value[i] += (i >> j & 1) * d[j];
      }
      // cout << value[i] << " " << i << endl;
    }
    memset(done, 0, sizeof(done));
    int ans = 0;
    while (que.size()) {
      node q = que.front();
      que.pop();
      if (q.t > 100)
        continue;
      if (c[q.x][q.y] != '.')
        continue;
      for (int i = 0; i < 4; i++) {
        int tx = q.x + dx[i];
        int ty = q.y + dy[i];
        if (c[tx][ty] >= '0' && c[tx][ty] <= '9') {
          int idx = c[tx][ty] - '0';
          if (s[idx] <= q.t && q.t < e[idx])
            q.bit |= 1 << idx;
        }
      }
      if (done[q.x][q.y][q.bit][q.t]++)
        continue;
      ans = max(ans, value[q.bit]);
      for (int i = 0; i < 5; i++) {
        que.push(node(q.x + dx[i], q.y + dy[i], q.bit, q.t + 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}