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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int W = 0; W < (n); W++)                                              \\
      cerr << v[W] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int mp[21][21];
int w, h;
int ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void dfs(int n, int x, int y, int c[21][21]) {
  if (n == 11)
    return;
  REP(i, 4) {
    int tx = x, ty = y;
    while (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
               ty + dy[i] < h && c[ty + dy[i]][tx + dx[i]] == 0 ||
           c[ty + dy[i]][tx + dx[i]] == 2) {
      tx += dx[i];
      ty += dy[i];
    }
    if (!(tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
          ty + dy[i] < h) ||
        ((x == tx && y == ty) && c[ty + dy[i]][tx + dx[i]] == 1))
      continue;
    tx += dx[i];
    ty += dy[i];
    if (c[ty][tx] == 3) {
      //	cout << "!!!" << n << ' ' << ty << ' ' << tx << endl;
      ans = min(ans, n);
    }
    if (c[ty][tx] == 1) {
      //	cout << n << ' ' << ty << ' ' << tx << endl;
      c[ty][tx] = 0;
      //	SHOW2d(c,h,w);
      dfs(n + 1, tx - dx[i], ty - dy[i], c);
      c[ty][tx] = 1;
    }
  }
}

int main() {
  while (cin >> w >> h, w | h) {
    int sx, sy;
    ans = 100;
    REP(i, h) {
      REP(j, w) {
        cin >> mp[i][j];
        if (mp[i][j] == 2) {
          sx = j, sy = i;
        }
      }
    }
    // cout << sx << ' ' << sy << endl;
    dfs(1, sx, sy, mp);

    if (ans != 100) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int W = 0; W < (n); W++)                                              \\
      cerr << v[W] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int mp[21][21];
int w, h;
int ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void dfs(int n, int x, int y, int c[21][21]) {
  if (n == 11)
    return;
  REP(i, 4) {
    int tx = x, ty = y;
    while (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
               ty + dy[i] < h && c[ty + dy[i]][tx + dx[i]] == 0 ||
           c[ty + dy[i]][tx + dx[i]] == 2) {
      tx += dx[i];
      ty += dy[i];
    }
    if (!(tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
          ty + dy[i] < h) ||
        ((x == tx && y == ty) && c[ty + dy[i]][tx + dx[i]] == 1))
      continue;
    tx += dx[i];
    ty += dy[i];
    if (c[ty][tx] == 3) {
      cout << "!!!" << n << ' ' << ty << ' ' << tx << endl;
      ans = min(ans, n);
    }
    if (c[ty][tx] == 1) {
      //	cout << n << ' ' << ty << ' ' << tx << endl;
      c[ty][tx] = 0;
      SHOW2d(c, h, w);
      dfs(n + 1, tx - dx[i], ty - dy[i], c);
      c[ty][tx] = 1;
    }
  }
}

int main() {
  while (cin >> w >> h, w | h) {
    int sx, sy;
    ans = 100;
    REP(i, h) {
      REP(j, w) {
        cin >> mp[i][j];
        if (mp[i][j] == 2) {
          sx = j, sy = i;
        }
      }
    }
    // cout << sx << ' ' << sy << endl;
    dfs(1, sx, sy, mp);

    if (ans != 100) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }

  return 0;
}