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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int h, w;
  cin >> h >> w;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;

  pair<int, int> s = make_pair(sx, sy), g = make_pair(gx, gy);
  vector<vector<int>> ans(h, vector<int>(w, mod));
  string sur[h];

  for (int i = 0; i < h; i++)
    cin >> sur[i];

  auto inout = [&](int x, int y) {
    if (0 <= x && x < w && 0 <= y && y < h)
      return true;
    else
      return false;
  };

  auto add = [&](pair<int, int> now, int dx, int dy) {
    pair<int, int> ret = make_pair(now.first + dx, now.second + dy);
    return ret;
  };

  queue<pair<int, int>> p, q;

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

  p.push(s);
  ans[s.second][s.first] = 0;

  int can = 0;

  while (!p.empty()) {
    while (!p.empty()) {
      pair<int, int> now = p.front();
      p.pop();
      q.push(now);
      bool first = true;
      for (int i = 0; i < 4; i++) {
        pair<int, int> nxt = add(now, dx[i], dy[i]);
        if (inout(nxt.first, nxt.second)) {
          if (sur[nxt.second][nxt.first] == '.') {
            ans[nxt.second][nxt.first] = can;
            if (nxt == g) {
              cout << ans[nxt.second][nxt.first] << endl;
              return 0;
            }
            p.push(nxt);
            sur[nxt.second][nxt.first] = '#';
          }
        }
      }
    }
    can++;
    while (!q.empty()) {
      pair<int, int> now = q.front();
      q.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          pair<int, int> nxt = make_pair(now.first + i, now.second + j);
          if (inout(nxt.first, nxt.second)) {
            if (sur[nxt.second][nxt.first] == '.') {
              ans[nxt.second][nxt.first] = can;
              p.push(nxt);
              sur[nxt.second][nxt.first] = '#';
              if (nxt == g) {
                cout << ans[nxt.second][nxt.first] << endl;
                return 0;
              }
            }
          }
        }
      }
    }
  }

  cout << -1 << endl;

  return 0;
}


Error: 1

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int h, w;
  cin >> h >> w;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;

  pair<int, int> s = make_pair(sx, sy), g = make_pair(gx, gy);
  vector<vector<int>> ans(h, vector<int>(w, mod));
  string sur[h];

  for (int i = 0; i < h; i++)
    cin >> sur[i];

  auto inout = [&](int x, int y) {
    if (0 <= x && x < w && 0 <= y && y < h)
      return true;
    else
      return false;
  };

  auto add = [&](pair<int, int> now, int dx, int dy) {
    pair<int, int> ret = make_pair(now.first + dx, now.second + dy);
    return ret;
  };

  queue<pair<int, int>> p, q;

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

  p.push(s);
  ans[s.second][s.first] = 0;

  int can = 0;

  while (!p.empty()) {
    while (!p.empty()) {
      pair<int, int> now = p.front();
      p.pop();
      q.push(now);
      bool first = true;
      for (int i = 0; i < 4; i++) {
        pair<int, int> nxt = add(now, dx[i], dy[i]);
        if (inout(nxt.first, nxt.second)) {
          if (sur[nxt.second][nxt.first] == '.') {
            ans[nxt.second][nxt.first] = can;
            if (nxt == g) {
              cout << ans[nxt.second][nxt.first] << endl;
              return true;
            }
            p.push(nxt);
            sur[nxt.second][nxt.first] = '#';
          }
        }
      }
    }
    can++;
    while (!q.empty()) {
      pair<int, int> now = q.front();
      q.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          pair<int, int> nxt = make_pair(now.first + i, now.second + j);
          if (inout(nxt.first, nxt.second)) {
            if (sur[nxt.second][nxt.first] == '.') {
              ans[nxt.second][nxt.first] = can;
              p.push(nxt);
              sur[nxt.second][nxt.first] = '#';
              if (nxt == g) {
                cout << ans[nxt.second][nxt.first] << endl;
                return true;
              }
            }
          }
        }
      }
    }
  }

  cout << -1 << endl;

  return 0;
}