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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
int A[10][10];
int N;
const int W = 10;
const int H = 10;
const int L = 13;

pair<int, int> next() {
  REP(y, H) REP(x, W) if (A[y][x] > 0) { return make_pair(x, y); }
  return make_pair(-1, -1);
}
int len[4] = {0, 5, 9, 13};
int dx[] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0};
int dy[] = {0, 0, 1, 0, -1, 1, -1, -1, 1, 0, 2, 0, -2};
int r[3] = {1, 1, 2};

int ok(int cx, int cy) {
  REP(i, L) {
    int x = cx + dx[i];
    int y = cy + dy[i];
    if (x < 0 || W <= x)
      return i;
    if (y < 0 || H <= y)
      return i;
    if (A[y][x] == 0)
      return i;
  }
  return L;
}
void add(int cx, int cy, int l, int r, int d) {
  for (int i = l; i < r; i++) {
    int x = cx + dx[i];
    int y = cy + dy[i];
    A[y][x] += d;
  }
}

int hist_x[15];
int hist_y[15];
int hist_z[15];

bool dfs(int iter) {
  if (iter == N) {
    if (next().first != -1)
      return false;
    REP(i, N) {
      int x = hist_x[i];
      int y = hist_y[i];
      int z = hist_z[i];
      cout << x << " " << y << " " << z << endl;
    }
    return true;
  }

  auto p = next();
  int px = p.first, py = p.second;
  // REP(y, H) {
  //     REP(x, W) cout << A[y][x] << " ";
  //     cout << endl;
  // }
  REP(k, 3) for (int i = len[k]; i < len[k + 1]; i++) {
    int cx = px - dx[i];
    int cy = py - dy[i];
    int r = ok(cx, cy);

    int max_k2 = -1;
    for (int k2 = 0; k2 < 3; k2++) {
      if (len[k2 + 1] <= r) {
        max_k2 = k2;
        add(cx, cy, len[k2], len[k2 + 1], -1);
        hist_x[iter] = cx;
        hist_y[iter] = cy;
        hist_z[iter] = k2 + 1;
        if (dfs(iter + 1))
          return true;
      } else {
        break;
      }
    }

    if (max_k2 >= 0) {
      add(cx, cy, len[0], len[max_k2 + 1], +1);
    }
  }

  return false;
}

int main() {
  while (cin >> N) {
    REP(y, H) REP(x, W) cin >> A[y][x];

    bool b = dfs(0);
    assert(b);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
int A[10][10];
int N;
const int W = 10;
const int H = 10;
const int L = 13;

int used[10][10];

pair<int, int> next() {
  REP(y, H) REP(x, W) if (A[y][x] > 0) { return make_pair(x, y); }
  return make_pair(-1, -1);
}
int len[4] = {0, 5, 9, 13};
int dx[] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0};
int dy[] = {0, 0, 1, 0, -1, 1, -1, -1, 1, 0, 2, 0, -2};
int r[3] = {1, 1, 2};

int ok(int cx, int cy) {
  if (used[cy][cx] > 0)
    return 0;
  REP(i, L) {
    int x = cx + dx[i];
    int y = cy + dy[i];
    if (x < 0 || W <= x)
      return i;
    if (y < 0 || H <= y)
      return i;
    if (A[y][x] == 0)
      return i;
  }
  return L;
}
void add(int cx, int cy, int l, int r, int d) {
  for (int i = l; i < r; i++) {
    int x = cx + dx[i];
    int y = cy + dy[i];
    A[y][x] += d;
  }
}

bool dfs(int iter) {
  if (iter == N) {
    if (next().first != -1)
      return false;
    REP(y, H) REP(x, W) if (used[y][x] > 0) {
      cout << x << " " << y << " " << used[y][x] << endl;
    }
    return true;
  }

  auto p = next();
  int px = p.first, py = p.second;
  // REP(y, H) {
  //     REP(x, W) cout << A[y][x] << " ";
  //     cout << endl;
  // }
  REP(k, 3) for (int i = len[k]; i < len[k + 1]; i++) {
    int cx = px - dx[i];
    int cy = py - dy[i];
    int r = ok(cx, cy);

    int max_k2 = -1;
    for (int k2 = 0; k2 < 3; k2++) {
      if (len[k2 + 1] <= r) {
        max_k2 = k2;
        add(cx, cy, len[k2], len[k2 + 1], -1);
        used[cy][cx] = k2 + 1;
        if (dfs(iter + 1))
          return true;
      } else {
        break;
      }
    }

    if (max_k2 >= 0) {
      add(cx, cy, len[0], len[max_k2 + 1], +1);
      used[cy][cx] = 0;
    }
  }

  return false;
}

int main() {
  while (cin >> N) {
    REP(y, H) REP(x, W) cin >> A[y][x];

    memset(used, 0, sizeof(used));
    bool b = dfs(0);
    assert(b);
  }
  return 0;
}