Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int grid[10][1000], n, m, k;

vector<int> get(int x, int y) {
  vector<int> ans(m);

  for (int c = 0; c < m; c++) {
    for (int r = x; r < y; r++) {
      ans[c] += grid[r][c];
    }
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = s[j] - '0';
    }
  }

  int ans = n * m;

  for (int s = 0; s < (1 << (n - 1)); s++) {
    vector<vector<int>> rows;
    vector<int> hor = {0};

    for (int i = 0; i < n - 1; i++) {
      if (s >> i & 1) {
        hor.push_back(i + 1);
      }
    }

    hor.push_back(m);

    for (int i = 1; i < (int)hor.size(); i++) {
      rows.push_back(get(hor[i - 1], hor[i]));
    }

    int cur = __builtin_popcount(s);

    bool bad = false;
    for (auto &v : rows) {
      for (int x : v) {
        if (x > k) {
          bad = true;
        }
      }
    }
    if (bad)
      continue;

    vector<int> sum(rows.size());

    for (int i = 0; i < m; i++) {
      bool need = false;

      for (int j = 0; j < (int)rows.size(); j++) {
        if (sum[j] + rows[j][i] > k) {
          need = true;
        }
      }

      if (need) {
        cur++;
        for (int j = 0; j < (int)rows.size(); j++) {
          sum[j] = rows[j][i];
        }
      } else {
        for (int j = 0; j < (int)rows.size(); j++) {
          sum[j] += rows[j][i];
        }
      }
    }

    ans = min(ans, cur);
  }

  cout << ans << endl;
}


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

int grid[10][1000], n, m, k;

vector<int> get(int x, int y) {
  vector<int> ans(m);

  for (int c = 0; c < m; c++) {
    for (int r = x; r < y; r++) {
      ans[c] += grid[r][c];
    }
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = s[j] - '0';
    }
  }

  int ans = n * m;

  for (int s = 0; s < (1 << (n - 1)); s++) {
    vector<vector<int>> rows;
    vector<int> hor = {0};

    for (int i = 0; i < n - 1; i++) {
      if (s >> i & 1) {
        hor.push_back(i + 1);
      }
    }

    hor.push_back(n);

    for (int i = 1; i < (int)hor.size(); i++) {
      rows.push_back(get(hor[i - 1], hor[i]));
    }

    int cur = __builtin_popcount(s);

    bool bad = false;
    for (auto &v : rows) {
      for (int x : v) {
        if (x > k) {
          bad = true;
        }
      }
    }
    if (bad)
      continue;

    vector<int> sum(rows.size());

    for (int i = 0; i < m; i++) {
      bool need = false;

      for (int j = 0; j < (int)rows.size(); j++) {
        if (sum[j] + rows[j][i] > k) {
          need = true;
        }
      }

      if (need) {
        cur++;
        for (int j = 0; j < (int)rows.size(); j++) {
          sum[j] = rows[j][i];
        }
      } else {
        for (int j = 0; j < (int)rows.size(); j++) {
          sum[j] += rows[j][i];
        }
      }
    }

    ans = min(ans, cur);
  }

  cout << ans << endl;
}