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

#include <bits/stdc++.h>

#define f(i, a, b, c) for (int i = a; i < b; i += c)
#define fr(i, a, b, c) for (int i = a; i >= b; i -= c)
#define fit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define frit(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define NL '\\n'
#define modn 1000000007
#define EPS 1e-9

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<lli> vli;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

#ifdef LOCAL_TEST
  ifstream h("in");
  ofstream g("out");
  clock_t z = clock();
#else
#define h cin
#define g cout
#endif // LOCAL_TEST

  int n, m, a, b, x, y;
  h >> n >> m >> a >> b >> x >> y;
  a--, b--, x--, y--;
  vs v(n);
  f(i, 0, n, 1) h >> v[i];
  int dr[] = {-1, 0, 1, 0};
  int dc[] = {0, 1, 0, -1};
  vvi dist(n, vi(m, INT_MAX));
  dist[a][b] = 0;
  deque<ii> q;
  q.pb({a, b});
  while (!q.empty()) {
    int nx = q.front().first, ny = q.front().second;
    q.pop_front();
    f(i, 0, 4, 1) {
      int dx = nx + dr[i], dy = ny + dc[i];
      if (dx < 0 || dx >= n || dy < 0 || dy >= m || v[dx][dy] == '#')
        continue;
      if (dist[dx][dy] > dist[nx][ny]) {
        dist[dx][dy] = dist[nx][ny];
        q.push_front({dx, dy});
      }
    }
    f(i, -2, 3, 1) {
      f(j, -2, 3, 1) {
        int dx = nx + i, dy = ny + j;
        if (dx < 0 || dx >= n || dy >= m || dy < 0 || v[dx][dy] == '#')
          continue;
        if (dist[dx][dy] > dist[nx][ny] + 1) {
          dist[dx][dy] = dist[nx][ny] + 1;
          q.pb({dx, dy});
        }
      }
    }
  }
  g << (dist[x][y] == INT_MAX ? -1 : dist[x][y]);

#ifdef LOCAL_TEST
  cerr << "Time taken is: " << double(clock() - z) / CLOCKS_PER_SEC << NL;
  h.close();
  g.close();
#endif // LOCAL_TEST
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define f(i, a, b, c) for (int i = a; i < b; i += c)
#define fr(i, a, b, c) for (int i = a; i >= b; i -= c)
#define fit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define frit(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define NL '\\n'
#define modn 1000000007
#define EPS 1e-9

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<lli> vli;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;

int n, m, a, b, x, y;
vs v;
vvi cnt;
vector<vb> vis;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};

int dfs(int r, int c, int coin) {
  //	cerr << r << ' ' << c << ' ' << coin << NL;
  if (r == x && c == y) {
    cnt[r][c] = min(cnt[r][c], coin);
    return cnt[r][c];
  }
  vis[r][c] = true;
  f(i, 0, 4, 1) {
    int rt = r + dr[i], ct = c + dc[i];
    //		cerr << rt << ' ' << ct << NL;
    if (rt < 1 || rt > n || ct < 1 || ct > m || v[rt][ct] == '#')
      continue;
    if (!vis[rt][ct])
      cnt[r][c] = min(cnt[r][c], dfs(rt, ct, coin));
    else
      cnt[r][c] = min(cnt[r][c], cnt[rt][ct]);
  }
  f(i, -2, 2, 1) {
    f(j, -2, 2, 1) {
      int rt = r + i, ct = c + j;
      if (!(ct < 1 || ct > m || rt < 1 || rt > m || v[rt][ct] == '#')) {
        if (!vis[rt][ct])
          cnt[r][c] = min(cnt[r][c], dfs(rt, ct, coin + 1));
        else
          cnt[r][c] = min(cnt[r][c], cnt[rt][ct]);
      }
    }
  }
  return cnt[r][c];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

#ifdef LOCAL_TEST
  ifstream h("in");
  ofstream g("out");
  clock_t z = clock();
#else
#define h cin
#define g cout
#endif // LOCAL_TEST

  h >> n >> m >> a >> b >> x >> y;
  v.resize(n + 1);
  f(i, 1, n + 1, 1) h >> v[i];
  vis.resize(n + 1);
  cnt.resize(n + 1);
  f(i, 1, n + 1, 1) {
    vis[i].resize(m + 1);
    cnt[i].resize(m + 1);
    fill(all(cnt[i]), INT_MAX);
  }
  dfs(a, b, 0);
  g << (cnt[a][b] == INT_MAX ? -1 : cnt[a][b]);

#ifdef LOCAL_TEST
  cerr << "Time taken is: " << double(clock() - z) / CLOCKS_PER_SEC << NL;
  h.close();
  g.close();
#endif // LOCAL_TEST
}