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

#include <bits/stdc++.h>
using namespace std;
#define create clear

typedef pair<int, int> Pi;

int w, h, n, sx, sy;
char mas[20][20];
struct sell {
  int d, s, e; // 終了時刻
};
sell shop[10];
int dp[2][1 << 8][20][20];
const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

int main() {

  while (cin >> w >> h, w) {

    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 'P') {
          mas[i][j] = '.';
          sx = j, sy = i;
        }
      }
    }

    cin >> n;
    int change[10];
    fill_n(change, 10, -1);
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      shop[i] = (sell){b, c, d};
      change[a] = i;
    }

    int cur = 0, next = 1;
    int ans = 0;
    dp[cur][0][sy][sx] = 0;
    for (int time = 0; time < 100; time++) {
      memset(dp[next], -1, sizeof(dp[next]));
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          for (int i = 0; i < (1 << 8); i++) {
            if (dp[cur][i][y][x] == -1)
              continue;
            vector<int> item;
#define rep(i, n) for (int i = 0; i < n; i++)
            // item.clear();
            int ret = -1;
            for (int j = 0; j < 4; j++) {
              int ny = y + dy[j], nx = x + dx[j];
              if (ny < 0 || ny >= h || nx < 0 || nx >= w || mas[ny][nx] == '.')
                continue;
              int pos = change[mas[ny][nx] - '0'];
              if (change[pos] == -1)
                continue;
              if (shop[pos].s <= time && time < shop[pos].e)
                item.push_back(pos);
            }

            for (int k = 0; k < (1 << item.size()); k++) {
              int bit = i, next_cost = dp[cur][i][y][x];
              bool flag = false;
              for (int l = 0; l < item.size(); l++) {
                if ((k >> l) & 1) {
                  if ((bit >> item[l]) & 1) {
                    flag = true;
                    break;
                  }
                  next_cost += shop[item[l]].d;
                  bit |= 1 << item[l];
                }
              }
              if (~flag)
                for (int l = 0; l < 4; l++) {
                  int ny = y + dy[l], nx = x + dx[l];
                  if (ny < 0 || ny >= h || nx < 0 || nx >= w ||
                      isdigit(mas[ny][nx]))
                    continue;
                  if (dp[next][bit][ny][nx] == -1 ||
                      dp[next][bit][ny][nx] > next_cost) {
                    dp[next][bit][ny][nx] = next_cost;
                    ans = max(ans, next_cost);
                  }
                }
            }
          }
        }
      }
      swap(next, cur);
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define create clear

typedef pair<int, int> Pi;

int w, h, n, sx, sy;
char mas[20][20];
struct sell {
  int d, s, e; // 終了時刻
};
sell shop[10];
vector<vector<Pi>> hori_sensei;
int dp[1 << 10][20][20][101];
int cost[20][20][20][20];
const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

void bfs(int y, int x) {
  queue<Pi> que;
  que.push(Pi(x, y));
  cost[y][x][y][x] = 0;
  while (!que.empty()) {
    Pi p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.second + dy[i], nx = p.first + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || mas[ny][nx] != '.')
        continue;
      if (cost[y][x][ny][nx] > cost[y][x][p.second][p.first] + 1) {
        cost[y][x][ny][nx] = cost[y][x][p.second][p.first] + 1;
        que.push(Pi(nx, ny));
      }
    }
  }
  return;
}
int main() {

  while (cin >> w >> h, w) {

    hori_sensei.create();
    hori_sensei.resize(10);
    int INF = 1 << 30;
    fill_n(***cost, 20 * 20 * 20 * 20, INF);
    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 'P') {
          mas[i][j] = '.';
          sx = j, sy = i;
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (mas[i][j] == '.') {
          for (int k = 0; k < 4; k++) {
            int ny = i + dy[k], nx = j + dx[k];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w || mas[ny][nx] == '.')
              continue;
            hori_sensei[mas[ny][nx] - '0'].push_back(Pi(i, j));
          }
          bfs(i, j);
        }
      }
    }

    cin >> n;
    for (int i = 0; i < 10; i++)
      shop[i].d = -1;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      shop[a] = (sell){b, c, d};
    }

    dp[0][sy][sx][0] = 0;
    for (int time = 0; time < 101; time++) {
      for (int i = 0; i < (1 << 10) - 1; i++) {
        for (int y = 0; y < h; y++) {
          for (int x = 0; x < w; x++) {
            if (dp[i][y][x][time] == -1 || mas[y][x] != '.')
              continue;
            for (int j = 0; j < 10; j++) {
              if ((i >> j) & 1 || shop[j].d == -1 || time >= shop[j].e)
                continue;
              for (int k = 0; k < hori_sensei[j].size(); k++) {
                int res = cost[y][x][hori_sensei[j][k].first]
                              [hori_sensei[j][k].second];
                if (time + res >= shop[j].e)
                  continue;
                int &ret =
                    dp[i | (1 << j)][hori_sensei[j][k].first]
                      [hori_sensei[j][k].second][max(time + res, shop[j].s)];
                ret = max(ret, dp[i][y][x][time] + shop[j].d);
              }
            }
          }
        }
      }
    }
    cout << *max_element(dp[0][0][0], dp[1 << 10][0][0]) << endl;
  }
}