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

#include <iostream>
#include <string>

#pragma warning(disable : 4996)

#define mod 100000

using namespace std;

int H, W, dp[2][1 << 19][2][2], bits[10946], maxbits;

string M[20];

void rec(int pos, int bit) {
  bits[maxbits++] = bit;

  for (int i = pos + 2; i < W - 1; i++) {
    rec(i, bit + (1 << i));
  }
}

int main() {
  scanf("%d", &H);
  scanf("%d", &W);

  for (int i = 0; i < H; i++)
    cin >> M[i];

  if (M[0][0] == 'J' || M[0][0] == '?')
    dp[0][0][0][0] += 1;
  if (M[0][0] == 'O' || M[0][0] == '?')
    dp[0][0][1][0] += 1;
  if (M[0][0] == 'I' || M[0][0] == '?')
    dp[0][0][1][0] += 1;

  maxbits = 1;
  bits[0] = 0;

  for (int i = 0; i < W - 1; i++) {
    rec(i, 1 << i);
  }

  for (int i = 0; i < H * W - 1; i++) {
    for (int j = 0; j < maxbits; j++) {
      dp[(i ^ 1) & 1][bits[j]][0][0] = 0;
      dp[(i ^ 1) & 1][bits[j]][0][1] = 0;
      dp[(i ^ 1) & 1][bits[j]][1][0] = 0;
      dp[(i ^ 1) & 1][bits[j]][1][1] = 0;
    }

    for (int j = 0; j < maxbits; j++) {
      char c = M[(i + 1) / W][(i + 1) % W];

      int nxt = (bits[j] << 1) % (1 << (W - 1));

      int sum0 = (dp[i & 1][bits[j]][0][0] + dp[i & 1][bits[j]][1][0]) % mod;
      int sum1 = (dp[i & 1][bits[j]][0][1] + dp[i & 1][bits[j]][1][1]) % mod;

      if (c == 'J' || c == '?') {
        dp[(i ^ 1) & 1][nxt][0][0] += sum0;
        dp[(i ^ 1) & 1][nxt][0][1] += sum1;
      }

      if (c == 'O' || c == '?') {
        if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
            i % W != W - 1) {
          dp[(i ^ 1) & 1][nxt + 1][1][0] += dp[i & 1][bits[j]][0][0];
          dp[(i ^ 1) & 1][nxt + 1][1][1] += dp[i & 1][bits[j]][0][1];

          dp[(i ^ 1) & 1][nxt + 1][1][0] %= mod;
          dp[(i ^ 1) & 1][nxt + 1][1][1] %= mod;

          dp[(i ^ 1) & 1][nxt][1][0] += (sum0 - dp[i & 1][bits[j]][0][0] + mod);
          dp[(i ^ 1) & 1][nxt][1][1] += (sum1 - dp[i & 1][bits[j]][0][1] + mod);
        } else {
          dp[(i ^ 1) & 1][nxt][1][0] += sum0;
          dp[(i ^ 1) & 1][nxt][1][1] += sum1;
        }
      }

      if (c == 'I' || c == '?') {
        if (bits[j] & (1 << (W - 2))) {
          dp[(i ^ 1) & 1][nxt][1][1] += sum0;
        } else {
          dp[(i ^ 1) & 1][nxt][1][0] += sum0;
        }

        dp[(i ^ 1) & 1][nxt][1][1] += sum1;
      }

      dp[(i ^ 1) & 1][nxt][1][0] %= mod;
      dp[(i ^ 1) & 1][nxt][1][1] %= mod;
    }
  }

  int ret = 0;

  for (int i = 0; i < maxbits; i++) {
    ret += dp[(H * W - 1) % 2][bits[i]][0][1];
    ret += dp[(H * W - 1) % 2][bits[i]][1][1];

    ret %= mod;
  }

  printf("%d\\n", ret);

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

#pragma warning(disable : 4996)

#define mod 100000

using namespace std;

int H, W, dp[2][1 << 19][2][2], bits[10946], maxbits;

string M[20];

void rec(int pos, int bit) {
  bits[maxbits++] = bit;

  for (int i = pos + 2; i < W; i++) {
    rec(i, bit + (1 << i));
  }
}

int main() {
  scanf("%d", &H);
  scanf("%d", &W);

  for (int i = 0; i < H; i++)
    cin >> M[i];

  if (M[0][0] == 'J' || M[0][0] == '?')
    dp[0][0][0][0] += 1;
  if (M[0][0] == 'O' || M[0][0] == '?')
    dp[0][0][1][0] += 1;
  if (M[0][0] == 'I' || M[0][0] == '?')
    dp[0][0][1][0] += 1;

  maxbits = 1;
  bits[0] = 0;

  for (int i = 0; i < W; i++) {
    rec(i, 1 << i);
  }

  for (int i = 0; i < H * W - 1; i++) {
    for (int j = 0; j < maxbits; j++) {
      dp[(i ^ 1) & 1][bits[j]][0][0] = 0;
      dp[(i ^ 1) & 1][bits[j]][0][1] = 0;
      dp[(i ^ 1) & 1][bits[j]][1][0] = 0;
      dp[(i ^ 1) & 1][bits[j]][1][1] = 0;
    }

    for (int j = 0; j < maxbits; j++) {
      char c = M[(i + 1) / W][(i + 1) % W];

      int nxt = (bits[j] << 1) % (1 << (W - 1));

      int sum0 = (dp[i & 1][bits[j]][0][0] + dp[i & 1][bits[j]][1][0]) % mod;
      int sum1 = (dp[i & 1][bits[j]][0][1] + dp[i & 1][bits[j]][1][1]) % mod;

      if (c == 'J' || c == '?') {
        dp[(i ^ 1) & 1][nxt][0][0] += sum0;
        dp[(i ^ 1) & 1][nxt][0][1] += sum1;
      }

      if (c == 'O' || c == '?') {
        if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
            i % W != W - 1) {
          dp[(i ^ 1) & 1][nxt + 1][1][0] += dp[i & 1][bits[j]][0][0];
          dp[(i ^ 1) & 1][nxt + 1][1][1] += dp[i & 1][bits[j]][0][1];

          dp[(i ^ 1) & 1][nxt + 1][1][0] %= mod;
          dp[(i ^ 1) & 1][nxt + 1][1][1] %= mod;

          dp[(i ^ 1) & 1][nxt][1][0] += (sum0 - dp[i & 1][bits[j]][0][0] + mod);
          dp[(i ^ 1) & 1][nxt][1][1] += (sum1 - dp[i & 1][bits[j]][0][1] + mod);
        } else {
          dp[(i ^ 1) & 1][nxt][1][0] += sum0;
          dp[(i ^ 1) & 1][nxt][1][1] += sum1;
        }
      }

      if (c == 'I' || c == '?') {
        if (bits[j] & (1 << (W - 2))) {
          dp[(i ^ 1) & 1][nxt][1][1] += sum0;
        } else {
          dp[(i ^ 1) & 1][nxt][1][0] += sum0;
        }

        dp[(i ^ 1) & 1][nxt][1][1] += sum1;
      }

      dp[(i ^ 1) & 1][nxt][1][0] %= mod;
      dp[(i ^ 1) & 1][nxt][1][1] %= mod;
    }
  }

  int ret = 0;

  for (int i = 0; i < maxbits; i++) {
    ret += dp[(H * W - 1) % 2][bits[i]][0][1];
    ret += dp[(H * W - 1) % 2][bits[i]][1][1];

    ret %= mod;
  }

  printf("%d\\n", ret);

  return 0;
}