Problem p02895 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

struct Debug {
  int cnt = 0;
  bool on = false;
  char debug[10] = "debug";
  template <class T> Debug &operator<<(const T &v) {
    if (on && cnt++ == 0)
      cerr << "debug: ";
    if (on)
      cerr << v << ' ';
    return *this;
  }
  Debug &operator<<(ostream &(*pManip)(ostream &)) {
    if (on)
      cerr << (*pManip);
    cnt = 0;
    return *this;
  }
} debug;
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    if (debug.on) {                                                            \
      for (auto x : v)                                                         \
        debug << x;                                                            \
      debug << endl;                                                           \
    }                                                                          \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    if (debug.on) {                                                            \
      for (int _i = 0; _i < n; _i++)                                           \
        debug << *(v + _i);                                                    \
      debug << endl;                                                           \
    }                                                                          \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 20 + 10;
int n;
string a[MAXN];
long long dp[MAXN][MAXN][MAXN];

long long dp_cal(int i, int j, int k) {
  if (dp[i][j][k] >= 0)
    return dp[i][j][k];
  if (i == j && k == j)
    return dp[i][j][k] = 1;
  // if(i + 1 >= j || k - 1 <= j) return dp[i][j][k] = 0;
  long long res = 0;
  for (int ii = i; ii < j; ii++) {
    for (int jj = k; jj > j; jj--) {
      if (a[ii][jj] == '0')
        continue;
      for (int iii = ii; iii < j; iii++) {
        for (int jjj = jj; jjj > j; jjj--) {
          res += dp_cal(i, ii, iii) * dp_cal(iii + 1, j, jjj - 1) *
                 dp_cal(jjj, jj, k);
        }
      }
    }
  }
  // debug << i << j << k << res << endl;
  return dp[i][j][k] = res;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  if (argc > 1 && !strcmp(argv[1], debug.debug))
    debug.on = true;

  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        dp[i][j][k] = -1;

  long long res = 0;
  for (int i = 1; i <= n - 1; i++) {
    // debug << 0 << i << n - 1 << endl;
    if (a[0][i] == '1')
      res += dp_cal(1, i, n - 1);
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

struct Debug {
  int cnt = 0;
  bool on = false;
  char debug[10] = "debug";
  template <class T> Debug &operator<<(const T &v) {
    if (on && cnt++ == 0)
      cerr << "debug: ";
    if (on)
      cerr << v << ' ';
    return *this;
  }
  Debug &operator<<(ostream &(*pManip)(ostream &)) {
    if (on)
      cerr << (*pManip);
    cnt = 0;
    return *this;
  }
} debug;
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    if (debug.on) {                                                            \
      for (auto x : v)                                                         \
        debug << x;                                                            \
      debug << endl;                                                           \
    }                                                                          \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    if (debug.on) {                                                            \
      for (int _i = 0; _i < n; _i++)                                           \
        debug << *(v + _i);                                                    \
      debug << endl;                                                           \
    }                                                                          \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 40 + 5;
int n;
string a[MAXN];
long long dp[MAXN][MAXN][MAXN];

long long dp_cal(int i, int j, int k) {
  if (dp[i][j][k] >= 0)
    return dp[i][j][k];
  if (i == j && k == j)
    return dp[i][j][k] = 1;
  // if(i + 1 >= j || k - 1 <= j) return dp[i][j][k] = 0;
  long long res = 0;
  for (int ii = i; ii < j; ii++) {
    for (int jj = k; jj > j; jj--) {
      if (a[ii][jj] == '0')
        continue;
      for (int iii = ii; iii < j; iii++) {
        for (int jjj = jj; jjj > j; jjj--) {
          res += dp_cal(i, ii, iii) * dp_cal(iii + 1, j, jjj - 1) *
                 dp_cal(jjj, jj, k);
        }
      }
    }
  }
  // debug << i << j << k << res << endl;
  return dp[i][j][k] = res;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  if (argc > 1 && !strcmp(argv[1], debug.debug))
    debug.on = true;

  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        dp[i][j][k] = -1;

  long long res = 0;
  for (int i = 1; i <= n - 1; i++) {
    // debug << 0 << i << n - 1 << endl;
    if (a[0][i] == '1')
      res += dp_cal(1, i, n - 1);
  }
  cout << res << endl;

  return 0;
}