Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MAX = 100;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int ei[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
                  0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
const int ej[] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                  1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
#define PI 3.14159265358979323846264338327950L

#define WHITE 0
#define GRAY 1
#define BLACK 2

int d[MAX][MAX];

void dijkstra(vector<string> s, int h, int w, int x, int y) {
  priority_queue<tuple<int, int, int>> PQ;
  int color[MAX][MAX];
  rep(i, MAX) {
    rep(j, MAX) {
      d[i][j] = INF;
      color[i][j] = WHITE;
    }
  }
  d[y][x] = 0;
  PQ.push(make_tuple(0, x, y));
  color[y][x] = GRAY;
  while (!PQ.empty()) {
    tuple<int, int, int> f = PQ.top();
    PQ.pop();
    int c = get<0>(f);
    int xx = get<1>(f);
    int yy = get<2>(f);
    color[yy][xx] = BLACK;
    if (d[yy][xx] < c * (-1))
      continue;
    rep(v, 4) {
      int nx = xx + dj[v];
      int ny = yy + di[v];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (color[ny][nx] == BLACK)
        continue;
      if (d[ny][nx] > d[yy][xx]) {
        d[ny][nx] = d[yy][xx];
        PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
        color[ny][nx] = GRAY;
      }
    }
    rep(v, 25) {
      int nx = xx + ej[v];
      int ny = yy + ei[v];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (color[ny][nx] == BLACK)
        continue;
      if (d[ny][nx] > d[yy][xx] + 1) {
        d[ny][nx] = d[yy][xx] + 1;
        PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
        color[ny][nx] = GRAY;
      }
    }
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, tx, ty;
  cin >> sy >> sx >> ty >> tx;
  sx--;
  sy--;
  tx--;
  ty--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  dijkstra(s, h, w, sx, sy);
  if (d[ty][tx] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[ty][tx] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MAX = 1000;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int ei[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
                  0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
const int ej[] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                  1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
#define PI 3.14159265358979323846264338327950L

#define WHITE 0
#define GRAY 1
#define BLACK 2

int d[MAX][MAX];

void dijkstra(vector<string> s, int h, int w, int x, int y) {
  priority_queue<tuple<int, int, int>> PQ;
  int color[MAX][MAX];
  rep(i, MAX) {
    rep(j, MAX) {
      d[i][j] = INF;
      color[i][j] = WHITE;
    }
  }
  d[y][x] = 0;
  PQ.push(make_tuple(0, x, y));
  color[y][x] = GRAY;
  while (!PQ.empty()) {
    tuple<int, int, int> f = PQ.top();
    PQ.pop();
    int c = get<0>(f);
    int xx = get<1>(f);
    int yy = get<2>(f);
    color[yy][xx] = BLACK;
    if (d[yy][xx] < c * (-1))
      continue;
    rep(v, 4) {
      int nx = xx + dj[v];
      int ny = yy + di[v];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (color[ny][nx] == BLACK)
        continue;
      if (d[ny][nx] > d[yy][xx]) {
        d[ny][nx] = d[yy][xx];
        PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
        color[ny][nx] = GRAY;
      }
    }
    rep(v, 25) {
      int nx = xx + ej[v];
      int ny = yy + ei[v];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (color[ny][nx] == BLACK)
        continue;
      if (d[ny][nx] > d[yy][xx] + 1) {
        d[ny][nx] = d[yy][xx] + 1;
        PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
        color[ny][nx] = GRAY;
      }
    }
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, tx, ty;
  cin >> sy >> sx >> ty >> tx;
  sx--;
  sy--;
  tx--;
  ty--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  dijkstra(s, h, w, sx, sy);
  if (d[ty][tx] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[ty][tx] << endl;
  }
}