Problem p00482 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;

int _now[1 << 20], _next[1 << 20];

const int modBase = 100000;

int main() {
  char mapData[21][20];
  int w, h;
  scanf("%d%d", &h, &w);

  int *now = _now;
  int *next = _next;

  for (int i = 0; i < h; i++)
    scanf("%s", mapData[i]);
  for (int i = 1; i < (1 << w); i++)
    now[i] = 0;
  now[0] = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < (1 << w); k++)
        next[k] = 0;
      for (int k = 0; k < (1 << w); k++) {
        if (mapData[i][j] == 'J' || mapData[i][j] == '?') {
          int idx;
          if (j == 0) {
            idx = (1 << (w - 1)) | (k & (~(1)));
          } else {
            idx = (1 << (w - 1)) | (k & (~(3 << (j - 1))));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
        if (mapData[i][j] == 'O' || mapData[i][j] == '?') {
          int idx;
          if (j == 0) {
            idx = (~(1 << (w - 1))) & k & (~1);
          } else if (k & (1 << (w - 1))) {
            idx = (~(1 << (w - 1))) & k & (~(1 << j)) | (1 << (j - 1));
          } else {
            idx = (~(1 << (w - 1))) & k & (~(3 << (j - 1)));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
        if (mapData[i][j] == 'I' || mapData[i][j] == '?') {
          int idx = 0;
          if (j < w - 1 && k & (1 << j)) {
            continue;
          }
          if (j == 0) {
            idx = (~(1 << (w - 1))) & k & (~1);
          } else {
            idx = (~(1 << (w - 1))) & k & (~(3 << (j - 1)));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
      }
      swap(now, next);
    }
  }
  int ans = 0;
  for (int i = 0; i < (1 << w); i++) {
    ans = (ans + now[i]) % modBase;
  }
  int all = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (mapData[i][j] == '?')
        all = (all * 3) % modBase;
    }
  }
  printf("%d\\n", (all + modBase - ans) % modBase);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;

int _now[1 << 21], _next[1 << 21];

const int modBase = 100000;

int main() {
  char mapData[21][20];
  int w, h;
  scanf("%d%d", &h, &w);

  int *now = _now;
  int *next = _next;

  for (int i = 0; i < h; i++)
    scanf("%s", mapData[i]);
  for (int i = 1; i < (1 << (w + 1)); i++)
    now[i] = 0;
  now[0] = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < (1 << (w + 1)); k++)
        next[k] = 0;
      for (int k = 0; k < (1 << (w + 1)); k++) {
        if (mapData[i][j] == 'J' || mapData[i][j] == '?') {
          int idx;
          if (j == 0) {
            idx = (1 << (w - 1)) | (k & (~(1)));
          } else {
            idx = (1 << (w - 1)) | (k & (~(3 << (j - 1))));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
        if (mapData[i][j] == 'O' || mapData[i][j] == '?') {
          int idx;
          if (j == 0) {
            idx = (~(1 << (w - 1))) & k & (~1);
          } else if (k & (1 << (w - 1))) {
            idx = (~(1 << (w - 1))) & k & (~(1 << j)) | (1 << (j - 1));
          } else {
            idx = (~(1 << (w - 1))) & k & (~(3 << (j - 1)));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
        if (mapData[i][j] == 'I' || mapData[i][j] == '?') {
          int idx = 0;
          if (j < w - 1 && k & (1 << j)) {
            idx = (1 << w);
          }
          if (j == 0) {
            idx = idx | ((~(1 << (w - 1))) & k & (~1));
          } else {
            idx = idx | ((~(1 << (w - 1))) & k & (~(3 << (j - 1))));
          }
          next[idx] += now[k];
          next[idx] %= modBase;
        }
      }
      swap(now, next);
    }
  }
  int ans = 0;
  for (int i = (1 << w); i < (1 << (w + 1)); i++) {
    ans = (ans + now[i]) % modBase;
  }
  printf("%d\\n", ans);
}