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

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 10;

class P {
public:
  int x, y, kind;
  P(int x, int y, int kind) : x(x), y(y), kind(kind) {}
};

class B {
public:
  int board[N][N];
  B() { rep(i, N) rep(j, N) board[i][j] = 0; }
};

int color[3][5][5] = {{{0, 0, 0, 0, 0},
                       {0, 0, 1, 0, 0},
                       {0, 1, 1, 1, 0},
                       {0, 0, 1, 0, 0},
                       {0, 0, 0, 0, 0}},
                      {{0, 0, 0, 0, 0},
                       {0, 1, 1, 1, 0},
                       {0, 1, 1, 1, 0},
                       {0, 1, 1, 1, 0},
                       {0, 0, 0, 0, 0}},
                      {{0, 0, 1, 0, 0},
                       {0, 1, 1, 1, 0},
                       {1, 1, 1, 1, 1},
                       {0, 1, 1, 1, 0},
                       {0, 0, 1, 0, 0}}};

void put(B &b, P p, int sc = 1) {
  rep(i, 5) {
    rep(j, 5) {
      int nx = p.x + j - 2;
      int ny = p.y + i - 2;
      if (nx < 0 || ny < 0 || nx >= N || ny >= N)
        continue;
      if (color[p.kind][i][j] == 1) {
        b.board[ny][nx] -= sc;
      }
    }
  }
}

bool canput(B &b, P p) {
  rep(i, 5) {
    rep(j, 5) {
      int nx = p.x + j - 2;
      int ny = p.y + i - 2;

      if (color[p.kind][i][j] == 1) {
        if (nx < 0 || ny < 0 || nx >= N || ny >= N)
          return false;
        if (b.board[ny][nx] <= 0)
          return false;
      }
    }
  }
  return true;
}

void pboard(B &b) {
  rep(i, N) {
    rep(j, N) printf("%d ", b.board[i][j]);
    puts("");
  }
}

int n;
vector<P> ans;

void saiki(B &b, int x, int y, vector<P> temp) {

  /*
  printf("%d %d\\n",x,y);
  pboard(b);
  */
  if (x == N - 1 && y == N - 1) {

    ans = temp;
    if (ans.size() == 0)
      ans.push_back(P(-1, -1, -1));
    return;
  }

  if (b.board[y][x] != 0) {

    P cand[3] = {P(x, y + 1, 0), P(x + 1, y + 1, 1), P(x, y + 2, 2)};

    rep(i, 3) {
      if (!canput(b, cand[i]))
        continue;

      put(b, cand[i]);
      temp.push_back(cand[i]);

      saiki(b, x, y, temp);

      put(b, cand[i], -1);
      temp.pop_back();
    }
  } else {
    saiki(b, (x + 1) % N, y + (x + 1) / N, temp);
  }
}

int main() {

  B b;
  cin >> n;
  rep(i, N) rep(j, N) {
    int c;
    cin >> c;
    b.board[i][j] = c;
  }

  vector<P> temp;
  ans.clear();
  saiki(b, 0, 0, temp);

  if (ans.size() == 0) {
    puts("hoge");
  } else {
    if (ans[ans.size() - 1].kind == -1) {
      while (1) {
        ans.push_back(P(0, 0, 0));
        if (ans.size() == 0)
          break;
        ans.pop_back();
      }
    }
  }

  rep(i, ans.size()) {
    printf("%d %d %d\\n", ans[i].x, ans[i].y, ans[i].kind + 1);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 10;

class P {
public:
  int x, y, kind;
  P(int x, int y, int kind) : x(x), y(y), kind(kind) {}
};

class B {
public:
  int board[N][N];
  B() { rep(i, N) rep(j, N) board[i][j] = 0; }
};

int color[3][5][5] = {{{0, 0, 0, 0, 0},
                       {0, 0, 1, 0, 0},
                       {0, 1, 1, 1, 0},
                       {0, 0, 1, 0, 0},
                       {0, 0, 0, 0, 0}},
                      {{0, 0, 0, 0, 0},
                       {0, 1, 1, 1, 0},
                       {0, 1, 1, 1, 0},
                       {0, 1, 1, 1, 0},
                       {0, 0, 0, 0, 0}},
                      {{0, 0, 1, 0, 0},
                       {0, 1, 1, 1, 0},
                       {1, 1, 1, 1, 1},
                       {0, 1, 1, 1, 0},
                       {0, 0, 1, 0, 0}}};

void put(B &b, P p, int sc = 1) {
  rep(i, 5) {
    rep(j, 5) {
      int nx = p.x + j - 2;
      int ny = p.y + i - 2;
      if (color[p.kind][i][j] == 1) {
        b.board[ny][nx] -= sc;
      }
    }
  }
}

bool canput(B &b, P p) {
  rep(i, 5) {
    rep(j, 5) {
      int nx = p.x + j - 2;
      int ny = p.y + i - 2;

      if (color[p.kind][i][j] == 1) {
        if (nx < 0 || ny < 0 || nx >= N || ny >= N)
          return false;
        if (b.board[ny][nx] <= 0)
          return false;
      }
    }
  }
  return true;
}

void pboard(B &b) {
  rep(i, N) {
    rep(j, N) printf("%d ", b.board[i][j]);
    puts("");
  }
}

int n;
vector<P> ans;

void saiki(B &b, int x, int y, vector<P> temp) {

  /*
  printf("%d %d\\n",x,y);
  pboard(b);
  */
  if (x == N - 1 && y == N - 1) {

    ans = temp;
    if (ans.size() == 0)
      ans.push_back(P(-1, -1, -1));
    return;
  }

  if (b.board[y][x] != 0) {

    P cand[3] = {P(x, y + 1, 0), P(x + 1, y + 1, 1), P(x, y + 2, 2)};

    rep(i, 3) {
      if (!canput(b, cand[i]))
        continue;

      put(b, cand[i]);
      temp.push_back(cand[i]);

      saiki(b, x, y, temp);

      put(b, cand[i], -1);
      temp.pop_back();
    }
  } else {
    saiki(b, (x + 1) % N, y + (x + 1) / N, temp);
  }
}

int main() {

  B b;
  cin >> n;
  rep(i, N) rep(j, N) {
    char c;
    cin >> c;
    b.board[i][j] = c - '0';
  }

  vector<P> temp;
  ans.clear();
  saiki(b, 0, 0, temp);

  if (ans[0].kind == -1) {
    while (1) {
      ans.push_back(P(0, 0, 0));
      if (ans.size() == 0)
        break;
      ans.pop_back();
    }
  }

  rep(i, ans.size()) {
    printf("%d %d %d\\n", ans[i].x, ans[i].y, ans[i].kind + 1);
  }
}