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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll h, w, ch, cw, dh, dw;
char tb[1005][1005];
ll mp[1005][1005];
vector<ll> v[1000005];
map<pair<ll, ll>, ll> mp2;
map<ll, ll> mp3;
void dfs(ll rw, ll col, ll ind) {
  if (rw <= 0 || rw > h || col <= 0 || col > w)
    return;
  if (mp[rw][col] != 0)
    return;
  if (tb[rw][col] == '#')
    return;
  mp[rw][col] = ind;
  dfs(rw - 1, col, ind);
  dfs(rw + 1, col, ind);
  dfs(rw, col - 1, ind);
  dfs(rw, col + 1, ind);
}

void func(ll start_row, ll start_col, ll end_row, ll end_col, ll rw, ll col) {
  ll i, j;
  for (i = start_row; i <= end_row; i++) {
    for (j = start_col; j <= end_col; j++) {
      if (((mp2.find({mp[i][j], mp[rw][col]}) == mp2.end()) &&
           (mp[i][j] != mp[rw][col])) &&
          (mp[i][j] != 0)) {
        v[mp[rw][col]].push_back(mp[i][j]);
        v[mp[i][j]].push_back(mp[rw][col]);
        mp2[{mp[i][j], mp[rw][col]}]++;
        mp2[{mp[rw][col], mp[i][j]}]++;
      }
    }
  }
}

ll bfs(ll ind, ll dest) {
  if (ind == dest)
    return 0;
  bool flag = false;
  ll i, j, z = 1, ans = 0;
  queue<ll> q;
  q.push(ind);
  mp3[ind]++;
  while (!q.empty()) {
    j = 0;
    z = q.size();
    while (j < z) {
      ll x = q.front();
      if (x == dest) {
        flag = true;
        break;
      }
      q.pop();
      ll p = v[x].size();
      for (i = 0; i < p; i++) {
        if (mp3[v[x][i]] == 0) {
          mp3[v[x][i]]++;
          q.push(v[x][i]);
        }
      }
      j++;
    }
    if (flag)
      break;
    ans++;
  }

  if (flag)
    return ans;
  return -1;
}
int main() {
  ll i, j, ind = 1;
  cin >> h >> w >> ch >> cw >> dh >> dw;

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> tb[i][j];
      mp[i][j] = 0;
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (mp[i][j] == 0 && (tb[i][j] == '.')) {
        dfs(i, j, ind);
        ind++;
      }
    }
  }

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      ll sr = max(i - 2, (ll)1);
      ll sc = max(j - 2, (ll)1);
      ll er = min(i + 2, h);
      ll ec = min(j + 2, w);
      if (tb[i][j] != '#')
        func(sr, sc, er, ec, i, j);
    }
  }

  /*for(i=1;i<=h;i++)
  {
          for(j=1;j<=w;j++)
          {
                  cout<<mp[i][j]<<" ";
          }
          cout<<"\\n";
  }



  for(i=1;i<=ind;i++)
  {
          cout<<i<<": ";
          for(j=0;j<v[i].size();j++)
          {
                  cout<<v[i][j]<<" ";
          }
          cout<<"\\n";
  }*/

  ll x = mp[ch][cw];
  ll y = mp[dh][dw];
  cout << bfs(x, y);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll h, w, ch, cw, dh, dw;
char tb[1005][1005];
ll mp[1005][1005];
vector<ll> v[100005];
map<pair<ll, ll>, ll> mp2;
map<ll, ll> mp3;
void dfs(ll rw, ll col, ll ind) {
  if (rw <= 0 || rw > h || col <= 0 || col > w)
    return;
  if (mp[rw][col] != 0)
    return;
  if (tb[rw][col] == '#')
    return;
  mp[rw][col] = ind;
  dfs(rw - 1, col, ind);
  dfs(rw + 1, col, ind);
  dfs(rw, col - 1, ind);
  dfs(rw, col + 1, ind);
}

void func(ll start_row, ll start_col, ll end_row, ll end_col, ll rw, ll col) {
  ll i, j;
  for (i = start_row; i <= end_row; i++) {
    for (j = start_col; j <= end_col; j++) {
      if (((mp2.find({mp[i][j], mp[rw][col]}) == mp2.end()) &&
           (mp[i][j] != mp[rw][col])) &&
          (mp[i][j] != 0)) {
        v[mp[rw][col]].push_back(mp[i][j]);
        v[mp[i][j]].push_back(mp[rw][col]);
        mp2[{mp[i][j], mp[rw][col]}]++;
        mp2[{mp[rw][col], mp[i][j]}]++;
      }
    }
  }
}

ll bfs(ll ind, ll dest) {
  if (ind == dest)
    return 0;
  bool flag = false;
  ll i, j, z = 1, ans = 0;
  queue<ll> q;
  q.push(ind);
  mp3[ind]++;
  while (!q.empty()) {
    j = 0;
    z = q.size();
    while (j < z) {
      ll x = q.front();
      if (x == dest) {
        flag = true;
        break;
      }
      q.pop();
      ll p = v[x].size();
      for (i = 0; i < p; i++) {
        if (mp3[v[x][i]] == 0) {
          mp3[v[x][i]]++;
          q.push(v[x][i]);
        }
      }
      j++;
    }
    if (flag)
      break;
    ans++;
  }

  if (flag)
    return ans;
  return -1;
}
int main() {
  ll i, j, ind = 1;
  cin >> h >> w >> ch >> cw >> dh >> dw;

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> tb[i][j];
      mp[i][j] = 0;
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (mp[i][j] == 0 && (tb[i][j] == '.')) {
        dfs(i, j, ind);
        ind++;
      }
    }
  }

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      ll sr = max(i - 2, (ll)1);
      ll sc = max(j - 2, (ll)1);
      ll er = min(i + 2, h);
      ll ec = min(j + 2, w);
      if (tb[i][j] != '#')
        func(sr, sc, er, ec, i, j);
    }
  }

  /*for(i=1;i<=h;i++)
  {
          for(j=1;j<=w;j++)
          {
                  cout<<mp[i][j]<<" ";
          }
          cout<<"\\n";
  }



  for(i=1;i<=ind;i++)
  {
          cout<<i<<": ";
          for(j=0;j<v[i].size();j++)
          {
                  cout<<v[i][j]<<" ";
          }
          cout<<"\\n";
  }*/

  ll x = mp[ch][cw];
  ll y = mp[dh][dw];
  cout << bfs(x, y);
}