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

#include <bits/stdc++.h>
using namespace std;

int ans[21][21];

int main() {
  ans[1][1] = 0;
  ans[1][1] = 0;
  ans[1][2] = 1;
  ans[2][1] = 1;
  ans[1][3] = 0;
  ans[3][1] = 0;
  ans[1][4] = 1;
  ans[4][1] = 1;
  ans[1][5] = 0;
  ans[5][1] = 0;
  ans[1][6] = 1;
  ans[6][1] = 1;
  ans[1][7] = 0;
  ans[7][1] = 0;
  ans[1][8] = 1;
  ans[8][1] = 1;
  ans[1][9] = 0;
  ans[9][1] = 0;
  ans[1][10] = 1;
  ans[10][1] = 1;
  ans[1][11] = 0;
  ans[11][1] = 0;
  ans[1][12] = 1;
  ans[12][1] = 1;
  ans[1][13] = 0;
  ans[13][1] = 0;
  ans[1][14] = 1;
  ans[14][1] = 1;
  ans[1][15] = 0;
  ans[15][1] = 0;
  ans[1][16] = 1;
  ans[16][1] = 1;
  ans[1][17] = 0;
  ans[17][1] = 0;
  ans[1][18] = 1;
  ans[18][1] = 1;
  ans[1][19] = 0;
  ans[19][1] = 0;
  ans[1][20] = 1;
  ans[20][1] = 1;
  ans[2][2] = 2;
  ans[2][2] = 2;
  ans[2][3] = 3;
  ans[3][2] = 3;
  ans[2][4] = 4;
  ans[4][2] = 4;
  ans[2][5] = 6;
  ans[5][2] = 6;
  ans[2][6] = 9;
  ans[6][2] = 9;
  ans[2][7] = 13;
  ans[7][2] = 13;
  ans[2][8] = 19;
  ans[8][2] = 19;
  ans[2][9] = 28;
  ans[9][2] = 28;
  ans[2][10] = 41;
  ans[10][2] = 41;
  ans[2][11] = 60;
  ans[11][2] = 60;
  ans[2][12] = 88;
  ans[12][2] = 88;
  ans[2][13] = 129;
  ans[13][2] = 129;
  ans[2][14] = 189;
  ans[14][2] = 189;
  ans[2][15] = 277;
  ans[15][2] = 277;
  ans[2][16] = 406;
  ans[16][2] = 406;
  ans[2][17] = 595;
  ans[17][2] = 595;
  ans[2][18] = 872;
  ans[18][2] = 872;
  ans[2][19] = 1278;
  ans[19][2] = 1278;
  ans[2][20] = 1873;
  ans[20][2] = 1873;
  ans[3][3] = 0;
  ans[3][3] = 0;
  ans[3][4] = 4;
  ans[4][3] = 4;
  ans[3][5] = 0;
  ans[5][3] = 0;
  ans[3][6] = 6;
  ans[6][3] = 6;
  ans[3][7] = 0;
  ans[7][3] = 0;
  ans[3][8] = 10;
  ans[8][3] = 10;
  ans[3][9] = 0;
  ans[9][3] = 0;
  ans[3][10] = 16;
  ans[10][3] = 16;
  ans[3][11] = 0;
  ans[11][3] = 0;
  ans[3][12] = 26;
  ans[12][3] = 26;
  ans[3][13] = 0;
  ans[13][3] = 0;
  ans[3][14] = 42;
  ans[14][3] = 42;
  ans[3][15] = 0;
  ans[15][3] = 0;
  ans[3][16] = 68;
  ans[16][3] = 68;
  ans[3][17] = 0;
  ans[17][3] = 0;
  ans[3][18] = 110;
  ans[18][3] = 110;
  ans[3][19] = 0;
  ans[19][3] = 0;
  ans[3][20] = 178;
  ans[20][3] = 178;
  ans[4][4] = 2;
  ans[4][4] = 2;
  ans[4][5] = 3;
  ans[5][4] = 3;
  ans[4][6] = 3;
  ans[6][4] = 3;
  ans[4][7] = 3;
  ans[7][4] = 3;
  ans[4][8] = 5;
  ans[8][4] = 5;
  ans[4][9] = 5;
  ans[9][4] = 5;
  ans[4][10] = 6;
  ans[10][4] = 6;
  ans[4][11] = 8;
  ans[11][4] = 8;
  ans[4][12] = 8;
  ans[12][4] = 8;
  ans[4][13] = 11;
  ans[13][4] = 11;
  ans[4][14] = 13;
  ans[14][4] = 13;
  ans[4][15] = 14;
  ans[15][4] = 14;
  ans[4][16] = 19;
  ans[16][4] = 19;
  ans[4][17] = 21;
  ans[17][4] = 21;
  ans[4][18] = 25;
  ans[18][4] = 25;
  ans[4][19] = 32;
  ans[19][4] = 32;
  ans[4][20] = 35;
  ans[20][4] = 35;
  ans[5][5] = 0;
  ans[5][5] = 0;
  ans[5][6] = 2;
  ans[6][5] = 2;
  ans[5][7] = 0;
  ans[7][5] = 0;
  ans[5][8] = 2;
  ans[8][5] = 2;
  ans[5][9] = 0;
  ans[9][5] = 0;
  ans[5][10] = 4;
  ans[10][5] = 4;
  ans[5][11] = 0;
  ans[11][5] = 0;
  ans[5][12] = 4;
  ans[12][5] = 4;
  ans[5][13] = 0;
  ans[13][5] = 0;
  ans[5][14] = 6;
  ans[14][5] = 6;
  ans[5][15] = 0;
  ans[15][5] = 0;
  ans[5][16] = 8;
  ans[16][5] = 8;
  ans[5][17] = 0;
  ans[17][5] = 0;
  ans[5][18] = 10;
  ans[18][5] = 10;
  ans[5][19] = 0;
  ans[19][5] = 0;
  ans[5][20] = 14;
  ans[20][5] = 14;
  ans[6][6] = 2;
  ans[6][6] = 2;
  ans[6][7] = 2;
  ans[7][6] = 2;
  ans[6][8] = 1;
  ans[8][6] = 1;
  ans[6][9] = 1;
  ans[9][6] = 1;
  ans[6][10] = 2;
  ans[10][6] = 2;
  ans[6][11] = 3;
  ans[11][6] = 3;
  ans[6][12] = 4;
  ans[12][6] = 4;
  ans[6][13] = 3;
  ans[13][6] = 3;
  ans[6][14] = 3;
  ans[14][6] = 3;
  ans[6][15] = 3;
  ans[15][6] = 3;
  ans[6][16] = 4;
  ans[16][6] = 4;
  ans[6][17] = 6;
  ans[17][6] = 6;
  ans[6][18] = 6;
  ans[18][6] = 6;
  ans[6][19] = 7;
  ans[19][6] = 7;
  ans[6][20] = 6;
  ans[20][6] = 6;
  ans[7][7] = 0;
  ans[7][7] = 0;
  ans[7][8] = 2;
  ans[8][7] = 2;
  ans[7][9] = 0;
  ans[9][7] = 0;
  ans[7][10] = 0;
  ans[10][7] = 0;
  ans[7][11] = 0;
  ans[11][7] = 0;
  ans[7][12] = 2;
  ans[12][7] = 2;
  ans[7][13] = 0;
  ans[13][7] = 0;
  ans[7][14] = 4;
  ans[14][7] = 4;
  ans[7][15] = 0;
  ans[15][7] = 0;
  ans[7][16] = 2;
  ans[16][7] = 2;
  ans[7][17] = 0;
  ans[17][7] = 0;
  ans[7][18] = 2;
  ans[18][7] = 2;
  ans[7][19] = 0;
  ans[19][7] = 0;
  ans[7][20] = 6;
  ans[20][7] = 6;
  ans[8][8] = 2;
  ans[8][8] = 2;
  ans[8][9] = 2;
  ans[9][8] = 2;
  ans[8][10] = 1;
  ans[10][8] = 1;
  ans[8][11] = 0;
  ans[11][8] = 0;
  ans[8][12] = 0;
  ans[12][8] = 0;
  ans[8][13] = 1;
  ans[13][8] = 1;
  ans[8][14] = 2;
  ans[14][8] = 2;
  ans[8][15] = 3;
  ans[15][8] = 3;
  ans[8][16] = 4;
  ans[16][8] = 4;
  ans[8][17] = 3;
  ans[17][8] = 3;
  ans[8][18] = 2;
  ans[18][8] = 2;
  ans[8][19] = 1;
  ans[19][8] = 1;
  ans[8][20] = 1;
  ans[20][8] = 1;
  ans[9][9] = 0;
  ans[9][9] = 0;
  ans[9][10] = 2;
  ans[10][9] = 2;
  ans[9][11] = 0;
  ans[11][9] = 0;
  ans[9][12] = 0;
  ans[12][9] = 0;
  ans[9][13] = 0;
  ans[13][9] = 0;
  ans[9][14] = 0;
  ans[14][9] = 0;
  ans[9][15] = 0;
  ans[15][9] = 0;
  ans[9][16] = 2;
  ans[16][9] = 2;
  ans[9][17] = 0;
  ans[17][9] = 0;
  ans[9][18] = 4;
  ans[18][9] = 4;
  ans[9][19] = 0;
  ans[19][9] = 0;
  ans[9][20] = 2;
  ans[20][9] = 2;
  ans[10][10] = 2;
  ans[10][10] = 2;
  ans[10][11] = 2;
  ans[11][10] = 2;
  ans[10][12] = 1;
  ans[12][10] = 1;
  ans[10][13] = 0;
  ans[13][10] = 0;
  ans[10][14] = 0;
  ans[14][10] = 0;
  ans[10][15] = 0;
  ans[15][10] = 0;
  ans[10][16] = 0;
  ans[16][10] = 0;
  ans[10][17] = 1;
  ans[17][10] = 1;
  ans[10][18] = 2;
  ans[18][10] = 2;
  ans[10][19] = 3;
  ans[19][10] = 3;
  ans[10][20] = 4;
  ans[20][10] = 4;
  ans[11][11] = 0;
  ans[11][11] = 0;
  ans[11][12] = 2;
  ans[12][11] = 2;
  ans[11][13] = 0;
  ans[13][11] = 0;
  ans[11][14] = 0;
  ans[14][11] = 0;
  ans[11][15] = 0;
  ans[15][11] = 0;
  ans[11][16] = 0;
  ans[16][11] = 0;
  ans[11][17] = 0;
  ans[17][11] = 0;
  ans[11][18] = 0;
  ans[18][11] = 0;
  ans[11][19] = 0;
  ans[19][11] = 0;
  ans[11][20] = 2;
  ans[20][11] = 2;
  ans[12][12] = 2;
  ans[12][12] = 2;
  ans[12][13] = 2;
  ans[13][12] = 2;
  ans[12][14] = 1;
  ans[14][12] = 1;
  ans[12][15] = 0;
  ans[15][12] = 0;
  ans[12][16] = 0;
  ans[16][12] = 0;
  ans[12][17] = 0;
  ans[17][12] = 0;
  ans[12][18] = 0;
  ans[18][12] = 0;
  ans[12][19] = 0;
  ans[19][12] = 0;
  ans[12][20] = 0;
  ans[20][12] = 0;
  ans[13][13] = 0;
  ans[13][13] = 0;
  ans[13][14] = 2;
  ans[14][13] = 2;
  ans[13][15] = 0;
  ans[15][13] = 0;
  ans[13][16] = 0;
  ans[16][13] = 0;
  ans[13][17] = 0;
  ans[17][13] = 0;
  ans[13][18] = 0;
  ans[18][13] = 0;
  ans[13][19] = 0;
  ans[19][13] = 0;
  ans[13][20] = 0;
  ans[20][13] = 0;
  ans[14][14] = 2;
  ans[14][14] = 2;
  ans[14][15] = 2;
  ans[15][14] = 2;
  ans[14][16] = 1;
  ans[16][14] = 1;
  ans[14][17] = 0;
  ans[17][14] = 0;
  ans[14][18] = 0;
  ans[18][14] = 0;
  ans[14][19] = 0;
  ans[19][14] = 0;
  ans[14][20] = 0;
  ans[20][14] = 0;
  ans[15][15] = 0;
  ans[15][15] = 0;
  ans[15][16] = 2;
  ans[16][15] = 2;
  ans[15][17] = 0;
  ans[17][15] = 0;
  ans[15][18] = 0;
  ans[18][15] = 0;
  ans[15][19] = 0;
  ans[19][15] = 0;
  ans[15][20] = 0;
  ans[20][15] = 0;
  ans[16][16] = 2;
  ans[16][16] = 2;
  ans[16][17] = 2;
  ans[17][16] = 2;
  ans[16][18] = 1;
  ans[18][16] = 1;
  ans[16][19] = 0;
  ans[19][16] = 0;
  ans[16][20] = 0;
  ans[20][16] = 0;
  ans[17][17] = 0;
  ans[17][17] = 0;
  ans[17][18] = 2;
  ans[18][17] = 2;
  ans[17][19] = 0;
  ans[19][17] = 0;
  ans[17][20] = 0;
  ans[20][17] = 0;
  ans[18][18] = 2;
  ans[18][18] = 2;
  ans[18][19] = 2;
  ans[19][18] = 2;
  ans[18][20] = 1;
  ans[20][18] = 1;
  ans[19][19] = 0;
  ans[19][19] = 0;
  ans[19][20] = 2;
  ans[20][19] = 2;
  ans[20][20] = 2;
  ans[20][20] = 2;

  for (int H, W; cin >> H >> W && (H | W);) {
    cout << ans[H][W] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long lli;

lli H, W;
map<deque<lli>, lli> dp[2];

int main() {
  while (cin >> H >> W && (H | W)) {
    dp[0].clear();
    dp[1].clear();
    dp[0][deque<lli>(W + 1, 0)] = 1;
    lli now = 0, next = 1;
    for (lli i = 0; i < H; ++i) {
      for (lli j = 0; j < W; ++j) {
        for (map<deque<lli>, lli>::iterator it = dp[now].begin();
             it != dp[now].end(); ++it) {
          deque<lli> d = it->first;
          lli num = it->second;
          if (d[1] == 1 && d[W] == 2)
            continue;
          if (d[1] == 1 || d[W] == 2) {
            d.pop_front();
            d.push_back(0);
            dp[next][d] += num;
            continue;
          }
          if (i && j && !d[0]) {
            continue;
          }
          if (i + 1 != H) {
            lli tmp = d.front();
            d.pop_front();
            d.push_back(1);
            dp[next][d] += num;
            d.push_front(tmp);
            d.pop_back();
          }
          if (j + 1 != W) {
            lli tmp = d.front();
            d.pop_front();
            d.push_back(2);
            dp[next][d] += num;
            d.push_front(tmp);
            d.pop_back();
          }
        }
        swap(now, next);
        dp[next].clear();
      }
    }
    lli res = 0;
    for (map<deque<lli>, lli>::iterator it = dp[now].begin();
         it != dp[now].end(); ++it) {
      res += it->second;
    }
    cout << res << endl;
  }
  return 0;
}