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

#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cout << "[" << __LINE__ << "] ";
#define dump(i) cout << #i ": " << i << " ";
#define dumpl(i) cout << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
// map<vector<vector<int>>, bool> mp;
int h, w;
int search(vector<vector<int>> field, int y, int x, int gy, int gx, int n) {
  /*
  cerr << "------------------------------------------------" << endl;
  cerr << h << "," << w << "," << n << "," << y << "," << x << endl;
  rep(i, h)
  {
      rep(j, w)
      {
          cerr << field[i][j];
      }
      cerr << endl;
  }
*/
  // mp[field] = true;
  if (n == 10)
    return 11;
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  int ret = 11;
  rep(i, 4) {
    auto rfield = field;
    int rx = x;
    int ry = y;
    field[y][x] = 0;
    bool flg = true;
    if (y + dy[i] < 0 || y + dy[i] > h - 1 || x + dx[i] < 0 ||
        x + dx[i] > w - 1 || field[y + dy[i]][x + dx[i]] == 1) {
      field = rfield;
      y = ry;
      x = rx;
      continue;
    }

    while (true) {
      if (y + dy[i] < 0 || y + dy[i] > h - 1 || x + dx[i] < 0 ||
          x + dx[i] > w - 1) {
        flg = false;
        break;
      }

      // cerr << y + dy[i] << "" << x + dx[i] << endl;
      if (field[y + dy[i]][x + dx[i]] == 1)
        break;
      if (field[y + dy[i]][x + dx[i]] == 3) {
        //   cerr << "success" << endl;
        //  cerr << n << endl;
        return n + 1;
      }

      y += dy[i];
      x += dx[i];
    }
    if (flg == false) {
      y = ry;
      x = rx;
      field = rfield;
      continue;
    }
    /* if (mp[field] == true)
    {
        cerr << "already" << endl;
        y = ry;
        x = rx;
        field[y][x] = 2;
        continue;
    }*/
    field[y][x] = 2;
    field[y + dy[i]][x + dx[i]] = 0;

    // cerr << "new:" << dy[i] << "," << dx[i] << endl;
    //  mp[field] = true;
    ret = min(ret, search(field, y, x, gy, gx, n + 1));

    y = ry;
    x = rx;

    field = rfield;
  }
  //  cerr << "ret" << ret << endl;
  return ret;
}

int main(int argc, char const *argv[]) {
  while (true) {

    cin >> w >> h;
    if (h == 0 && w == 0)
      break;
    //  cerr << h << "," << w << endl;
    vector<vector<int>> field(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> field[i][j];
    int sy, sx, gy, gx;
    rep(i, h) rep(j, w) {
      // cerr << i << "," << j << endl;
      if (field[i][j] == 2) {
        sy = i;
        sx = j;
        //    field[i][j] = 0;
      }
      if (field[i][j] == 3) {
        gy = i;
        gx = j;
        //   field[i][j] = 0;
      }
    }
    int ans = search(field, sy, sx, gy, sx, 0);
    cout << (ans == 11 ? -1 : ans) << endl;
  }
  return 0;
}


Error: 0
------------------------------------------------
1
1
2
1
1
3
------------------------------------------------
2
0
1
1
1
1
1
1
1
1
1
1
3


Fixed submission:

#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cout << "[" << __LINE__ << "] ";
#define dump(i) cout << #i ": " << i << " ";
#define dumpl(i) cout << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
// map<vector<vector<int>>, bool> mp;
int h, w;
int search(vector<vector<int>> field, int y, int x, int gy, int gx, int n) {

  /*
  cerr << "------------------------------------------------" << endl;
  cerr << h << "," << w << "," << n << "," << y << "," << x << endl;
  rep(i, h)
  {
      rep(j, w)
      {
          cerr << field[i][j];
      }
      cerr << endl;
  }
*/
  // mp[field] = true;
  if (n == 10)
    return 11;
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  int ret = 11;
  rep(i, 4) {
    auto rfield = field;
    int rx = x;
    int ry = y;
    field[y][x] = 0;
    bool flg = true;
    if (y + dy[i] < 0 || y + dy[i] > h - 1 || x + dx[i] < 0 ||
        x + dx[i] > w - 1 || field[y + dy[i]][x + dx[i]] == 1) {
      field = rfield;

      continue;
    }

    while (true) {
      if (y + dy[i] < 0 || y + dy[i] > h - 1 || x + dx[i] < 0 ||
          x + dx[i] > w - 1) {
        flg = false;
        break;
      }

      // cerr << y + dy[i] << "" << x + dx[i] << endl;
      if (field[y + dy[i]][x + dx[i]] == 1)
        break;
      if (field[y + dy[i]][x + dx[i]] == 3) {
        // cerr << "success" << endl;
        // cerr << n << endl;
        return n + 1;
      }

      y += dy[i];
      x += dx[i];
    }
    if (flg == false) {
      y = ry;
      x = rx;
      field = rfield;
      continue;
    }
    /* if (mp[field] == true)
    {
        cerr << "already" << endl;
        y = ry;
        x = rx;
        field[y][x] = 2;
        continue;
    }*/
    field[y][x] = 2;
    field[y + dy[i]][x + dx[i]] = 0;

    // cerr << "new:" << dy[i] << "," << dx[i] << endl;
    //  mp[field] = true;
    ret = min(ret, search(field, y, x, gy, gx, n + 1));

    y = ry;
    x = rx;

    field = rfield;
  }
  //  cerr << "ret" << ret << endl;
  return ret;
}

int main(int argc, char const *argv[]) {
  while (true) {

    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    //  cerr << h << "," << w << endl;
    vector<vector<int>> field(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> field[i][j];
    int sy, sx, gy, gx;
    rep(i, h) rep(j, w) {
      // cerr << i << "," << j << endl;
      if (field[i][j] == 2) {
        sy = i;
        sx = j;
        //    field[i][j] = 0;
      }
      if (field[i][j] == 3) {
        gy = i;
        gx = j;
        //   field[i][j] = 0;
      }
    }
    int ans = search(field, sy, sx, gy, sx, 0);
    if (ans == 11) {
      cerr << "------------------------------------------------" << endl;
      rep(i, h) {
        rep(j, w) { cerr << field[i][j]; }
        cerr << endl;
      }
    }
    cout << (ans == 11 ? -1 : ans) << endl;
  }
  return 0;
}