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

#include <bits/stdc++.h>

// @author : sam000013

#define int long long
#define PI 3.14159265
#define fi(i, start, end) for (int i = 0; i < n; i++)
#define pb(x) push_back(x)
#define int long long
#define PI 3.14159265
#define all(v) v.begin(), v.end()
#define endl cout << "\\n"
#define ll long long
#define dbg(x)                                                                 \\
  cout << #x << " " << x;                                                      \\
  endl;
#define bye                                                                    \\
  endl;                                                                        \\
  return
#define uni 1.000000
using namespace std;

int mod = 1000000007;
template <typename T>

void debug(vector<T> x) {
  for (auto e : x) {
    cout << e << " ";
  }
  cout << "\\n";
}
/*
1
8 6
1 2 4 1 2 4 1 2

*/
bool isValid(int i, int j, int n, int m) {
  return (i >= 0 && i < n && j >= 0 && j < m);
}
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};

void solve() {

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<vector<char>> maze(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> maze[i][j];
  }
  deque<tuple<int, int, int>> q;
  q.push_front(make_tuple(ch, cw, 0));
  vector<vector<bool>> vis(h, vector<bool>(w));
  int m, c, r;
  while (!q.empty()) {
    tie(c, r, m) = q.front();
    // cout<<c<<" "<<r<<" "<<m;endl;
    q.pop_front();
    if (vis[c][r])
      continue;
    vis[c][r] = 1;
    if (c == dh && r == dw) {
      cout << m;
      bye;
    }
    for (int i = 0; i < 4; i++) {
      int nc = c + dx[i], nr = r + dy[i];
      if (isValid(nc, nr, h, w) && !vis[nc][nr] && maze[nc][nr] == '.') {
        // cout<<"No magic nc: "<<nc<<" nr: "<<nr;endl;
        q.push_front(make_tuple(nc, nr, m));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nc = c + i, nr = r + j;
        if (isValid(nc, nr, h, w) && !vis[nc][nr] && maze[nc][nr] == '.') {
          q.push_back(make_tuple(nc, nr, m + 1));
          // cout<<"nc: "<<nc<<" nr: "<<nr;endl;
        }
      }
    }
  }
  cout << -1;
  bye;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// @author : sam000013

#define int long long
#define PI 3.14159265
#define fi(i, start, end) for (int i = 0; i < n; i++)
#define pb(x) push_back(x)
#define int long long
#define PI 3.14159265
#define all(v) v.begin(), v.end()
#define endl cout << "\\n"
#define ll long long
#define dbg(x)                                                                 \\
  cout << #x << " " << x;                                                      \\
  endl;
#define bye                                                                    \\
  endl;                                                                        \\
  return
#define uni 1.000000
using namespace std;

int mod = 1000000007;
template <typename T>

void debug(vector<T> x) {
  for (auto e : x) {
    cout << e << " ";
  }
  cout << "\\n";
}
/*
1
8 6
1 2 4 1 2 4 1 2

*/
bool isValid(int i, int j, int n, int m) {
  return (i >= 0 && i < n && j >= 0 && j < m);
}
vector<int> dx = {1, 1, 1, 0, 0, -1, -1, -1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

void solve() {

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  vector<vector<char>> maze(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> maze[i][j];
  }
  int ans = 1e18;
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(0, ch - 1, cw - 1));
  vector<vector<bool>> visited(h, vector<bool>(w));
  int m, i, j;
  while (!q.empty()) {
    tie(m, i, j) = q.front();
    q.pop();
    if (!visited[i][j]) {
      visited[i][j] = true;
      if (i == dh - 1 && j == dw - 1) {
        ans = min(m, ans);
      }
      q.pop();
    }
    bool moved = false;
    for (int adj = 0; adj < 8; adj++) {
      int neighborX = i + dx[adj], neighborY = j + dy[adj];
      if (isValid(neighborX, neighborY, h, w) &&
          !visited[neighborX][neighborY] && maze[neighborX][neighborY] != '#') {
        q.push(make_tuple(m, neighborX, neighborY));
        moved = true;
      }
    }

    if (!moved) {
      for (int neighborX = -2; neighborX <= 2; neighborX++) {
        for (int neighborY = -2; neighborY <= 2; neighborY++) {
          if (isValid(neighborX + i, neighborY + j, h, w) &&
              !visited[neighborX + i][neighborY + j] &&
              maze[neighborX + i][neighborY + j] == '.') {
            q.push(make_tuple(m + 1, neighborX + i, neighborY + j));
          }
        }
      }
    }
  }
  if (ans == 1e18) {
    cout << -1;
    bye;
  }
  cout << ans;
  bye;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}