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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

#define MAX_H (25)
#define MAX_W (25)
#define MAX_HW (MAX_H * MAX_W)

/* global variables */

int h, w, hw, d;
char mat[MAX_HW][MAX_HW], v[MAX_HW];

/* subroutines */

bool solve() {
  for (int i = 0; i < hw; i++) {
    if (mat[i][i] == 0) {
      for (int i0 = i + 1; i0 < hw; i0++) {
        if (mat[i0][i] != 0) {
          for (int j = 0; j < hw; j++) {
            int tmp = mat[i0][j];
            mat[i0][j] = mat[i][j];
            mat[i][j] = tmp;
          }
          int tmp = v[i0];
          v[i0] = v[i];
          v[i] = tmp;
          break;
        }
      }
    }

    if (mat[i][i] != 0) {
      for (int i0 = i + 1; i0 < hw; i0++) {
        if (mat[i0][i] != 0) {
          mat[i0][i] = 0;
          for (int j = i + 1; j < hw; j++)
            mat[i0][j] = (mat[i0][j] + mat[i][j]) & 1;
          v[i0] = (v[i0] + v[i]) & 1;
        }
      }
    }
  }
  // p mat; p v

  for (int i = hw - 1; i >= 0; i--) {
    for (int j = i + 1; j < hw; j++)
      v[i] = (v[i] + mat[i][j] * v[j]) & 1;
    if (mat[i][i] == 0 && v[i] != 0)
      return false;
  }
  // p mat; p v

  return true;
}

/* main */

int main() {
  for (;;) {
    cin >> w >> h >> d;
    if ((w | h | d) == 0)
      break;
    // printf("w=%d,h=%d,d=%d\\n", w, h, d);

    hw = h * w;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        int pos = y * w + x;
        int b;
        cin >> b;
        v[pos] = b;

        for (int j = 0; j < hw; j++)
          mat[pos][j] = 0;
        mat[pos][pos] = 1;

        for (int dy = -d; dy <= d; dy++) {
          int ddx = d - abs(dy);
          int dxs[] = {-ddx, ddx};

          for (int dxi = 0; dxi < 2; dxi++) {
            int dx = dxs[dxi];
            int x0 = x + dx;
            int y0 = y + dy;
            if (x0 >= 0 && x0 < w && y0 >= 0 && y0 < h) {
              int pos0 = y0 * w + x0;
              mat[pos][pos0] = 1;
            }
          }
        }
      }
    }
    // p v; p mat

    puts(solve() ? "1" : "0");
  }

  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

#define MAX_N (20)
#define MAX_NN (MAX_N * (MAX_N - 1) / 2)
#define MAX_D (400)
#define MAX_ND (MAX_N * MAX_D)

/* typedef */

typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;

/* global variables */

int n, nn;
int max_d;
int ps[MAX_N], ds[MAX_NN];
int dmap[MAX_ND];
vvi ans;

/* subroutines */

void push_ans() {
  vi *vp = new vi();
  for (int i = 1; i < n; i++)
    (*vp).push_back(ps[i] - ps[i - 1]);
  if (find(ans.begin(), ans.end(), *vp) == ans.end())
    ans.push_back(*vp);
}

bool check(int i0, int i1, int j, int &k, int *used) {
  int pj = ps[j];
  for (int i = i0; i <= i1; i++) {
    int d = abs(pj - ps[i]);
    if (!dmap[d])
      return false;
    dmap[d]--;
    used[k++] = d;
  }
  return true;
}

void rec(int i0, int i1) {
  if (false) {
    printf("(%d,%d): ", i0, i1);
    for (int i = 0; i < n; i++) {
      if (i > 0)
        putchar(',');
      printf("%d", ps[i]);
    }
    putchar('\\n');
    for (int d = 1; d <= ds[0]; d++)
      if (dmap[d] > 0)
        printf(" %d:%d", d, dmap[d]);
    putchar('\\n');
  }

  if (i0 + 1 == i1) {
    push_ans();
    return;
  }

  int prvd = max_d;
  while (dmap[max_d] == 0)
    max_d--;
  // printf(" max_d=%d\\n", max_d);

  int used[MAX_NN];
  int k = 0;

  ps[i0 + 1] = ps[n - 1] - max_d;
  if (check(0, i0, i0 + 1, k, used) && check(i1, n - 1, i0 + 1, k, used))
    rec(i0 + 1, i1);
  ps[i0 + 1] = 0;
  while (k > 0)
    dmap[used[--k]]++;

  ps[i1 - 1] = ps[0] + max_d;
  if (check(0, i0, i1 - 1, k, used) && check(i1, n - 1, i1 - 1, k, used))
    rec(i0, i1 - 1);
  ps[i1 - 1] = 0;
  while (k > 0)
    dmap[used[--k]]++;

  max_d = prvd;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    nn = n * (n - 1) / 2;
    memset(dmap, 0, sizeof(dmap));
    ans.clear();

    for (int i = 0; i < nn; i++) {
      cin >> ds[i];
      dmap[ds[i]]++;
    }

    memset(ps, 0, sizeof(ps));

    ps[0] = 0;
    ps[n - 1] = ds[0];
    dmap[ds[0]]--;
    max_d = ds[0];

    rec(0, n - 1);

    sort(ans.begin(), ans.end());

    for (int i = 0; i < ans.size(); i++) {
      vi &ai = ans[i];

      for (int j = 0; j < ai.size(); j++) {
        if (j > 0)
          cout << ' ';
        cout << ai[j];
      }
      cout << endl;
    }

    cout << "-----" << endl;
  }

  return 0;
}