Problem p02614 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  int r, c, k;
  cin >> r >> c >> k;
  char grid[r][c];
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      cin >> grid[i][j];
  int ans = 0;
  int rows = (1 << r) - 1;
  int cols = (1 << c) - 1;
  // cout << rows << " " << cols << endl;
  char cg[r][c];
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      memcpy(cg, grid, sizeof(cg));
      int bit = 0;
      int ci = i;
      vi to_do;
      while (ci > 0) {
        if (ci & 1) {
          to_do.pb(bit);
        }
        ci >>= 1;
        bit++;
      }
      for (int y = 0; y < (int)to_do.size(); y++) {
        for (int x = 0; x < c; x++)
          cg[to_do[y]][x] = 'r';
      }

      int cj = j;
      bit = 0;
      to_do.clear();
      while (cj > 0) {
        if (cj & 1)
          to_do.pb(bit);
        cj >>= 1;
        bit++;
      }

      for (int y = 0; y < (int)to_do.size(); y++) {
        for (int x = 0; x < r; x++)
          cg[x][to_do[y]] = 'r';
      }
      int cur = 0;
      for (int x = 0; x < r; x++) {
        for (int y = 0; y < c; y++) {
          if (cg[x][y] == '#')
            cur++;
        }
      }
      if (cur == k)
        ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  int r, c, k;
  cin >> r >> c >> k;
  string grid[r];
  for (int i = 0; i < r; i++)
    cin >> grid[i];
  int ans = 0;
  int rows = (1 << r) - 1;
  int cols = (1 << c) - 1;
  // cout << rows << " " << cols << endl;
  string cg[r];
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      memcpy(cg, grid, sizeof(cg));
      int bit = 0;
      int ci = i;
      vi to_do;
      while (ci > 0) {
        if (ci & 1) {
          to_do.pb(bit);
        }
        ci >>= 1;
        bit++;
      }
      for (int y = 0; y < to_do.size(); y++) {
        for (int x = 0; x < c; x++)
          cg[to_do[y]][x] = 'r';
      }

      int cj = j;
      bit = 0;
      to_do.clear();
      while (cj > 0) {
        if (cj & 1)
          to_do.pb(bit);
        cj >>= 1;
        bit++;
      }

      for (int y = 0; y < to_do.size(); y++) {
        for (int x = 0; x < r; x++)
          cg[x][to_do[y]] = 'r';
      }
      int cur = 0;
      for (int x = 0; x < r; x++) {
        for (int y = 0; y < c; y++) {
          if (cg[x][y] == '#')
            cur++;
        }
      }
      if (cur == k)
        ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}