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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

int w, h;
int sx, sy;
int lastTime;
int dc[10][102];

char t[22][22];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int dp[2][20][20][1 << 10];

void solve() {
  memset(dp, -1, sizeof(dp));
  dp[0][sy][sx][0] = 0;

  int to = (1 << 10);

  for (int I = 0; I < lastTime; I++) {
    int i = I % 2;
    int ni = (i + 1) % 2;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        for (int j = 0; j < to; j++) {
          dp[ni][y][x][j] = -1;
        }
      }
    }

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (t[y][x] != '.')
          continue;

        for (int j = 0; j < to; j++) {
          if (dp[i][y][x][j] == -1)
            continue;

          int ncost = dp[i][y][x][j];
          int nj = j;

          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
              continue;
            if (t[ny][nx] == '.')
              continue;

            int id = t[ny][nx] - '0';

            if (nj & (1 << id))
              continue;
            if (dc[id][I] == -1)
              continue;

            ncost += dc[id][I];
            nj |= (1 << id);
          }

          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
              continue;
            if (t[ny][nx] != '.')
              continue;

            dp[ni][ny][nx][nj] = max(dp[ni][ny][nx][nj], ncost);
          }
        }
      }
    }
  }

  int ans = 0;

  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      for (int j = 0; j < to; j++) {
        ans = max(ans, dp[lastTime % 2][y][x][j]);
      }
    }
  }

  cout << ans << endl;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> t[i][j];

        if (t[i][j] == 'P') {
          t[i][j] = '.';
          sx = j;
          sy = i;
        }
      }
    }

    lastTime = 0;
    memset(dc, -1, sizeof(dc));

    int n;
    cin >> n;

    while (n--) {
      int g, d, s, e;
      cin >> g >> d >> s >> e;
      lastTime = max(lastTime, e);

      for (int i = s; i < e; i++) {
        dc[g][i] = d;
      }
    }

    solve();
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

int w, h;
int sx, sy;
int lastTime;
int dc[10][102];

char t[22][22];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

short dp[101][20][20][1 << 10];

void solve() {
  memset(dp, -1, sizeof(dp));
  dp[0][sy][sx][0] = 0;

  int to = (1 << 10);

  for (int i = 0; i < lastTime; i++) {
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (t[y][x] != '.')
          continue;

        for (int j = 0; j < to; j++) {
          if (dp[i][y][x][j] == -1)
            continue;

          int ncost = dp[i][y][x][j];
          int nj = j;

          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
              continue;
            if (t[ny][nx] == '.')
              continue;

            int id = t[ny][nx] - '0';

            if (nj & (1 << id))
              continue;
            if (dc[id][i] == -1)
              continue;

            ncost += dc[id][i];
            nj |= (1 << id);
          }

          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
              continue;
            if (t[ny][nx] != '.')
              continue;

            dp[i + 1][ny][nx][nj] = max((int)dp[i + 1][ny][nx][nj], ncost);
          }
        }
      }
    }
  }

  int ans = 0;

  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      for (int j = 0; j < to; j++) {
        ans = max(ans, (int)dp[lastTime][y][x][j]);
      }
    }
  }

  cout << ans << endl;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> t[i][j];

        if (t[i][j] == 'P') {
          t[i][j] = '.';
          sx = j;
          sy = i;
        }
      }
    }

    lastTime = 0;
    memset(dc, -1, sizeof(dc));

    int n;
    cin >> n;

    while (n--) {
      int g, d, s, e;
      cin >> g >> d >> s >> e;
      lastTime = max(lastTime, e);

      for (int i = s; i < e; i++) {
        dc[g][i] = d;
      }
    }

    solve();
  }
}