Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, -1));
  deque<pair<int, int>> que;
  dist[ch][cw] = 0;
  que.push_front(pair<int, int>(ch, cw));
  while (!que.empty()) {
    auto v = que.front();
    que.pop_front();
    int y = v.first, x = v.second;
    cout << y << ", " << x << ": " << dist[x][y] << endl;
    for (int dir = 0; dir < 4; dir++) {
      int ny = y + dy[dir], nx = x + dx[dir];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dist[ny][nx] == -1) {
        dist[ny][nx] = dist[y][x];
        que.push_front(pair<int, int>(ny, nx));
      } else if (dist[ny][nx] > dist[y][x]) {
        dist[ny][nx] = dist[y][x];
        que.push_front(pair<int, int>(ny, nx));
      }
    }
    for (int ny = y - 2; ny <= y + 2; ny++) {
      for (int nx = x - 2; nx <= x + 2; nx++) {
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dist[ny][nx] == -1) {
          dist[ny][nx] = dist[y][x] + 1;
          que.push_back(pair<int, int>(ny, nx));
        } else if (dist[ny][nx] > dist[y][x] + 1) {
          dist[ny][nx] = dist[y][x] + 1;
          que.push_front(pair<int, int>(ny, nx));
        }
      }
    }
  }
  cout << dist[dh][dw] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, -1));
  deque<pair<int, int>> que;
  dist[ch][cw] = 0;
  que.push_front(pair<int, int>(ch, cw));
  while (!que.empty()) {
    auto v = que.front();
    que.pop_front();
    int y = v.first, x = v.second;
    for (int dir = 0; dir < 4; dir++) {
      int ny = y + dy[dir], nx = x + dx[dir];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dist[ny][nx] == -1) {
        dist[ny][nx] = dist[y][x];
        que.push_front(pair<int, int>(ny, nx));
      } else if (dist[ny][nx] > dist[y][x]) {
        dist[ny][nx] = dist[y][x];
        que.push_front(pair<int, int>(ny, nx));
      }
    }
    for (int ny = y - 2; ny <= y + 2; ny++) {
      for (int nx = x - 2; nx <= x + 2; nx++) {
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dist[ny][nx] == -1) {
          dist[ny][nx] = dist[y][x] + 1;
          que.push_back(pair<int, int>(ny, nx));
        } else if (dist[ny][nx] > dist[y][x] + 1) {
          dist[ny][nx] = dist[y][x] + 1;
          que.push_front(pair<int, int>(ny, nx));
        }
      }
    }
  }
  cout << dist[dh][dw] << endl;
  return 0;
}