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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int m, n;
struct point {
  int x, y;
  int status; // 状态，运动或静止
};
point s, goal;
int MinStep;
int board[22][22];
void dfs(int i, int j, bool status, bool flag, int direction, int step) {
  // direction 为当前方向 0: east;1: west;2: south; 3: north
  // flag为是否消除block 0: 消除
  // status 当前状态 0: 静止
  if (step > 10) { //
    return;
  }
  if (board[i][j] == 3) {
    if (MinStep > step) {
      MinStep = step;
      return;
    }
  }
  if (!flag) {
    switch (direction) {
    case 0:
      board[i][j + 1] = 0;
      break;
    case 1:
      board[i][j - 1] = 0;
      break;
    case 2:
      board[i + 1][j] = 0;
      break;
    case 3:
      board[i - 1][j] = 0;
      break;
    }
  }
  if (!status) { // 静止，选择方向
    if (j + 1 < m && (board[i][j + 1] == 0 || board[i][j + 1] == 3)) // east
      dfs(i, j + 1, true, true, 0, step + 1);
    if (j - 1 >= 0 && (board[i][j - 1] == 0 || board[i][j - 1] == 3)) // west
      dfs(i, j - 1, true, true, 1, step + 1);
    if (i + 1 < n && (board[i + 1][j] == 0 || board[i + 1][j] == 3)) // south
      dfs(i + 1, j, true, true, 2, step + 1);
    if (i - 1 >= 0 && (board[i - 1][j] == 0 || board[i - 1][j] == 3)) // north
      dfs(i - 1, j, true, true, 3, step + 1);
  } else if (status) { // 运动
    switch (direction) {
    case 0:              // east
      if (j + 1 > m - 1) // 越界
        return;
      if (board[i][j + 1] == 0) // 下一位置为0，继续运动
        dfs(i, j + 1, true, true, 0, step);
      if (board[i][j + 1] == 1) // 下一位置为1，停止运动，消除石头
        dfs(i, j, false, false, 0, step);
      if (board[i][j + 1] == 3) // 下一位置为终点，停止运动，游戏结束
        dfs(i, j + 1, false, true, 0, step);
      break;
    case 1: // west
      if (j - 1 < 0)
        return;
      if (board[i][j - 1] == 0)
        dfs(i, j - 1, true, true, 1, step);
      if (board[i][j - 1] == 1)
        dfs(i, j, false, false, 1, step);
      if (board[i][j - 1] == 3)
        dfs(i, j - 1, false, true, 1, step);
      break;
    case 2: // south
      if (i + 1 > n - 1)
        return;
      if (board[i + 1][j] == 0)
        dfs(i + 1, j, true, true, 2, step);
      if (board[i + 1][j] == 1)
        dfs(i, j, false, false, 2, step);
      if (board[i + 1][j] == 3)
        dfs(i + 1, j, false, true, 2, step);
      break;
    case 3: // north
      if (i - 1 < 0)
        return;
      if (board[i - 1][j] == 0)
        dfs(i - 1, j, true, true, 3, step);
      if (board[i - 1][j] == 1)
        dfs(i, j, false, false, 3, step);
      if (board[i - 1][j] == 3)
        dfs(i - 1, j, false, true, 3, step);
      break;
    }
  }
  if (!flag) { // 恢复消除的石头
    switch (direction) {
    case 0:
      board[i][j + 1] = 1;
      break;
    case 1:
      board[i][j - 1] = 1;
      break;
    case 2:
      board[i + 1][j] = 1;
      break;
    case 3:
      board[i - 1][j] = 1;
      break;
    }
  }
  return;
}
int main() {
  while (cin >> m >> n && (m + n)) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        cin >> board[i][j];
        if (board[i][j] == 2) { // 起点
          s.x = i;
          s.y = j;
          s.status = false;
          board[i][j] = 0;
        }
        if (board[i][j] == 3) { // 终点
          goal.x = i;
          goal.y = j;
        }
      }
    MinStep = 20; // 将最小步数初始设置为较大值
    dfs(s.x, s.y, s.status, true, 0, 0);
    if (MinStep > 10)
      cout << "-1" << endl;
    else
      cout << MinStep << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int n, m;
int sx, sy, zx, zy;
int nex[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int mp[50][50];
int book[50][50];
int flag = 0;
void dfs(int x, int y, int step) {
  if (step >= 10)
    return;
  for (int i = 0; i < 4; i++) {
    int xx = x + nex[i][1];
    int yy = y + nex[i][0];
    if (mp[xx][yy] == 1)
      continue;
    while (!mp[xx][yy]) {
      xx += nex[i][1];
      yy += nex[i][0];
    }
    if (xx >= 1 && xx <= m && yy >= 1 && yy <= n) {
      if (mp[xx][yy] == 1) {
        mp[xx][yy] = 0;
        dfs(xx - nex[i][1], yy - nex[i][0], step + 1);
        mp[xx][yy] = 1;
      }
      if (mp[xx][yy] == 3) {
        flag = min(flag, step + 1);
      }
    }
  }
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    if (!n && !m)
      break;
    memset(book, 0, sizeof(book));
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        scanf("%d", &mp[i][j]);
        if (mp[i][j] == 2) {
          sx = i;
          sy = j;
          mp[i][j] = 0;
        }
      }
    }
    flag = 99999999;
    dfs(sx, sy, 0);
    if (flag > 10)
      cout << "-1" << endl;
    else
      cout << flag << endl;
  }
  return 0;
}