Problem p00245 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;

struct Data {
  int y, x, t, mask;
  Data(int y, int x, int t, int m) : y(y), x(x), t(t), mask(m) {}
};

struct Sale {
  int d, s, e;
};

const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};
bool visited[20][20][100][1 << 8];
int main() {
  int h, w;
  while (scanf("%d%d", &w, &h), w) {
    char field[20][20];
    rep(i, h) rep(j, w) {
      char c[4];
      scanf("%s", c);
      field[i][j] = c[0];
    }

    int n;
    scanf("%d", &n);

    int dict[10];
    fill(dict, dict + 10, -1);
    Sale sale[8];
    rep(i, n) {
      int g;
      scanf("%d%d%d%d", &g, &sale[i].d, &sale[i].s, &sale[i].e);
      dict[g] = i;
    }

    rep(i, h) rep(j, w) {
      if (isdigit(field[i][j])) {
        int v = dict[field[i][j] - '0'];
        if (v == -1)
          field[i][j] = 'x';
        else
          field[i][j] = v + '0';
      }
    }

    int mask[20][20];
    rep(i, h) rep(j, w) {
      if (isdigit(field[i][j]) || field[i][j] == 'x')
        mask[i][j] = -1;
      else {
        mask[i][j] = 0;
        rep(k, 4) {
          int py = i + dy[k];
          int px = j + dx[k];
          if (py < 0 || h <= py || px < 0 || w <= px)
            continue;
          if (isdigit(field[py][px]))
            mask[i][j] |= 1 << (field[py][px] - '0');
        }
      }
    }

    int money[1 << 8];
    rep(m, 1 << n) {
      money[m] = 0;
      rep(i, n) {
        if ((m >> i) & 1)
          money[m] += sale[i].d;
      }
    }

    queue<Data> Q;
    rep(i, h) rep(j, w) {
      if (field[i][j] == 'P') {
        Q.push(Data(i, j, 0, 0));
        goto next;
      }
    }

  next:
    int ans = 0;
    rep(y, h) rep(x, w) rep(t, 100) rep(m, 1 << n) visited[y][x][t][m] = false;

    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      rep(i, n) {
        if (((mask[d.y][d.x] >> i) & 1) && sale[i].s <= d.t && d.t < sale[i].e)
          d.mask |= 1 << i;
      }
      ans = max(ans, money[d.mask]);
      if (d.t == 100 || visited[d.y][d.x][d.t][d.mask])
        continue;
      visited[d.y][d.x][d.t][d.mask] = true;

      Q.push(Data(d.y, d.x, d.t + 1, d.mask));
      rep(k, 4) {
        int py = d.y + dy[k];
        int px = d.x + dx[k];
        if (py < 0 || h <= py || px < 0 || w <= px || mask[py][px] == -1)
          continue;
        Q.push(Data(py, px, d.t + 1, d.mask));
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;

struct Data {
  int y, x, t, mask;
  Data(int y, int x, int t, int m) : y(y), x(x), t(t), mask(m) {}
};

struct Sale {
  int d, s, e;
};

const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};
bool visited[20][20][100][1 << 8];
int main() {
  int h, w;
  while (scanf("%d%d", &w, &h), w) {
    char field[20][20];
    rep(i, h) rep(j, w) {
      char c[4];
      scanf("%s", c);
      field[i][j] = c[0];
    }

    int n;
    scanf("%d", &n);

    int dict[10];
    fill(dict, dict + 10, -1);
    Sale sale[8];
    rep(i, n) {
      int g;
      scanf("%d%d%d%d", &g, &sale[i].d, &sale[i].s, &sale[i].e);
      dict[g] = i;
      if (sale[i].s == 100) {
        assert(false);
        for (;;)
          ;
      }
    }

    rep(i, h) rep(j, w) {
      if (isdigit(field[i][j])) {
        int v = dict[field[i][j] - '0'];
        if (v == -1)
          field[i][j] = 'x';
        else
          field[i][j] = v + '0';
      }
    }

    int mask[20][20];
    rep(i, h) rep(j, w) {
      if (isdigit(field[i][j]) || field[i][j] == 'x')
        mask[i][j] = -1;
      else {
        mask[i][j] = 0;
        rep(k, 4) {
          int py = i + dy[k];
          int px = j + dx[k];
          if (py < 0 || h <= py || px < 0 || w <= px)
            continue;
          if (isdigit(field[py][px]))
            mask[i][j] |= 1 << (field[py][px] - '0');
        }
      }
    }

    int money[1 << 8];
    rep(m, 1 << n) {
      money[m] = 0;
      rep(i, n) {
        if ((m >> i) & 1)
          money[m] += sale[i].d;
      }
    }

    queue<Data> Q;
    rep(i, h) rep(j, w) {
      if (field[i][j] == 'P') {
        Q.push(Data(i, j, 0, 0));
        goto next;
      }
    }

  next:
    int ans = 0;
    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      rep(i, n) {
        if (((mask[d.y][d.x] >> i) & 1) && sale[i].s <= d.t && d.t < sale[i].e)
          d.mask |= 1 << i;
      }
      if (visited[d.y][d.x][d.t][d.mask])
        continue;
      visited[d.y][d.x][d.t][d.mask] = true;
      ans = max(ans, money[d.mask]);
      if (d.t == 99)
        continue;

      Q.push(Data(d.y, d.x, d.t + 1, d.mask));
      rep(k, 4) {
        int py = d.y + dy[k];
        int px = d.x + dx[k];
        if (py < 0 || h <= py || px < 0 || w <= px || mask[py][px] == -1)
          continue;
        Q.push(Data(py, px, d.t + 1, d.mask));
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}