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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<char> vc;
typedef vector<vc> vvc;

int main() {
  for (int w, h; cin >> w >> h, w | h;) {
    vvc grid(h, vc(w));
    rep(i, h) rep(j, w) cin >> grid[i][j];
    int n;
    cin >> n;
    vi gs(n), ds(n), ss(n), es(n);
    rep(i, n) cin >> gs[i] >> ds[i] >> ss[i] >> es[i];

    rep(i, h) rep(j, w) {
      if (!isdigit(grid[i][j]))
        continue;
      grid[i][j] = find(all(gs), grid[i][j] - '0') - gs.begin() + '0';
    }

    static bool dp[101][20][20][1 << 10]; // [t][i][j][bit]
    memset(dp, 0, sizeof dp);

    rep(i, h) rep(j, w) if (grid[i][j] == 'P') {
      dp[0][i][j][0] = 1;
      grid[i][j] = '.';
    }

    rep(t, 100) rep(i, h) rep(j, w) rep(k, 1 << n) {
      if (dp[t][i][j][k] == 0)
        continue;
      rep(_, 4) {
        int ni = i + "\\xff\\x1\\0\\0"[_], nj = j + "\\0\\0\\xff\\x1"[_];
        if (ni < 0 || h <= ni || nj < 0 || w <= nj)
          continue;
        if (isdigit(grid[ni][nj])) {
          int item = grid[ni][nj] - '0';
          if (ss[item] <= t && t < es[item])
            dp[t][i][j][k | 1 << grid[ni][nj] - '0'] = 1;
        }
        if (grid[ni][nj] == '.')
          dp[t + 1][ni][nj][k] = 1;
      }
    }

    vi ps(1 << n);
    rep(i, 1 << n) rep(j, n) if (i & 1 << j) ps[i] += ds[j];

    int res = 0;
    rep(i, h) rep(j, w) rep(k, 1 << n) {
      if (dp[100][i][j][k])
        res = max(res, ps[k]);
    }
    cout << res << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<char> vc;
typedef vector<vc> vvc;

int main() {
  for (int w, h; cin >> w >> h, w | h;) {
    vvc grid(h, vc(w));
    rep(i, h) rep(j, w) cin >> grid[i][j];
    int n;
    cin >> n;
    vi gs(n), ds(n), ss(n), es(n);
    rep(i, n) cin >> gs[i] >> ds[i] >> ss[i] >> es[i];

    rep(i, h) rep(j, w) {
      if (!isdigit(grid[i][j]))
        continue;
      grid[i][j] = find(all(gs), grid[i][j] - '0') - gs.begin() + '0';
    }

    static int dp[101][10][10][1 << 10]; // [t][i][j][bit]
    memset(dp, 0, sizeof dp);

    // rep(i,h) rep(j,w)
    //	if(grid[i][j]=='P'){
    //		dp[0][i][j][0]=1;
    //		grid[i][j]='.';
    //	}
    //
    // rep(t,100) rep(i,h) rep(j,w) rep(k,1<<n){
    //	if(dp[t][i][j][k]==0)
    //		continue;
    //	rep(_,4){
    //		int ni=i+"\\xff\\x1\\0\\0"[_],nj=j+"\\0\\0\\xff\\x1"[_];
    //		if(ni<0 || h<=ni || nj<0 || w<=nj)
    //			continue;
    //		if(isdigit(grid[ni][nj])){
    //			int item=grid[ni][nj]-'0';
    //			if(ss[item]<=t && t<es[item])
    //				dp[t][i][j][k|1<<grid[ni][nj]-'0']=1;
    //		}
    //		if(grid[ni][nj]=='.')
    //			dp[t+1][ni][nj][k]=1;
    //	}
    // }

    vi ps(1 << n);
    rep(i, 1 << n) rep(j, n) if (i & 1 << j) ps[i] += ds[j];

    int res = 0;
    rep(i, h) rep(j, w) rep(k, 1 << n) {
      if (dp[100][i][j][k])
        res = max(res, ps[k]);
    }
    cout << res << endl;
  }

  return 0;
}