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

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define v(i) vector<i>
#define FOR(i, a, b) for (i = a; i < b; i++)
#define FORE(i, a, b) for (i = a; i <= b; i++)
#define FORV(i, a, b) for (i = a; i > b; i--)
#define FOREV(i, a, b) for (i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define look(v)                                                                \\
  for (auto c : v)                                                             \\
    cout << c << " ";                                                          \\
  cout << "\\n";
#define dfdc                                                                   \\
  int i, j, k, m, n, ct = 0;                                                   \\
  string s;
#define ppl(a, st, end)                                                        \\
  for (i = st; i <= end; i++)                                                  \\
    cin >> a[i];
#define fz(a) memset(a, 0, sizeof a);
#define dp_1(n)                                                                \\
  int dp[n];                                                                   \\
  fz(dp);
#define dp_2(m, n)                                                             \\
  int dp[m][n];                                                                \\
  fz(dp);
#define arpt(a, s, e)                                                          \\
  for (i = s; i <= e; i++)                                                     \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define tk                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;

typedef long long int ll;

int h, w;

int main() {
  fastio cin >> h >> w;
  vector<vector<char>> vec(h, vector<char>(w, '\\0'));
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> vec[i][j];
    }
  }
  priority_queue<pair<int, pair<int, int>>> q;
  vector<vector<int>> d(h, vector<int>(w, -1));
  vector<vector<bool>> vis(h, vector<bool>(w, false));
  d[ch - 1][cw - 1] = 0;
  q.push({0, {ch - 1, cw - 1}});
  while (!q.empty()) {
    auto pp = q.top();
    q.pop();
    int dist = -pp.first;
    int X = pp.second.first;
    int Y = pp.second.second;
    // cout<<X<<" "<<Y<<"\\n";
    if (!vis[X][Y]) {
      vis[X][Y] = true;
      for (int i = X - 2; i <= X + 2; i++) {
        for (int j = Y - 2; j <= Y + 2; j++) {
          if (i >= 0 && j >= 0 && i < h && j < w && vec[i][j] != '#') {
            if (abs(i - X) + abs(j - Y) == 1) {
              if (d[i][j] != -1) {
                d[i][j] = min(d[i][j], dist);
              } else {
                d[i][j] = dist;
              }
              q.push({-d[i][j], {i, j}});
            } else {
              if (d[i][j] != -1) {
                d[i][j] = min(d[i][j], 1 + dist);
              } else {
                d[i][j] = 1 + dist;
              }
              q.push({-d[i][j], {i, j}});
            }
          }
        }
      }
    }
  }
  cout << d[dh - 1][dw - 1] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define v(i) vector<i>
#define FOR(i, a, b) for (i = a; i < b; i++)
#define FORE(i, a, b) for (i = a; i <= b; i++)
#define FORV(i, a, b) for (i = a; i > b; i--)
#define FOREV(i, a, b) for (i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define look(v)                                                                \\
  for (auto c : v)                                                             \\
    cout << c << " ";                                                          \\
  cout << "\\n";
#define dfdc                                                                   \\
  int i, j, k, m, n, ct = 0;                                                   \\
  string s;
#define ppl(a, st, end)                                                        \\
  for (i = st; i <= end; i++)                                                  \\
    cin >> a[i];
#define fz(a) memset(a, 0, sizeof a);
#define dp_1(n)                                                                \\
  int dp[n];                                                                   \\
  fz(dp);
#define dp_2(m, n)                                                             \\
  int dp[m][n];                                                                \\
  fz(dp);
#define arpt(a, s, e)                                                          \\
  for (i = s; i <= e; i++)                                                     \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define tk                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;

typedef long long int ll;
int h, w;
vector<vector<char>> grid;
vector<vector<int>> region;

bool ok2(int x, int y) {
  if (x < 0 || y < 0 || x >= h || y >= w)
    return false;
  return region[x][y] != -1;
}

bool ok(int x, int y) {
  if (x < 0 || y < 0 || x >= h || y >= w)
    return false;
  return region[x][y] == -1 && grid[x][y] == '.';
}

void flood(int x, int y, int lvl) {
  region[x][y] = lvl;
  if (ok(x + 1, y))
    flood(x + 1, y, lvl);
  if (ok(x - 1, y))
    flood(x - 1, y, lvl);
  if (ok(x, y + 1))
    flood(x, y + 1, lvl);
  if (ok(x, y - 1))
    flood(x, y - 1, lvl);
}
vector<vector<int>> adj;
vector<int> d;
int main() {
  fastio cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  grid.resize(h, vector<char>(w, '\\0'));
  region.resize(h, vector<int>(w, -1));
  // dp.resize(h,vector<int>(w,-1));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> grid[i][j];
  }
  int lvl = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (region[i][j] == -1 && grid[i][j] == '.') {
        flood(i, j, lvl);
        lvl++;
      }
    }
  }
  grid.clear();
  adj.resize(lvl, vector<int>(lvl, 0));
  d.resize(lvl, -1);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (region[i][j] != -1) {
        for (int a = -2; a <= 2; a++) {
          for (int b = -2; b <= 2; b++) {
            if (ok2(i + a, j + b)) {
              adj[region[i][j]][region[i + a][j + b]] = 1;
              adj[region[i + a][j + b]][region[i][j]] = 1;
            }
          }
        }
      }
    }
  }

  queue<int> q;
  q.push(region[ch - 1][cw - 1]);
  d[region[ch - 1][cw - 1]] = 0;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (int i = 0; i < adj[node].size(); i++) {
      int X = adj[node][i];
      if (X == 1 && d[i] == -1) {
        d[i] = d[node] + 1;
        q.push(i);
      }
    }
  }
  cout << d[region[dh - 1][dw - 1]] << "\\n";

  return 0;
}