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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;
const ll lnf = 1ll << 60;

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

ll dp[1010][1010];
bool seen[1010][1010];

int main() {
  int h, w;
  cin >> h >> w;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  rep(i, h) rep(j, w) dp[i][j] = lnf;
  queue<pair<int, int>> q;
  q.push(make_pair(a, b));
  dp[a][b] = 0;
  set<pii> st;
  const int dy[4] = {-1, 1, 0, 0};
  const int dx[4] = {0, 0, 1, -1};
  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    seen[y][x] = 1;
    if (st.count(pii(y, x)))
      st.erase(pii(y, x));
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (seen[y + i][x + j])
          continue;
        if (0 <= y + i && y + i < h && x + j >= 0 && x + j < w &&
            g[y + i][x + j] == '.' && (!(i == 0 && j == 0))) {
          chmin(dp[y + i][x + j], dp[y][x] + 1);
          st.insert(pii(y + i, x + j));
        }
      }
    }
    rep(dir, 4) {
      int ny = y + dy[dir];
      int nx = x + dx[dir];
      if (0 <= ny && ny < h && nx >= 0 && nx < w && g[ny][nx] == '.' &&
          seen[ny][nx] == 0) {
        chmin(dp[ny][nx], dp[y][x]);
        seen[ny][nx] = 1;
        q.push(pii(ny, nx));
      }
    }
    if (q.empty() && st.size()) {
      for (auto v : st)
        q.push(pii(v.first, v.second));
      set<pii> st;
    }
  }
  if (seen[c][d] == 0) {
    cout << -1 << endl;
    return 0;
  }
  cout << dp[c][d] << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

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

int dp[10][10];
bool seen[10][10];

int main() {
  int h, w;
  cin >> h >> w;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  queue<pair<int, int>> q;
  q.push(make_pair(a, b));
  set<pii> st;
  const int dy[4] = {-1, 1, 0, 0};
  const int dx[4] = {0, 0, 1, -1};
  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    seen[y][x] = 1;
    if (st.count(pii(y, x)))
      st.erase(pii(y, x));
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (seen[y + i][x + j])
          continue;
        if (0 <= y + i && y + i < h && x + j >= 0 && x + j < w &&
            (!(i == 0 && j == 0))) {
          dp[y + i][x + j] = dp[y][x] + 1;
          if (g[y + i][x + j] == '.')
            st.insert(pii(y + i, x + j));
        }
      }
    }
    rep(dir, 4) {
      int ny = y + dy[dir];
      int nx = x + dx[dir];
      if (0 <= ny && ny < h && nx >= 0 && nx < w && g[ny][nx] == '.' &&
          seen[ny][nx] == 0) {
        chmin(dp[ny][nx], dp[y][x]);
        seen[ny][nx] = 1;
        q.push(pii(ny, nx));
      }
    }
    if (q.empty() && st.size()) {
      for (auto v : st)
        q.push(pii(v.first, v.second));
      set<pii> st;
    }
  }
  if (seen[c][d] == 0) {
    cout << -1 << endl;
    return 0;
  }
  cout << dp[c][d] << endl;
}