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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i, j) for (int i = 0; i < j; i++)
#define FORN(i, j, k) for (int i = j; i < k; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(a) a.begin(), a.end()
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int DX[25] = {0, 0, 0,  0,  0,  1,  1,  1,  1,  1,  2,  2, 2,
                    2, 2, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int DY[25] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                    1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int n, m;
int x, y, enx, eny, goal;
bool g[1005][1005];
int graph[1005][1005];
bool vis[1005][1005], used[500005];
vpii ver[500005];
int dis[500005] = {0};
int k = 0;
ll ans = 0;
bool infield(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
void bfs1(int a, int b) {
  queue<pii> q;
  q.push(mp(a, b));
  while (!q.empty()) {
    int _x = q.front().fi, _y = q.front().se;
    q.pop();
    vis[_x][_y] = 1;
    graph[_x][_y] = k;
    ver[k].pb(mp(_x, _y));
    REP(i, 4) {
      if ((!vis[_x + dx[i]][_y + dy[i]]) && g[_x + dx[i]][_y + dy[i]] &&
          infield(_x + dx[i], _y + dy[i])) {
        vis[_x + dx[i]][_y + dy[i]] = 1;
        q.push(mp(_x + dx[i], _y + dy[i]));
      }
    }
  }
}
void bfs2(int pos) {
  queue<int> q;
  used[pos] = 1;
  q.push(pos);
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    REP(i, (int)ver[a].size()) {
      int _x = ver[a][i].fi, _y = ver[a][i].se;
      REP(j, 25) {
        if (g[_x + DX[j]][_y + DY[j]] && infield(_x + DX[j], _y + DY[j])) {
          if (graph[_x + DX[j]][_y + DY[j]] != a &&
              !used[graph[_x + DX[j]][_y + DY[j]]]) {
            q.push(graph[_x + DX[j]][_y + DY[j]]);
            used[graph[_x + DX[j]][_y + DY[j]]] = 1;
            dis[graph[_x + DX[j]][_y + DY[j]]] = dis[a] + 1;
          }
        }
      }
    }
  }
}
int main(void) {
  cin >> n >> m >> x >> y >> enx >> eny;
  x--, y--, enx--, eny--;
  REP(i, n) REP(j, m) graph[i][j] = -1;
  REP(i, n) REP(j, m) {
    char c;
    cin >> c;
    if (c == '.') {
      g[i][j] = 1;
    } else {
      g[i][j] = 0;
    }
  }
  REP(i, n) REP(j, m) {
    if (!vis[i][j] && g[i][j]) {
      bfs1(i, j);
      k++;
    }
  }
  memset(vis, false, sizeof(vis));
  goal = graph[enx][eny];
  if (goal == graph[x][y]) {
    cout << 0;
    return 0;
  }
  bfs2(graph[x][y]);
  if (dis[goal] == 0)
    cout << -1;
  else
    cout << dis[goal];
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i, j) for (int i = 0; i < j; i++)
#define FORN(i, j, k) for (int i = j; i < k; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(a) a.begin(), a.end()
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int DX[25] = {0, 0, 0,  0,  0,  1,  1,  1,  1,  1,  2,  2, 2,
                    2, 2, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int DY[25] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                    1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int n, m;
int x, y, enx, eny, goal;
bool g[1005][1005];
int graph[1005][1005];
bool vis[1005][1005], used[500005];
vpii ver[500005];
int dis[500005] = {0};
int k = 0;
ll ans = 0;
bool infield(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
void bfs1(int a, int b) {
  queue<pii> q;
  q.push(mp(a, b));
  while (!q.empty()) {
    int _x = q.front().fi, _y = q.front().se;
    q.pop();
    vis[_x][_y] = 1;
    graph[_x][_y] = k;
    ver[k].pb(mp(_x, _y));
    REP(i, 4) {
      if ((!vis[_x + dx[i]][_y + dy[i]]) && g[_x + dx[i]][_y + dy[i]] &&
          infield(_x + dx[i], _y + dy[i])) {
        vis[_x + dx[i]][_y + dy[i]] = 1;
        q.push(mp(_x + dx[i], _y + dy[i]));
      }
    }
  }
}
bool bfs2(int pos) {
  queue<int> q;
  used[pos] = 1;
  q.push(pos);
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    REP(i, (int)ver[a].size()) {
      int _x = ver[a][i].fi, _y = ver[a][i].se;
      REP(j, 25) {
        if (g[_x + DX[j]][_y + DY[j]] && infield(_x + DX[j], _y + DY[j])) {
          if (graph[_x + DX[j]][_y + DY[j]] != a &&
              !used[graph[_x + DX[j]][_y + DY[j]]]) {
            q.push(graph[_x + DX[j]][_y + DY[j]]);
            used[graph[_x + DX[j]][_y + DY[j]]] = 1;
            dis[graph[_x + DX[j]][_y + DY[j]]] = dis[a] + 1;
          }
        }
      }
    }
  }
}
int main(void) {
  cin >> n >> m >> x >> y >> enx >> eny;
  x--, y--, enx--, eny--;
  REP(i, n) REP(j, m) graph[i][j] = -1;
  REP(i, n) REP(j, m) {
    char c;
    cin >> c;
    if (c == '.') {
      g[i][j] = 1;
    } else {
      g[i][j] = 0;
    }
  }
  REP(i, n) REP(j, m) {
    if (!vis[i][j] && g[i][j]) {
      bfs1(i, j);
      k++;
    }
  }
  memset(vis, false, sizeof(vis));
  goal = graph[enx][eny];
  if (goal == graph[x][y]) {
    cout << 0;
    return 0;
  }
  bfs2(graph[x][y]);
  if (dis[goal] == 0)
    cout << -1;
  else
    cout << dis[goal];
  return 0;
}