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

#include <bits/stdc++.h>

#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define TC                                                                     \\
  int tc;                                                                      \\
  cin >> tc;                                                                   \\
  while (tc--)

using namespace std;

const int INF = 1e9 + 7;

int h, w, sx, sy, dh, dw;
vector<vector<char>> g;

bool legal(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w || g[i][j] == '#')
    return false;
  return true;
}

int main(void) {
  FAST;
  cin >> h >> w >> sx >> sy >> dh >> dw;
  sx--;
  sy--;
  dh--;
  dw--;
  g = vector<vector<char>>(h, vector<char>(w));
  vector<vector<int>> dist(h, vector<int>(w, INF));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
    }
  }

  deque<int> x, y;
  x.push_front(sx);
  y.push_front(sy);
  dist[sx][sy] = 0;

  int dx[4] = {0, 1, -1, 0};
  int dy[4] = {1, 0, 0, -1};

  while (!x.empty() && !y.empty()) {

    int curx = x.front(), cury = y.front();
    x.pop_front();
    y.pop_front();

    for (int i = 0; i < 4; i++) {
      int nx = curx + dx[i], ny = cury + dy[i];
      if (!legal(nx, ny))
        continue;
      if (dist[nx][ny] > dist[curx][cury]) {
        dist[nx][ny] = dist[curx][cury];
        x.push_front(nx);
        y.push_front(ny);
      }
    }

    for (int nx = curx - 2; nx <= curx + 2; nx++) {
      for (int ny = cury - 2; ny <= cury + 2; ny++) {
        if (!legal(nx, ny))
          continue;
        if (dist[nx][ny] > dist[curx][cury] + 1) {
          dist[nx][ny] = dist[curx][cury] + 1;
          x.push_back(nx);
          y.push_back(ny);
        }
      }
    }
  }

  if (dist[dh][dw] == INF)
    cout << -1 << '\\n';
  else
    cout << dist[dh][dw] << '\\n';

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define TC                                                                     \\
  int tc;                                                                      \\
  cin >> tc;                                                                   \\
  while (tc--)

using namespace std;

const int INF = 1e9 + 7;

int h, w, sh, sw, vh, vw;

void dfs(int i, int j, int cur, vector<vector<int>> &c,
         vector<vector<char>> &g) {

  c[i][j] = cur;

  if (i > 0 && g[i - 1][j] != '#')
    if (!c[i - 1][j])
      dfs(i - 1, j, cur, c, g);
  if (i < h - 1 && g[i + 1][j] != '#')
    if (!c[i + 1][j])
      dfs(i + 1, j, cur, c, g);
  if (j > 0 && g[i][j - 1] != '#')
    if (!c[i][j - 1])
      dfs(i, j - 1, cur, c, g);
  if (j < w - 1 && g[i][j + 1] != '#')
    if (!c[i][j + 1])
      dfs(i, j + 1, cur, c, g);
  return;
}

int main(void) {
  FAST;
  set<int> adj[10100];

  cin >> h >> w;
  cin >> sh >> sw >> vh >> vw;
  sh--;
  sw--;
  vh--;
  vw--;
  vector<vector<char>> g(h, vector<char>(w));
  vector<vector<int>> c(h, vector<int>(w, 0));

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> g[i][j];

  int cur = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == 0 && g[i][j] != '#') {
        dfs(i, j, cur, c, g);
        cur++;
      }
    }
  }

  if (c[sh][sw] == c[vh][vw])
    cout << 0 << '\\n';
  else {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (i > 1) {

          if (c[i - 2][j] != c[i][j] && g[i - 2][j] != '#') {
            adj[c[i][j]].insert(c[i - 2][j]);
            adj[c[i - 2][j]].insert(c[i][j]);
          }

          if (j > 1 && g[i - 2][j - 2] != '#') {
            if (c[i - 2][j - 2] != c[i][j])
              adj[c[i][j]].insert(c[i - 2][j - 2]);
            adj[c[i - 2][j - 2]].insert(c[i][j]);
          }

          if (j < w - 2 && g[i - 2][j + 2] != '#') {
            if (c[i - 2][j + 2] != c[i][j])
              adj[c[i][j]].insert(c[i - 2][j + 2]);
            adj[c[i - 2][j + 2]].insert(c[i][j]);
          }
        }
        if (i < h - 2) {

          if (c[i + 2][j] != c[i][j] && g[i + 2][j] != '#') {
            adj[c[i][j]].insert(c[i + 2][j]);
            adj[c[i + 2][j]].insert(c[i][j]);
          }

          if (j > 1 && g[i + 2][j - 2] != '#') {
            if (c[i + 2][j - 2] != c[i][j])
              adj[c[i][j]].insert(c[i + 2][j - 2]);
            adj[c[i + 2][j - 2]].insert(c[i][j]);
          }

          if (j < w - 2 && g[i + 2][j + 2] != '#') {
            if (c[i + 2][j + 2] != c[i][j])
              adj[c[i][j]].insert(c[i + 2][j + 2]);
            adj[c[i + 2][j + 2]].insert(c[i][j]);
          }
        }
        if (j > 1 && g[i][j - 2] != '#') {
          if (c[i][j - 2] != c[i][j]) {
            adj[c[i][j]].insert(c[i][j - 2]);
            adj[c[i][j - 2]].insert(c[i][j]);
          }
        }
        if (j < w - 2 && g[i][j + 2] != '#') {
          if (c[i][j + 2] != c[i][j]) {
            adj[c[i][j]].insert(c[i][j + 2]);
            adj[c[i][j + 2]].insert(c[i][j]);
          }
        }

        if (j < w - 1 && i < h - 1) {
          if (c[i + 1][j + 1] != c[i][j]) {
            adj[c[i][j]].insert(c[i + 1][j + 1]);
            adj[c[i + 1][j + 1]].insert(c[i][j]);
          }
        }
        if (j < w - 1 && i > 0) {
          if (c[i - 1][j + 1] != c[i][j]) {
            adj[c[i][j]].insert(c[i - 1][j + 1]);
            adj[c[i - 1][j + 1]].insert(c[i][j]);
          }
        }
        if (j > 0 && i < h - 1) {
          if (c[i + 1][j - 1] != c[i][j]) {
            adj[c[i][j]].insert(c[i + 1][j - 1]);
            adj[c[i + 1][j - 1]].insert(c[i][j]);
          }
        }
        if (j > 0 && i > 0) {
          if (c[i - 1][j - 1] != c[i][j]) {
            adj[c[i][j]].insert(c[i - 1][j - 1]);
            adj[c[i - 1][j - 1]].insert(c[i][j]);
          }
        }
      }
    }

    for (auto i : adj[c[sh][sw]])
      cout << i << ' ';
    cout << '\\n';
    for (auto i : adj[c[vh][vw]])
      cout << i << ' ';
    cout << '\\n';

    vector<int> seen(10000, 0);
    vector<pii> front;
    front.pb({c[sh][sw], 0});
    seen[0] = 1;
    bool ok = 0;
    while (!front.empty()) {
      vector<pii> nxt;
      for (auto p : front) {
        int cur = p.first;
        int depth = p.second;
        if (cur == c[vh][vw]) {
          ok = 1;
          cout << depth << '\\n';
        }
        for (int v : adj[cur]) {
          if (!seen[v]) {
            seen[v] = 1;
            nxt.pb({v, depth + 1});
          }
        }
      }
      front = nxt;
    }
    if (!ok)
      cout << -1 << '\\n';
  }

  return 0;
}