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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

#define ll long long

const int mod = 1e9 + 7;
const ll INF = 1e14;

bool valid(int i, int j, int h, int w) {
  if (i < 0 || j < 0)
    return false;
  if (i >= h || j >= w)
    return false;
  return true;
}

void solve() {

  int h, w;
  cin >> h >> w;
  int x, y;
  cin >> x >> y;
  int dx, dy;
  cin >> dx >> dy;
  --x, --y, --dx, --dy;

  char arr[h][w];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> arr[i][j];

  vector<vector<bool>> vis(h, vector<bool>(w, false));
  set<pair<ll, pair<int, int>>> set;
  set.insert({0, {x, y}});
  vector<ll> wt(h * w, INF);
  wt[x * w + y] = (ll)0;

  while (!set.empty()) {
    pair<ll, pair<int, int>> it = *set.begin();
    set.erase(set.begin());
    int xc = it.second.first;
    int yc = it.second.second;

    vis[xc][yc] = true;

    for (int i : {-1, 0, 1}) {
      for (int j : {-1, 0, 1}) {
        if (abs(i) != abs(j) && valid(xc + i, yc + j, h, w)) {
          if (arr[xc + i][yc + j] == '.' && vis[xc + i][yc + j] == false) {
            if (wt[w * (xc + i) + yc + j] > wt[w * xc + yc]) {
              if (set.find({wt[w * (xc + i) + yc + j], {xc + i, yc + j}}) !=
                  set.end())
                set.erase({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
              wt[w * (xc + i) + yc + j] = wt[w * xc + yc];
              set.insert({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
            }
          }
        }
      }
    }
    for (int i : {-2, -1, 0, 1, 2}) {
      for (int j : {-2, -1, 0, 1, 2}) {
        if ((i != 0 || j != 0) && valid(xc + i, yc + j, h, w)) {
          if (arr[xc + i][yc + j] == '.' && !vis[xc + i][yc + j]) {
            if (wt[w * (xc + i) + yc + j] > wt[w * xc + yc] + 1) {
              if (set.find({wt[w * (xc + i) + yc + j], {xc + i, yc + j}}) !=
                  set.end())
                set.erase({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
              wt[w * (xc + i) + yc + j] = wt[w * xc + yc] + 1;
              set.insert({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
            }
          }
        }
      }
    }
  }
  cout << (wt[dx * w + dy] == INF ? -1 : wt[dx * w + dy]) << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

#define ll long long

const int INF = 1e9;

bool valid(int i, int j, int h, int w) {
  if (i < 0 || j < 0)
    return false;
  if (i >= h || j >= w)
    return false;
  return true;
}

void solve() {

  int h, w;
  cin >> h >> w;
  int x, y;
  cin >> x >> y;
  int dx, dy;
  cin >> dx >> dy;
  --x, --y, --dx, --dy;

  char arr[h][w];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> arr[i][j];

  vector<vector<bool>> vis(h, vector<bool>(w, false));
  set<pair<int, pair<int, int>>> set;
  set.insert({0, {x, y}});
  vector<int> wt(h * w, INF);
  wt[x * w + y] = 0;

  while (!set.empty()) {
    pair<int, pair<int, int>> it = *set.begin();
    set.erase(set.begin());
    int xc = it.second.first;
    int yc = it.second.second;

    vis[xc][yc] == true;

    for (int i : {-1, 0, 1}) {
      for (int j : {-1, 0, 1}) {
        if (i != j && arr[xc + i][yc + j] == '.' &&
            valid(xc + i, yc + j, h, w) && !vis[xc + i][yc + j]) {
          if (wt[w * (xc + i) + yc + j] > wt[w * xc + yc]) {
            if (set.find({wt[w * (xc + i) + yc + j], {xc + i, yc + j}}) !=
                set.end())
              set.erase({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
            wt[w * (xc + i) + yc + j] = wt[w * xc + yc];
            set.insert({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
          }
        }
      }
    }
    for (int i : {-2, -1, 0, 1, 2}) {
      for (int j : {-2, -1, 0, 1, 2}) {
        if (i != j && arr[xc + i][yc + j] == '.' &&
            valid(xc + i, yc + j, h, w) && !vis[xc + i][yc + j]) {
          if (wt[w * (xc + i) + yc + j] > wt[w * xc + yc]) {
            if (set.find({wt[w * (xc + i) + yc + j], {xc + i, yc + j}}) !=
                set.end())
              set.erase({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
            wt[w * (xc + i) + yc + j] = wt[w * xc + yc] + 1;
            set.insert({wt[w * (xc + i) + yc + j], {xc + i, yc + j}});
          }
        }
      }
    }
  }
  cout << (wt[dx * w + dy] == INF ? -1 : wt[dx * w + dy]) << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}