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

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; ++i)
#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)
#define rep(i, begin, end) for (__typeof(end) i = (begin); i != (end); i++)
#define fill(a, value) memset(a, value, sizeof(a));
#define gcd(a, b) __gcd((a), (b))
#define watch1(x) cout << (x) << endl
#define watch2(x, y) cout << (x) << " " << (y) << endl
#define watch3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 1005;

int i, j, h, w, ch, cw, dh, dw, dist[N][N];
char s[N][N];

void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> s[i][j];
      dist[i][j] = INF;
    }
  }
  dist[ch][cw] = 0;
  deque<pii> q;
  q.pf({ch, cw});
  while (!q.empty()) {
    pii p = q.front();
    q.pop_front();
    int x = p.f, y = p.s;
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int nx = x + dx, ny = y + dy;
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && s[nx][ny] == '.') {
          if (abs(dx) + abs(dy) == 1 && dist[x][y] < dist[nx][ny]) {
            dist[nx][ny] = dist[x][y];
            q.pf({nx, ny});
          } else if (dist[x][y] + 1 < dist[nx][ny]) {
            dist[nx][ny] = dist[x][y] + 1;
            q.pb({nx, ny});
          }
        }
      }
    }
  }
  if (dist[dh][dw] == INF)
    dist[dh][dw] = -1;
  cout << dist[dh][dw];
}

signed main() {
  fastio;
  int tt;
  // cin>>tt;
  tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; ++i)
#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)
#define rep(i, begin, end) for (__typeof(end) i = (begin); i != (end); i++)
#define fill(a, value) memset(a, value, sizeof(a));
#define gcd(a, b) __gcd((a), (b))
#define watch1(x) cout << (x) << endl
#define watch2(x, y) cout << (x) << " " << (y) << endl
#define watch3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 1005;

int i, j, h, w, ch, cw, dh, dw;
char s[N][N];

void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> s[i][j];
    }
  }
  queue<pii> q;
  map<pii, int> dist;
  q.push({dh, dw});
  dist[{dh, dw}] = 0;
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int x = p.f, y = p.s;
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int nx = x + dx, ny = y + dy;
        if (nx <= h && ny <= w && s[nx][ny] == '.' &&
            dist.find({nx, ny}) == dist.end()) {
          q.push({nx, ny});
          dist[{nx, ny}] = dist[{x, y}] + 1;
        }
      }
    }
  }
  map<pii, int> vis;
  q.push({ch, cw});
  vis[{ch, cw}] = 1;
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int x = p.f, y = p.s;
    if (x - 1 >= 1 && s[x - 1][y] == '.' && vis.find({x - 1, y}) == vis.end()) {
      vis[{x - 1, y}] = 1;
      q.push({x - 1, y});
    }
    if (x + 1 <= h && s[x + 1][y] == '.' && vis.find({x + 1, y}) == vis.end()) {
      vis[{x + 1, y}] = 1;
      q.push({x + 1, y});
    }
    if (y - 1 >= 1 && s[x][y - 1] == '.' && vis.find({x, y - 1}) == vis.end()) {
      vis[{x, y - 1}] = 1;
      q.push({x, y - 1});
    }
    if (y + 1 <= w && s[x][y + 1] == '.' && vis.find({x, y + 1}) == vis.end()) {
      vis[{x, y + 1}] = 1;
      q.push({x, y + 1});
    }
  }
  int ans = INF;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (vis[{i, j}] == 1 && dist.find({i, j}) != dist.end()) {
        ans = min(ans, dist[{i, j}]);
      }
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans;
}

signed main() {
  fastio;
  int tt;
  // cin>>tt;
  tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}