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

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ll long long

int main() {
  fastio int h, w, sx, sy, ex, ey;
  vector<string> vals;
  string s;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    cin >> s;
    vals.push_back(s);
  }

  queue<vector<int>> q;
  vector<vector<bool>> vis(h, vector<bool>(w));

  int steps = 0;
  vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (!q.empty()) {
    set<vector<int>> nxt;
    while (!q.empty()) {
      auto top = q.front();
      q.pop();
      int x = top[0], y = top[1];
      if (x == ex && y == ey) {
        cout << steps;
        return 0;
      }
      for (auto d : dirs) {
        int nx = d[0] + x, ny = d[1] + y;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w || vals[nx][ny] == '#' ||
            vis[nx][ny])
          continue;
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
      // magic
      for (int j = -2; j <= 2; j++) {
        for (int k = -2; k <= 2; k++) {
          int nx = x + j, ny = y + k;
          if (nx < 0 || ny < 0 || nx >= h || ny >= w || vals[nx][ny] == '#' ||
              vis[nx][ny])
            continue;
          nxt.insert({nx, ny});
        }
      }
    }
    for (auto p : nxt) {
      int nx = p[0], ny = p[1];
      if (vis[nx][ny])
        continue;
      vis[nx][ny] = true;
      q.push({nx, ny});
    }
    steps++;
  }
  cout << -1;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ll long long

ll mod = 1e9 + 7;

int main() {
  fastio int h, w, sx, sy, ex, ey;
  vector<string> vals(h);
  string s;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++)
    cin >> vals[i];

  queue<vector<int>> q;
  bool vis[h][w];
  memset(vis, 0, sizeof vis);

  int steps = 0;
  vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (!q.empty()) {
    set<vector<int>> nxt;
    while (!q.empty()) {
      auto top = q.front();
      q.pop();
      int x = top[0], y = top[1];
      if (x == ex && y == ey) {
        cout << steps;
        return 0;
      }
      for (auto d : dirs) {
        int nx = d[0] + x, ny = d[1] + y;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w || vals[nx][ny] == '#' ||
            vis[nx][ny])
          continue;
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
      // magic
      for (int j = -2; j <= 2; j++) {
        for (int k = -2; k <= 2; k++) {
          int nx = x + j, ny = y + k;
          if (nx < 0 || ny < 0 || nx >= h || ny >= w || vals[nx][ny] == '#' ||
              vis[nx][ny])
            continue;
          nxt.insert({nx, ny});
        }
      }
    }
    for (auto &p : nxt) {
      int nx = p[0], ny = p[1];
      if (vis[nx][ny])
        continue;
      vis[nx][ny] = true;
      q.push({nx, ny});
    }
    steps++;
  }
  cout << -1;
}