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

#include "bits/stdc++.h"

using namespace std;

// #define int long long
#define PB push_back
#define MK make_pair
#define MKT make_tuple
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

// constexpr int INF = 1LL<<60;
constexpr int INF = 1 << 28;
constexpr int MOD = 100000;
constexpr int MAX_N = 20;
constexpr int dx[] = {-1, 0, 1, 0};
constexpr int dy[] = {0, -1, 0, 1};

int dp[2][1 << MAX_N][3][2], w, h, fld[MAX_N + 1][MAX_N + 1];
vector<int> gedo;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> h >> w;

  for (int i = 0; i < (1 << w); ++i) {
    bool pre = false, flg = false;
    for (int j = 0; j < w; ++j) {
      bool tmp = i & (1 << j);
      if (pre && tmp) {
        flg = true;
        break;
      }
      pre = tmp;
    }
    if (flg)
      continue;
    gedo.PB(i);
  }

  for (int y = 1; y <= h; ++y) {
    string s;
    cin >> s;
    for (int x = 1; x <= w; ++x) {
      int t = -1;
      switch (s[x - 1]) {
      case 'J':
        t = 0;
        break;
      case 'O':
        t = 1;
        break;
      case 'I':
        t = 2;
        break;
      }
      fld[x][y] = t;
    }
  }

  for (int i = fld[1][1] == -1 ? 0 : fld[1][1]; i < 3; ++i) {
    dp[0][0][i][0] = 1;
    if (fld[1][1] != -1)
      break;
  }
  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      int idx = ((y - 1) * w + x - 1) % 2;
      int nx, ny;
      if (x == w) {
        nx = 1;
        ny = y + 1;
      } else {
        nx = x + 1;
        ny = y;
      }
      for (int mask : gedo) {
        for (int cur = fld[x][y] == -1 ? 0 : fld[x][y]; cur < 3; ++cur) {
          for (int nxt = fld[nx][ny] == -1 ? 0 : fld[nx][ny]; nxt < 3; ++nxt) {
            int nok = (mask & 1) && cur == 2 && x != w;
            int nmask = (mask >> 1) +
                        ((cur == 0 && nxt == 1 && x != w) ? (1 << (w - 1)) : 0);
            // cerr << "x:" << x << "y:" << y << "mask:" << mask << endl;
            for (int ok = 0; ok < 2; ++ok) {
              dp[!idx][nmask][nxt][nok || ok] += dp[idx][mask][cur][ok];
              dp[!idx][nmask][nxt][nok || ok] %= MOD;
            }
            if (fld[nx][ny] != -1)
              break;
          }

          if (fld[x][y] != -1)
            break;
        }
      }

      // fill(dp[idx][0][0], dp[idx][1 << MAX_N][0], 0);
      memset(dp[idx], 0, sizeof(dp[idx]));
    }
  }

  int ans = 0, tar = (w * h) % 2;
  for (int i = 0; i < (1 << w); ++i) {
    for (int j = 0; j < 3; ++j) {
      ans += dp[tar][i][j][1];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

// #define int long long
#define PB push_back
#define MK make_pair
#define MKT make_tuple
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

// constexpr int INF = 1LL<<60;
constexpr int INF = 1 << 28;
constexpr int MOD = 100000;
constexpr int MAX_N = 20;
constexpr int dx[] = {-1, 0, 1, 0};
constexpr int dy[] = {0, -1, 0, 1};

int dp[2][1 << MAX_N][3][2], w, h, fld[MAX_N + 1][MAX_N + 1];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> h >> w;
  for (int y = 1; y <= h; ++y) {
    string s;
    cin >> s;
    for (int x = 1; x <= w; ++x) {
      int t = -1;
      switch (s[x - 1]) {
      case 'J':
        t = 0;
        break;
      case 'O':
        t = 1;
        break;
      case 'I':
        t = 2;
        break;
      }
      fld[x][y] = t;
    }
  }

  for (int i = fld[1][1] == -1 ? 0 : fld[1][1]; i < 3; ++i) {
    dp[0][0][i][0] = 1;
    if (fld[1][1] != -1)
      break;
  }
  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      int idx = ((y - 1) * w + x - 1) % 2;
      int nx, ny;
      if (x == w) {
        nx = 1;
        ny = y + 1;
      } else {
        nx = x + 1;
        ny = y;
      }
      for (int mask = 0; mask < (1 << w); ++mask) {
        for (int cur = fld[x][y] == -1 ? 0 : fld[x][y]; cur < 3; ++cur) {
          for (int nxt = fld[nx][ny] == -1 ? 0 : fld[nx][ny]; nxt < 3; ++nxt) {
            int nok = (mask & 1) && cur == 2 && x != w;
            int nmask = (mask >> 1) +
                        ((cur == 0 && nxt == 1 && x != w) ? (1 << (w - 1)) : 0);
            // cerr << "x:" << x << "y:" << y << "mask:" << mask << endl;
            for (int ok = 0; ok < 2; ++ok) {
              dp[!idx][nmask][nxt][nok || ok] += dp[idx][mask][cur][ok];
              dp[!idx][nmask][nxt][nok || ok] %= MOD;
            }
            if (fld[nx][ny] != -1)
              break;
          }

          if (fld[x][y] != -1)
            break;
        }
      }

      // fill(dp[idx][0][0], dp[idx][1 << MAX_N][0], 0);
      memset(dp[idx], 0, sizeof(dp[idx]));
    }
  }

  int ans = 0, tar = (w * h) % 2;
  for (int i = 0; i < (1 << w); ++i) {
    for (int j = 0; j < 3; ++j) {
      ans += dp[tar][i][j][1];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}