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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define inf 0x7fffffff
#define minn(x, y) x = min(x, y)
#define maxx(x, y) x = max(x, y)
using namespace std;
string s[1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int vis[1010][1010], n, m;
int dfs(int x, int y, int j) {
  vis[x][y] = j;
  for (int i = 0; i < 4; i++) {
    if (dx[i] + x >= 0 && dx[i] + x < n && dy[i] + y >= 0 && dy[i] + y < m &&
        s[x + dx[i]][y + dy[i]] == '.' && !vis[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i], j);
    }
  }
  return 0;
}
map<pii, int> ma;
int main() {
  int i, j, k, x, y, z, t, l, c1, c2, d1, d2;
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &c1, &c2, &d1, &d2);
  c1--;
  c2--;
  d1--;
  d2--;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  x = 1;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == '.' && !vis[i][j]) {
        dfs(i, j, x);
        x++;
      }
    }
  }
  vector<vector<int>> ve(x);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        for (k = max(0, i - 2); k <= min(i + 2, n - 1); k++) {
          for (l = max(0, j - 2); l <= min(j + 2, m - 1); l++) {
            if (vis[k][l] != vis[i][j] && vis[k][l]) {
              if (!ma.count(mp(vis[k][l], vis[i][j])) &&
                  !ma.count(mp(vis[i][j], vis[k][l]))) {
                ve[vis[k][l]].push_back(vis[i][j]);
                ve[vis[i][j]].push_back(vis[k][l]);
                ma[mp(vis[i][j], vis[k][l])] = 1;
              }
            }
          }
        }
      }
    }
  }
  queue<int> q;
  vector<int> vi(x);
  vector<int> d(x, 1e8);
  q.push(vis[c1][c2]);
  vi[vis[c1][c2]] = 1;
  d[vis[c1][c2]] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    vi[x] = 0;
    if (x == vis[d1][d2]) {
      printf("%d", d[x]);
      return 0;
    }
    for (i = 0; i < ve[x].size(); i++) {
      if (d[x] + 1 < d[ve[x][i]]) {
        d[ve[x][i]] = d[x] + 1;
        if (!vi[ve[x][i]]) {
          vi[ve[x][i]] = 1;
          q.push(ve[x][i]);
        }
      }
    }
  }
  printf("-1");
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define inf 0x7fffffff
#define minn(x, y) x = min(x, y)
#define maxx(x, y) x = max(x, y)
using namespace std;
string s[1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int vis[1010][1010], n, m;
int dfs(int x, int y, int j) {
  vis[x][y] = j;
  for (int i = 0; i < 4; i++) {
    if (dx[i] + x >= 0 && dx[i] + x < n && dy[i] + y >= 0 && dy[i] + y < m &&
        s[x + dx[i]][y + dy[i]] == '.' && !vis[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i], j);
    }
  }
}
map<pii, int> ma;
int main() {
  int i, j, k, x, y, z, t, l, c1, c2, d1, d2;
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &c1, &c2, &d1, &d2);
  c1--;
  c2--;
  d1--;
  d2--;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  x = 1;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == '.' && !vis[i][j]) {
        dfs(i, j, x);
        x++;
      }
    }
  }
  vector<vector<int>> ve(x);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        for (k = max(0, i - 2); k <= min(i + 2, n - 1); k++) {
          for (l = max(0, j - 2); l <= min(j + 2, m - 1); l++) {
            if (vis[k][l] != vis[i][j] && vis[k][l]) {
              if (!ma.count(mp(vis[k][l], vis[i][j])) &&
                  !ma.count(mp(vis[i][j], vis[k][l]))) {
                ve[vis[k][l]].push_back(vis[i][j]);
                ve[vis[i][j]].push_back(vis[k][l]);
                ma[mp(vis[i][j], vis[k][l])] = 1;
              }
            }
          }
        }
      }
    }
  }
  queue<int> q;
  vector<int> vi(x);
  vector<int> d(x, 1e8);
  q.push(vis[c1][c2]);
  vi[vis[c1][c2]] = 1;
  d[vis[c1][c2]] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    vi[x] = 0;
    if (x == vis[d1][d2]) {
      printf("%d", d[x]);
      return 0;
    }
    for (i = 0; i < ve[x].size(); i++) {
      if (d[x] + 1 < d[ve[x][i]]) {
        d[ve[x][i]] = d[x] + 1;
        if (!vi[ve[x][i]]) {
          vi[ve[x][i]] = 1;
          q.push(ve[x][i]);
        }
      }
    }
  }
  printf("-1");
}