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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 1000005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define endl "\\n"
using namespace std;

int a, b, c, d, n, m;
bool vis[1002][1002];
char mat[1002][1002];

bool valid(int i, int j) { return i >= 1 && j >= 1 && i <= n && j <= m; }

int dr[] = {0, 0, 1, -1};
int dc[] = {-1, 1, 0, 0};

void test_case() {

  cin >> n >> m;

  cin >> a >> b;
  cin >> c >> d;

  fo(i, 1, n) {
    fo(j, 1, m) { cin >> mat[i][j]; }
  }

  deque<pair<pii, int>> dq;
  dq.push_front(mpr(mpr(a, b), 0));

  while (!dq.empty()) {
    pair<pii, int> temp = dq.front();
    dq.pop_front();

    pii pos = temp.first;

    // cout<<pos.first<<pos.second<<endl;

    if (pos.first == c && pos.second == d) {
      cout << temp.second << endl;
      return;
    }

    if (vis[pos.first][pos.second] == 1)
      continue;

    vis[pos.first][pos.second] = 1;

    for (int i = 0; i < 4; i++) {
      int x = pos.first + dr[i];
      int y = pos.second + dc[i];
      if (valid(x, y) && mat[x][y] == '.' && vis[x][y] == 0) {
        dq.push_front(mpr(mpr(x, y), temp.second));
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x = pos.first + i;
        int y = pos.second + j;
        if (pos.first == 3 && pos.second == 3) {
          // cout<<x<<" "<<y<<endl;
        }
        if (valid(x, y) && mat[x][y] == '.' && vis[x][y] == 0) {
          // cout<<x<<" "<<y<<" hwcijkckw";
          dq.push_back(mpr(mpr(x, y), temp.second + 1));
        }
      }
    }
  }

  cout << -1 << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  int t = 1;
  //  cin >> t;
  while (t--) {
    test_case();
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 1000005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

int vis[10001][10001];
char arr[10001][10001];
int cluster[1001][1001];
unordered_map<int, vector<pii>> extra;
int h, w;
int dp[1001][1001];

void dfs(int i, int j, int cnt, vector<pii> &temp) {
  if (i < 1 || i > h || j < 1 || j > w || vis[i][j] == 1 || arr[i][j] == '#') {
    return;
  }
  temp.pb(mpr(i, j));
  cluster[i][j] = cnt;
  vis[i][j] = 1;

  dfs(i - 1, j, cnt, temp);
  dfs(i + 1, j, cnt, temp);
  dfs(i, j - 1, cnt, temp);
  dfs(i, j + 1, cnt, temp);
}

int find(int a, int b) {

  if (dp[a][b] != INT_MAX) {
    return dp[a][b];
  }

  if (dp[b][a] != INT_MAX) {
    return dp[b][a];
  }

  if (a == b) {
    dp[a][b] = 0;
    return dp[a][b];
  }

  int val = INT_MAX;

  for (auto it : extra[a]) {

    int x = it.first;
    int y = it.second;

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 1 || j < 1 || i > h || j > w || arr[i][j] == '#' ||
            a == cluster[i][j] || vis[i][j] == 1) {
          continue;
        } else {
          vis[i][j] = 1;
          dp[a][b] = min(dp[a][b], 1 + find(cluster[i][j], b));
          val = min(val, dp[a][b]);
        }
      }
    }
  }

  dp[a][b] = val;
  dp[b][a] = val;
  return val;
}

void solve() {

  cin >> h >> w;

  int a, b;
  cin >> a >> b;

  int x, y;
  cin >> x >> y;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> arr[i][j];
    }
  }

  int cnt = 1;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (vis[i][j] == 0 && arr[i][j] == '.') {
        vector<pii> temp;
        dfs(i, j, cnt, temp);
        extra[cnt] = temp;
        cnt++;
      }
    }
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      dp[i][j] = INT_MAX;
      if (i == j) {
        dp[i][i] = 0;
      }
      vis[i][j] = 0;
    }
  }

  int ans = find(cluster[a][b], cluster[x][y]);

  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}