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

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using ii = pair<int, int>;

int dijkstra(vector<vector<char>> graph, int sourcex, int sourcey, int endx,
             int endy) {
  priority_queue<pair<int, ii>> pq;
  set<pair<int, int>> visited;

  pq.push(make_pair(0, ii(sourcex, sourcey)));

  while (!pq.empty()) {
    pair<int, ii> val = pq.top();
    pq.pop();
    int d = val.first;
    int nodex = val.second.first;
    int nodey = val.second.second;

    if (visited.find(make_pair(nodex, nodey)) != visited.end()) {
      continue;
    }
    if (nodex == endx && nodey == endy) {
      return -d;
    }
    if (nodex > 0 && graph[nodex - 1][nodey] == '.') {
      pq.push(make_pair(d, ii(nodex - 1, nodey)));
    }
    if (nodex + 1 < graph.size() && graph[nodex + 1][nodey] == '.') {
      pq.push(make_pair(d, ii(nodex + 1, nodey)));
    }
    if (nodey > 0 && graph[nodex][nodey - 1] == '.') {
      pq.push(make_pair(d, ii(nodex, nodey - 1)));
    }
    if (nodey + 1 < graph[0].size() && graph[nodex][nodey + 1] == '.') {
      pq.push(make_pair(d, ii(nodex, nodey + 1)));
    }
    for (int i = nodex - 2; i <= nodex + 2; i++) {
      if (i < 0 || i >= graph.size()) {
        continue;
      }
      for (int j = nodey - 2; j <= nodey + 2; j++) {
        if (j < 0 || j >= graph[0].size()) {
          continue;
        }
        if (graph[i][j] == '.') {
          pq.push(make_pair(d - 1, ii(i, j)));
        }
      }
    }
    visited.insert(make_pair(nodex, nodey));
  }
  return -1;
}

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  cin.get();
  vector<vector<char>> maze(W, vector<char>(H));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin.get(maze[j][i]);
    }
    cin.get();
  }
  cout << dijkstra(maze, Cw, Ch, Dw, Dh);
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using ii = pair<int, int>;

int dijkstra(vector<vector<char>> graph, int sourcex, int sourcey, int endx,
             int endy) {
  priority_queue<pair<int, ii>> pq;
  set<pair<int, int>> visited;

  pq.push(make_pair(0, ii(sourcex, sourcey)));

  while (!pq.empty()) {
    pair<int, ii> val = pq.top();
    pq.pop();
    int d = val.first;
    int nodex = val.second.first;
    int nodey = val.second.second;

    if (visited.find(make_pair(nodex, nodey)) != visited.end()) {
      continue;
    }
    if (nodex == endx && nodey == endy) {
      return -d;
    }
    if (nodex > 0 && graph[nodex - 1][nodey] == '.') {
      pq.push(make_pair(d, ii(nodex - 1, nodey)));
    }
    if (nodex + 1 < graph.size() && graph[nodex + 1][nodey] == '.') {
      pq.push(make_pair(d, ii(nodex + 1, nodey)));
    }
    if (nodey > 0 && graph[nodex][nodey - 1] == '.') {
      pq.push(make_pair(d, ii(nodex, nodey - 1)));
    }
    if (nodey + 1 < graph[0].size() && graph[nodex][nodey + 1] == '.') {
      pq.push(make_pair(d, ii(nodex, nodey + 1)));
    }
    for (int i = nodex - 2; i <= nodex + 2; i++) {
      if (i < 0 || i >= graph.size()) {
        continue;
      }
      for (int j = nodey - 2; j <= nodey + 2; j++) {
        if (j < 0 || j >= graph[0].size()) {
          continue;
        }
        if (graph[i][j] == '.') {
          pq.push(make_pair(d - 1, ii(i, j)));
        }
      }
    }
    visited.insert(make_pair(nodex, nodey));
  }
}

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  cin.get();
  vector<vector<char>> maze(W, vector<char>(H));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin.get(maze[j][i]);
    }
    cin.get();
  }
  cout << dijkstra(maze, Cw, Ch, Dw, Dh);
}