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

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define ll long long
#define ll long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

const int N = 1e3 + 5;
char grid[N][N];
int ans[N][N];
int h, w, ch, cw, dh, dw;

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

bool valid(int x, int y) { return (x > 0 and y > 0 and x <= h and y <= w); }

void bfs(int x, int y) {
  deque<pair<int, int>> q;
  q.push_front({x, y});
  int in_queue[N][N];
  in_queue[x][y] = 1;
  memset(in_queue, 0, sizeof(in_queue));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    in_queue[x][y] = 0;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int X = x + dx[i], Y = y + dy[i];
      if (i > 4) {
        cout << "HERE\\n";
        cout.flush();
      }
      if (valid(X, Y)) {
        if (grid[X][Y] == '#')
          continue;
        if (ans[X][Y] == -1 || ans[X][Y] > ans[x][y]) {
          ans[X][Y] = ans[x][y];
          if (in_queue[X][Y])
            continue;
          q.push_front({X, Y});
          in_queue[X][Y] = 1;
        }
      }
    }
    // teleport to a square within square
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 and j == 0)
          continue;
        int X = x + i, Y = y + j;
        if (valid(X, Y) and grid[X][Y] != '#') {
          if (ans[X][Y] == -1 || ans[X][Y] > ans[x][y] + 1) {
            ans[X][Y] = ans[x][y] + 1;
            if (in_queue[X][Y])
              continue;
            q.push_back({X, Y});
            in_queue[X][Y] = 1;
          }
        }
      }
    }
  }
}

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> grid[i + 1][j + 1];
    }
  }
  memset(ans, -1, sizeof(ans));
  ans[ch][cw] = 0;
  bfs(ch, cw);
  cout << ans[dh][dw] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define ll long long
#define ll long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

const int N = 1e3 + 5;
char grid[N][N];
int ans[N][N];
int h, w, ch, cw, dh, dw;

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

bool valid(int x, int y) { return (x > 0 and y > 0 and x <= h and y <= w); }

void bfs(int x, int y) {
  deque<pair<int, int>> q;
  q.push_front({x, y});
  int in_queue[N][N];
  in_queue[x][y] = 1;
  memset(in_queue, 0, sizeof(in_queue));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    in_queue[x][y] = 0;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int X = x + dx[i], Y = y + dy[i];
      if (valid(X, Y)) {
        if (grid[X][Y] == '#')
          continue;
        if (ans[X][Y] == -1 || ans[X][Y] > ans[x][y]) {
          ans[X][Y] = ans[x][y];
          if (in_queue[X][Y])
            continue;
          q.push_front({X, Y});
          in_queue[X][Y] = 1;
        }
      }
    }
    // teleport to a square within square
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 and j == 0)
          continue;
        int X = x + i, Y = y + j;
        if (valid(X, Y) and grid[X][Y] != '#') {
          if (ans[X][Y] == -1 || ans[X][Y] > ans[x][y] + 1) {
            ans[X][Y] = ans[x][y] + 1;
            if (in_queue[X][Y])
              continue;
            q.push_back({X, Y});
            in_queue[X][Y] = 1;
          }
        }
      }
    }
  }
}

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> grid[i + 1][j + 1];
    }
  }
  memset(ans, -1, sizeof(ans));
  ans[ch][cw] = 0;
  bfs(ch, cw);
  cout << ans[dh][dw] << "\\n";
  return 0;
}