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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1e3 + 3;
const int mod = 1e9 + 7;

int x, y, xx, yy, n, m;

char a[N][N];
bool vis[N][N];
int cost[N][N];

void dijk(int r, int c) {
  priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
                 greater<pair<ll, pair<int, int>>>>
      q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cost[i][j] = 1e9;
    }
  }
  cost[r][c] = 0;
  q.push({0, {r, c}});
  while (q.empty() == false) {

    ll r = q.top().second.first;
    ll c = q.top().second.second;
    ll curcost = q.top().first;
    q.pop();
    if (vis[r][c])
      continue;
    vis[r][c] = true;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = r + i;
        int nc = c + j;
        int dis = 1;
        if ((abs(nr - r) == 1 && abs(nc - c) == 0) ||
            (abs(nr - r) == 0 && abs(nc - c) == 1))
          dis = 0;
        if (nr >= n || nc >= m || nc < 0 || nr < 0 || a[nr][nc] == '#' ||
            (nr == r && nc == c))
          continue;
        if (curcost + dis < cost[nr][nc]) {
          cost[nr][nc] = curcost + dis;
          q.push({cost[nr][nc], {nr, nc}});
        }
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  cin >> x >> y;
  cin >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  dijk(x, y);
  cout << (cost[xx][yy] == 1e9 ? -1 : cost[xx][yy]);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1e3 + 3;
const int mod = 1e9 + 7;

int x, y, xx, yy, n, m;

char a[N][N];
bool vis[N][N];
ll cost[N][N];

void dijk(int r, int c) {
  priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
                 greater<pair<ll, pair<int, int>>>>
      q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cost[i][j] = 1e9;
    }
  }
  cost[r][c] = 0;
  q.push({0, {r, c}});
  while (q.empty() == false) {

    ll r = q.top().second.first;
    ll c = q.top().second.second;
    ll curcost = q.top().first;
    q.pop();
    if (vis[r][c])
      continue;
    vis[r][c] = true;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = r + i;
        int nc = c + j;
        int c = 1;
        if ((abs(nr - r) == 1 && abs(nc - c) == 0) ||
            (abs(nr - r) == 0 && abs(nc - c) == 1))
          c = 0;
        if (r >= n || c >= m || c < 0 || r < 0 || a[r][c] == '#')
          continue;
        if (curcost + c < cost[nr][nc]) {
          cost[nr][nc] = curcost + c;
          q.push({cost[nr][nc], {nr, nc}});
        }
      }
    }
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  cin >> x >> y;
  cin >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  dijk(x, y);
  cout << (cost[xx][yy] == 1e9 ? -1 : cost[xx][yy]);
}