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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define num(x, y) (x * 1000 + y)
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
int pa[N];
int h, w;
string s[1005];
int d[N];
int find(int i) {
  if (pa[i] == i)
    return i;
  return pa[i] = find(pa[i]);
}
void join(int i, int j) {
  int a = find(i);
  int b = find(j);
  pa[a] = b;
}
bool check(int i, int j) {
  return i >= 0 && j >= 0 && i < h && j < w && s[i][j] == '.';
}
void solve() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < N; i++)
    pa[i] = i;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = dx[k] + i;
        int y = dy[k] + j;
        if (check(x, y)) {
          int p1 = find(num(x, y));
          int p2 = find(num(i, j));
          if (p1 == p2)
            continue;
          join(p1, p2);
        }
      }
    }
  }
  int src = find(num(ch, cw));
  int dest = find(num(dh, dw));
  set<int> ed[N];
  if (src == dest) {
    cout << 0;
    return;
  }
  for (int x = 0; x < h; x++)
    for (int y = 0; y < w; y++) {
      if (s[x][y] == '#')
        continue;
      int i = find(num(x, y));
      // cout<<i<<" ";
      for (int j = -2; j <= 2; j++)
        for (int k = -2; k <= 2; k++) {
          int x1 = x + j;
          int y1 = y + k;
          if (check(x1, y1)) {
            int p1 = i;
            int p2 = find(num(x1, y1));
            if (p1 == p2)
              continue;
            // cout<<p1<<" "<<p2<<"\\n";
            ed[p1].insert(p2);
            ed[p2].insert(p1);
            // join(p1,p2);
          }
        }
    }
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[src] = 0;
  // cout<<find(num(ch,cw))<<"c\\n";
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    int dd = d[u];
    q.pop();
    for (int v : ed[u]) {
      if (dd + 1 < d[v]) {
        d[v] = 1 + dd;
        q.push(v);
      }
    }
  }
  if (d[dest] >= inf)
    cout << -1;
  else
    cout << d[dest];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define num(x, y) (x * 1000 + y)
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 100005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
int pa[N];
int h, w;
string s[1005];
int d[N];
int find(int i) {
  if (pa[i] == i)
    return i;
  return pa[i] = find(pa[i]);
}
void join(int i, int j) { pa[i] = j; }
bool check(int i, int j) {
  return i >= 0 && j >= 0 && i < h && j < w && s[i][j] == '.';
}
void solve() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < N; i++)
    pa[i] = i;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = dx[k] + i;
        int y = dy[k] + j;
        if (check(x, y)) {
          int p1 = find(num(x, y));
          int p2 = find(num(i, j));
          if (p1 == p2)
            continue;
          join(p1, p2);
        }
      }
    }
  }
  int src = find(num(ch, cw));
  int dest = find(num(dh, dw));
  vector<int> v;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      if (find(num(i, j)) == num(i, j))
        v.pb(num(i, j));
    }
  set<int> ed[N];
  if (src == dest) {
    cout << 0;
    return;
  }
  for (int i : v) {
    int x = i / 1000;
    int y = i % 1000;
    // cout<<i<<" ";
    for (int j = -2; j <= 2; j++)
      for (int k = -2; k <= 2; k++) {
        int x1 = x + j;
        int y1 = y + k;
        if (check(x1, y1)) {
          int p1 = i;
          int p2 = find(num(x1, y1));
          if (p1 == p2)
            continue;
          // cout<<p1<<" "<<p2<<"\\n";
          ed[p1].insert(p2);
          ed[p2].insert(p1);
          // join(p1,p2);
        }
      }
  }
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[src] = 0;
  // cout<<find(num(ch,cw))<<"c\\n";
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    int dd = d[u];
    q.pop();
    for (int v : ed[u]) {
      if (dd + 1 < d[v]) {
        d[v] = 1 + dd;
        q.push(v);
      }
    }
  }
  if (d[dest] >= inf)
    cout << -1;
  else
    cout << d[dest];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}