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

#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MP make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(x) x.size()
#define all(c) c.begin(), c.end()
#define int long long
#define fr(i, j, x) for (int i = j; i < x; i++)
#define rep(i, j, x) for (int i = j; i <= x; i++)
#define mem(x, val) memset(x, val, sizeof(x))
// #define endl '\\n'
#define vi vector<int>
#define pii pair<int, int>
#define piii pair<int, pii>

const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int MAXN = 2e6 + 10;
const int INF = 1e18;

char grid[1010][1010];
int dis[1010][1010], h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int val(int x, int y) {
  if (x >= 1 && y >= 1 && x <= h && y <= w && grid[x][y] != '#') {
    return 1;
  }
  return 0;
}

void solve() {
  int s, e, fs, fe;
  cin >> h >> w >> s >> e >> fs >> fe;
  rep(i, 1, h) {
    rep(j, 1, w) {
      cin >> grid[i][j];
      dis[i][j] = -1;
    }
  }
  deque<pii> q;
  q.push_back({s, e});
  dis[s][e] = 0;
  while (!q.empty()) {
    auto x = q.front().ff;
    auto y = q.front().ss;
    q.pop_front();
    // if(vis[x][y]){
    //     continue;
    // }
    // if(x==fs && y==es){
    //     break;
    // }
    rep(i, 0, 3) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (val(nx, ny)) {
        int vl = dis[x][y];
        if (dis[nx][ny] == -1 || dis[nx][ny] > vl) {
          dis[nx][ny] = vl;
          q.push_front({nx, ny});
        }
      }
    }
    rep(i, -2, 2) {
      rep(j, -2, 2) {
        int nx = x + i;
        int ny = y + j;
        if (val(nx, ny)) {
          int vl = dis[x][y] + 1;
          if (dis[nx][ny] == -1 || dis[nx][ny] > vl) {
            dis[nx][ny] = vl;
            q.push_back({nx, ny});
          }
        }
      }
    }
  }
  cout << dis[fs][fe] << endl;
}

int32_t main() {
  //     #ifndef ONLINE_JUDGE
  //         freopen("inputf.in", "r", stdin);
  //         //freopen("outputf.in", "w", stdout);
  //     #endif
  SPEED;
  int T = 1;
  // cin>>T;
  int t1 = 1;
  while (true) {
    // cout<<"Case #"<<t1<<": ";
    solve();
    t1++;
    if (t1 > T)
      break;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define MP make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(x) x.size()
#define all(c) c.begin(), c.end()
#define int long long
#define fr(i, j, x) for (int i = j; i < x; i++)
#define rep(i, j, x) for (int i = j; i <= x; i++)
#define mem(x, val) memset(x, val, sizeof(x))
// #define endl '\\n'
#define vi vector<int>
#define pii pair<int, int>
#define piii pair<int, pii>

const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int MAXN = 2e6 + 10;
const int INF = 1e18;

char grid[1010][1010];
int dis[1010][1010], h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int val(int x, int y) {
  if (x >= 1 && y >= 1 && x <= h && y <= w && grid[x][y] != '#') {
    return 1;
  }
  return 0;
}

void solve() {
  int s, e, fs, fe;
  cin >> h >> w >> s >> e >> fs >> fe;
  rep(i, 1, h) {
    rep(j, 1, w) {
      cin >> grid[i][j];
      dis[i][j] = -1;
    }
  }
  deque<pii> q;
  q.push_back({s, e});
  dis[s][e] = 0;
  while (!q.empty()) {
    auto x = q.front().ff;
    auto y = q.front().ss;
    q.pop_front();
    // if(vis[x][y]){
    //     continue;
    // }
    // if(x==fs && y==es){
    //     break;
    // }
    rep(i, 0, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (val(nx, ny)) {
        int vl = dis[x][y];
        if (dis[nx][ny] == -1 || dis[nx][ny] > vl) {
          dis[nx][ny] = vl;
          q.push_front({nx, ny});
        }
      }
    }
    rep(i, -2, 2) {
      rep(j, -2, 2) {
        if (i == 0 || j == 0) {
          continue;
        }
        int nx = x + i;
        int ny = y + j;
        if (val(nx, ny)) {
          int vl = dis[x][y] + 1;
          if (dis[nx][ny] == -1 || dis[nx][ny] > vl) {
            dis[nx][ny] = vl;
            q.push_back({nx, ny});
          }
        }
      }
    }
  }
  cout << dis[fs][fe] << endl;
}

int32_t main() {
  //     #ifndef ONLINE_JUDGE
  //         freopen("inputf.in", "r", stdin);
  //         //freopen("outputf.in", "w", stdout);
  //     #endif
  SPEED;
  int T = 1;
  // cin>>T;
  int t1 = 1;
  while (true) {
    // cout<<"Case #"<<t1<<": ";
    solve();
    t1++;
    if (t1 > T)
      break;
  }
}