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

#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int H, W;
char data[20][20];

int dp[2][2][2][1 << 19];
/*
int dfs(int pos , int j , int f , int bit){
        //cout << pos << " " << j << " " << f << " " << bit << endl;
        if(pos == H*W) return f;
        if(memo[pos][j][f].find(bit) != memo[pos][j][f].end() ) return
memo[pos][j][f][bit]; int ans = 0; int x = pos % W; int y = pos / W; if(f){
                if(data[y][x] == '?') ans += 3 * dfs( pos+1 , 0 , 1 , 0);
                else ans += dfs(pos+1,0,1,0);
        }else{
                if(data[y][x] == '?'){
                        if(j) ans += dfs( pos+1 , 0 , 0 , (bit>>1)|(1<<(W-2)) );
                        else ans += dfs(pos+1,0,0,bit>>1);

                        if(x != W-1) ans += dfs(pos+1,1,0,bit>>1);
                        else 	     ans += dfs(pos+1,0,0,bit>>1);

                        if(bit&1) ans += dfs(pos+1,0,1,0);
                        else	  ans += dfs(pos+1,0,0,bit>>1);

                }else{
                        if(j && data[y][x] == 'O') ans += dfs( pos+1 , 0 , 0 ,
(bit>>1)|(1<<(W-2)) ); else if(data[y][x] == 'J' && x != W-1) ans +=
dfs(pos+1,1,0,bit>>1); else if(data[y][x] == 'I' && (bit&1) ) ans +=
dfs(pos+1,0,1,0); else ans += dfs(pos+1,0,0,bit>>1);
                }
        }
        return memo[pos][j][f][bit] = ans % 100000;
}
*/
int main() {
  while (cin >> H >> W && H) {
    vector<int> c;
    rep(i, H) rep(j, W) cin >> data[i][j];
    rep(i, 1 << (W - 1)) {
      bool f = true;
      rep(j, W) {
        if ((i >> (j + 1) & 1) && ((i >> j) & 1)) {
          f = false;
          break;
        }
      }

      if (f)
        c.push_back(i);
    }

    int N = H * W, BIT = c.size();
    dp[0][0][0][0] = 1;
    for (int pos = 0; pos < N; pos++) {
      int x = pos % W;
      int y = pos / W;
      // cout << data[y][x] << endl;
      for (int b = 0; b < c.size(); b++) {
        int bit = c[b];
        for (int j = 0; j < 2; j++) {
          for (int f = 0; f < 2; f++) {
            dp[(pos + 1) & 1][j][f][bit] = 0;
          }
        }
      }
      for (int b = 0; b < c.size(); b++) {
        int bit = c[b];
        for (int j = 0; j < 2; j++) {
          for (int f = 0; f < 2; f++) {
            dp[pos & 1][j][f][bit] %= 100000;
            // if(x == 1 && y) cout << dp[pos&1][j][f][bit] << endl;
            if (f) {
              if (data[y][x] == '?')
                dp[(pos + 1) & 1][0][1][0] += 3 * dp[pos & 1][j][f][bit];
              else
                dp[(pos + 1) & 1][0][1][0] += dp[pos & 1][j][f][bit];
            } else {
              if (data[y][x] == '?') {
                if (j)
                  dp[(pos + 1) & 1][0][0][(bit >> 1) | (1 << (W - 2))] +=
                      dp[pos & 1][j][f][bit];
                else
                  dp[(pos + 1) & 1][0][0][bit >> 1] += dp[pos & 1][j][f][bit];

                if (x != W - 1)
                  dp[(pos + 1) & 1][1][0][bit >> 1] += dp[pos & 1][j][f][bit];
                else
                  dp[(pos + 1) & 1][0][0][bit >> 1] += dp[pos & 1][j][f][bit];

                if (bit & 1)
                  dp[(pos + 1) & 1][0][1][0] += dp[pos & 1][j][f][bit];
                else
                  dp[(pos + 1) & 1][0][0][bit >> 1] += dp[pos & 1][j][f][bit];

              } else {
                if (j && data[y][x] == 'O')
                  dp[(pos + 1) & 1][0][0][(bit >> 1) | (1 << (W - 2))] +=
                      dp[pos & 1][j][f][bit];
                else if (data[y][x] == 'J' && x != W - 1)
                  dp[(pos + 1) & 1][1][0][bit >> 1] += dp[pos & 1][j][f][bit];
                else if (data[y][x] == 'I' && (bit & 1))
                  dp[(pos + 1) & 1][0][1][0] += dp[pos & 1][j][f][bit];
                else
                  dp[(pos + 1) & 1][0][0][bit >> 1] += dp[pos & 1][j][f][bit];
              }
            }
          }
        }
      }
    }
    cout << dp[N & 1][0][1][0] % 100000 << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int H, W;
char data[20][20];

map<int, int> memo[400][2][2];

int dfs(int pos, int j, int f, int bit) {
  // cout << pos << " " << j << " " << f << " " << bit << endl;
  if (pos == H * W)
    return f;
  if (memo[pos][j][f].find(bit) != memo[pos][j][f].end())
    return memo[pos][j][f][bit];
  int ans = 0;
  int x = pos % W;
  int y = pos / W;
  if (f) {
    if (data[y][x] == '?')
      ans += 3 * dfs(pos + 1, 0, 1, 0);
    else
      ans += dfs(pos + 1, 0, 1, 0);
  } else {
    if (data[y][x] == '?') {
      if (j)
        ans += dfs(pos + 1, 0, 0, (bit >> 1) | (1 << (W - 2)));
      else
        ans += dfs(pos + 1, 0, 0, bit >> 1);

      if (x != W - 1)
        ans += dfs(pos + 1, 1, 0, bit >> 1);
      else
        ans += dfs(pos + 1, 0, 0, bit >> 1);

      if (bit & 1)
        ans += dfs(pos + 1, 0, 1, 0);
      else
        ans += dfs(pos + 1, 0, 0, bit >> 1);

    } else {
      if (j && data[y][x] == 'O')
        ans += dfs(pos + 1, 0, 0, (bit >> 1) | (1 << (W - 2)));
      else if (data[y][x] == 'J' && x != W - 1)
        ans += dfs(pos + 1, 1, 0, bit >> 1);
      else if (data[y][x] == 'I' && (bit & 1))
        ans += dfs(pos + 1, 0, 1, 0);
      else
        ans += dfs(pos + 1, 0, 0, bit >> 1);
    }
  }
  return memo[pos][j][f][bit] = ans % 100000;
}
int main() {

  while (cin >> H >> W && H) {
    rep(i, H) rep(j, W) cin >> data[i][j];
    cout << dfs(0, 0, 0, 0) << endl;
  }
}