Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a, b, k, x1, y_1, x2, y2, d[500005], v[1005][1005];
bool n[1005][1005], vi[500005];
char c;
struct st {
  int w, v;
  st(int V, int W) {
    w = W;
    v = V;
  }
};
struct st1 {
  int w, dd;
  st1() {}
  st1(int W, int DD) {
    w = W;
    dd = DD;
  }
  friend bool operator<(st1 x, st1 y) { return x.dd > y.dd; }
};
priority_queue<st1> q;
vector<st> ans[2505];
int Dijkstra(int s, int t) {
  memset(d, 0x3f, sizeof(d));
  memset(vi, 0, sizeof(vi));
  d[s] = 0;
  q.push(st1(s, 0));
  while (q.size()) {
    int x = q.top().w;
    q.pop();
    if (vi[x]) {
      continue;
    }
    vi[x] = 1;
    int siz = ans[x].size();
    for (int i = 0; i < siz; i++) {
      int vr = ans[x][i].v, w = ans[x][i].w;
      if (d[vr] > d[x] + w) {
        d[vr] = d[x] + w;
        q.push(st1(vr, d[vr]));
      }
    }
  }
  return d[t];
}
void dfs(int x, int y) {
  if (x > 1 && v[x - 1][y] == 0 && n[x - 1][y] == 0) {
    n[x - 1][y] = 1;
    v[x - 1][y] = k;
    dfs(x - 1, y);
    n[x - 1][y] = 0;
  }
  if (x < a && v[x + 1][y] == 0 && n[x + 1][y] == 0) {
    n[x + 1][y] = 1;
    v[x + 1][y] = k;
    dfs(x + 1, y);
    n[x + 1][y] = 0;
  }
  if (y > 1 && v[x][y - 1] == 0 && n[x][y - 1] == 0) {
    n[x][y - 1] = 1;
    v[x][y - 1] = k;
    dfs(x, y - 1);
    n[x][y - 1] = 0;
  }
  if (y < b && v[x][y + 1] == 0 && n[x][y + 1] == 0) {
    n[x][y + 1] = 1;
    v[x][y + 1] = k;
    dfs(x, y + 1);
    n[x][y + 1] = 0;
  }
}
int main() {
  scanf("%d %d\n", &a, &b);
  scanf("%d %d\n", &x2, &y2);
  scanf("%d %d", &x1, &y_1);
  for (int i = 1; i <= a; i++) {
    scanf("\n");
    for (int j = 1; j <= b; j++) {
      scanf("%c", &c);
      if (c == '#')
        n[i][j] = 1;
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (v[i][j] == 0 && n[i][j] == 0) {
        k++;
        v[i][j] = k;
        n[i][j] = 1;
        dfs(i, j);
        n[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      for (int i1 = max(1, i - 2); i1 <= min(a, i + 2); i1++) {
        for (int j1 = max(1, j - 2); j1 <= min(b, j + 2); j1++) {
          if (v[i][j] != 0 && v[i1][j1] != 0 && v[i][j] != v[i1][j1]) {
            ans[v[i][j]].push_back(st(v[i1][j1], 1));
            ans[v[i1][j1]].push_back(st(v[i][j], 1));
          }
        }
      }
    }
  }
  if (Dijkstra(v[x1][y_1], v[x2][y2]) == 0x3f3f3f3f) {
    printf("-1");
  } else
    printf("%d", Dijkstra(v[x1][y_1], v[x2][y2]));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a, b, k, x1, y_1, x2, y2, d[500005], v[1005][1005];
bool n[1005][1005], vi[500005];
char c;
struct st {
  int w, v;
  st(int V, int W) {
    w = W;
    v = V;
  }
};
struct st1 {
  int w, dd;
  st1() {}
  st1(int W, int DD) {
    w = W;
    dd = DD;
  }
  friend bool operator<(st1 x, st1 y) { return x.dd > y.dd; }
};
priority_queue<st1> q;
vector<st> ans[500005];
int Dijkstra(int s, int t) {
  memset(d, 0x3f, sizeof(d));
  memset(vi, 0, sizeof(vi));
  d[s] = 0;
  q.push(st1(s, 0));
  while (q.size()) {
    int x = q.top().w;
    q.pop();
    if (vi[x]) {
      continue;
    }
    vi[x] = 1;
    int siz = ans[x].size();
    for (int i = 0; i < siz; i++) {
      int vr = ans[x][i].v, w = ans[x][i].w;
      if (d[vr] > d[x] + w) {
        d[vr] = d[x] + w;
        q.push(st1(vr, d[vr]));
      }
    }
  }
  return d[t];
}
void dfs(int x, int y) {
  if (x > 1 && v[x - 1][y] == 0 && n[x - 1][y] == 0) {
    n[x - 1][y] = 1;
    v[x - 1][y] = k;
    dfs(x - 1, y);
    n[x - 1][y] = 0;
  }
  if (x < a && v[x + 1][y] == 0 && n[x + 1][y] == 0) {
    n[x + 1][y] = 1;
    v[x + 1][y] = k;
    dfs(x + 1, y);
    n[x + 1][y] = 0;
  }
  if (y > 1 && v[x][y - 1] == 0 && n[x][y - 1] == 0) {
    n[x][y - 1] = 1;
    v[x][y - 1] = k;
    dfs(x, y - 1);
    n[x][y - 1] = 0;
  }
  if (y < b && v[x][y + 1] == 0 && n[x][y + 1] == 0) {
    n[x][y + 1] = 1;
    v[x][y + 1] = k;
    dfs(x, y + 1);
    n[x][y + 1] = 0;
  }
}
int main() {
  scanf("%d %d\n", &a, &b);
  scanf("%d %d\n", &x2, &y2);
  scanf("%d %d", &x1, &y_1);
  for (int i = 1; i <= a; i++) {
    scanf("\n");
    for (int j = 1; j <= b; j++) {
      scanf("%c", &c);
      if (c == '#')
        n[i][j] = 1;
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (v[i][j] == 0 && n[i][j] == 0) {
        k++;
        v[i][j] = k;
        n[i][j] = 1;
        dfs(i, j);
        n[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      for (int i1 = max(1, i - 2); i1 <= min(a, i + 2); i1++) {
        for (int j1 = max(1, j - 2); j1 <= min(b, j + 2); j1++) {
          if (v[i][j] != 0 && v[i1][j1] != 0 && v[i][j] != v[i1][j1]) {
            ans[v[i][j]].push_back(st(v[i1][j1], 1));
            ans[v[i1][j1]].push_back(st(v[i][j], 1));
          }
        }
      }
    }
  }
  if (Dijkstra(v[x1][y_1], v[x2][y2]) == 0x3f3f3f3f) {
    printf("-1");
  } else
    printf("%d", Dijkstra(v[x1][y_1], v[x2][y2]));
}