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

#include <algorithm>
#include <iostream>
using namespace std;

int W, H;
int board[10][10][3];
int R_end, C_end;

bool solve(int row, int column) {
  //	cout << "@" << row << "," << column << endl;
  int nex_row, nex_column;
  nex_row = row + 1;
  nex_column = column + nex_row / H;
  nex_row = nex_row % H;

  if (row >= R_end && column >= C_end && !(row == R_end && column == C_end)) {
    //		cout << "err" << endl;
    return false;
  }

  if (board[row + 1][column + 1][0] == 1) {
    if ((board[row][column + 1][1] & 2) || (board[row + 1][column][1] & 1))
      return false;
    board[row + 1][column + 1][1] = 0;
    board[row + 1][column + 1][2] = 0;
    return solve(nex_row, nex_column);
  } else {
    if ((board[row][column + 1][1] & 2) && (board[row + 1][column][1] & 1)) {
      //			cout << "@" << row << "," << column << ":";
      //			cout << board[row+1][column][2] << " " <<
      //board[row][column+1][2] << endl;
      if (row == R_end && column == C_end &&
          board[row + 1][column][2] == board[row][column + 1][2])
        return true;
      if ((row != R_end || column != C_end) &&
          board[row + 1][column][2] == board[row][column + 1][2])
        return false;

      board[row + 1][column + 1][1] = 0;

      int cp[10];
      for (int i = 0; i < H; i++)
        cp[i] = board[(row + i) % H + 1][column + (row + i) / H][2];

      int tmp = board[row + 1][column][2];
      for (int i = 0; i < H; i++) {
        if (board[(row + i) % H + 1][column + (row + i) / H][2] == tmp)
          board[(row + i) % H + 1][column + (row + i) / H][2] =
              board[row][column + 1][2];
      }
      board[row + 1][column + 1][2] = board[row][column + 1][2];
      if (solve(nex_row, nex_column))
        return true;

      for (int i = 0; i < H; i++)
        board[(row + i) % H + 1][column + (row + i) / H][2] = cp[i];
    } else if (board[row][column + 1][1] & 2) {
      board[row + 1][column + 1][2] = board[row][column + 1][2];
      for (int i = 0; i < 2; i++) {
        if (board[row + 1 + i][column + 1 + !i][0] == 0) {
          board[row + 1][column + 1][1] = i + 1;
          if (solve(nex_row, nex_column))
            return true;
        }
      }
    } else if (board[row + 1][column][1] & 1) {
      board[row + 1][column + 1][2] = board[row + 1][column][2];
      for (int i = 0; i < 2; i++) {
        if (board[row + 1 + i][column + 1 + !i][0] == 0) {
          board[row + 1][column + 1][1] = i + 1;
          if (solve(nex_row, nex_column))
            return true;
        }
      }
    } else {
      if (board[row + 2][column + 1][0] || board[row + 1][column + 2][0])
        return false;
      board[row + 1][column + 1][1] = 3;
      int mx = 0;
      for (int i = 0; i < H; i++)
        mx = max(mx, board[(row + i) % H + 1][column + (row + i) / H][2]);
      board[row + 1][column + 1][2] = mx + 1;
      return solve(nex_row, nex_column);
    }
  }

  return false;
}

int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        board[i][j][0] = 1;
        board[i][j][1] = 0;
        board[i][j][2] = 0;
      }
    }

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> board[i + 1][j + 1][0];

    R_end = C_end = 0;
    for (int i = 0; i < W; i++) {
      for (int j = 0; j < H; j++) {
        if (board[j + 1][i + 1][0] == 0) {
          R_end = j;
          C_end = i;
        }
      }
    }

    //		cout << "*" << R_end << " " << C_end << endl;

    if (solve(0, 0))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int W, H;
int board[10][10][3];
int R_end, C_end;

bool solve(int row, int column) {
  //	cout << "@" << row << "," << column << endl;
  int nex_row, nex_column;
  nex_row = row + 1;
  nex_column = column + nex_row / H;
  nex_row = nex_row % H;

  if (row >= R_end && column >= C_end && !(row == R_end && column == C_end)) {
    //		cout << "err" << endl;
    return false;
  }

  if (board[row + 1][column + 1][0] == 1) {
    if ((board[row][column + 1][1] & 2) || (board[row + 1][column][1] & 1))
      return false;
    board[row + 1][column + 1][1] = 0;
    board[row + 1][column + 1][2] = 0;
    return solve(nex_row, nex_column);
  } else {
    if ((board[row][column + 1][1] & 2) && (board[row + 1][column][1] & 1)) {
      //			cout << "@" << row << "," << column << ":";
      //			cout << board[row+1][column][2] << " " <<
      //board[row][column+1][2] << endl;
      if (row == R_end && column == C_end &&
          board[row + 1][column][2] == board[row][column + 1][2])
        return true;
      if ((row != R_end || column != C_end) &&
          board[row + 1][column][2] == board[row][column + 1][2])
        return false;

      board[row + 1][column + 1][1] = 0;

      int cp[10];
      for (int i = 0; i < H; i++)
        cp[i] = board[(row + i) % H + 1][column + (row + i) / H][2];

      int tmp = board[row + 1][column][2];
      for (int i = 0; i < H; i++) {
        if (board[(row + i) % H + 1][column + (row + i) / H][2] == tmp)
          board[(row + i) % H + 1][column + (row + i) / H][2] =
              board[row][column + 1][2];
      }
      board[row + 1][column + 1][2] = board[row][column + 1][2];
      if (solve(nex_row, nex_column))
        return true;

      for (int i = 0; i < H; i++)
        board[(row + i) % H + 1][column + (row + i) / H][2] = cp[i];
    } else if (board[row][column + 1][1] & 2) {
      board[row + 1][column + 1][2] = board[row][column + 1][2];
      for (int i = 0; i < 2; i++) {
        if (board[row + 1 + i][column + 1 + !i][0] == 0) {
          board[row + 1][column + 1][1] = i + 1;
          if (solve(nex_row, nex_column))
            return true;
        }
      }
    } else if (board[row + 1][column][1] & 1) {
      board[row + 1][column + 1][2] = board[row + 1][column][2];
      for (int i = 0; i < 2; i++) {
        if (board[row + 1 + i][column + 1 + !i][0] == 0) {
          board[row + 1][column + 1][1] = i + 1;
          if (solve(nex_row, nex_column))
            return true;
        }
      }
    } else {
      if (board[row + 2][column + 1][0] || board[row + 1][column + 2][0])
        return false;
      board[row + 1][column + 1][1] = 3;
      int mx = 0;
      for (int i = 0; i < H; i++)
        mx = max(mx, board[(row + i) % H + 1][column + (row + i) / H][2]);
      board[row + 1][column + 1][2] = mx + 1;
      return solve(nex_row, nex_column);
    }
  }

  return false;
}

int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        board[i][j][0] = 1;
        board[i][j][1] = 0;
        board[i][j][2] = 0;
      }
    }

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> board[i + 1][j + 1][0];

    for (int i = 0; i < W; i++) {
      for (int j = 0; j < H; j++) {
        if (board[j + 1][i + 1][0] == 0) {
          R_end = j;
          C_end = i;
        }
      }
    }

    //		cout << "*" << R_end << " " << C_end << endl;

    if (solve(0, 0))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}