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

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;

int h, w;
bool inside(int x, int y) { return x >= 1 && x <= h && y >= 1 && y <= w; }

int32_t main() {
  fast;
  cin >> h >> w;

  int A[2], B[2];
  cin >> A[0] >> A[1];
  cin >> B[0] >> B[1];

  char s[h + 1][w + 1];
  f(i, 1, h + 1) f(j, 1, w + 1) cin >> s[i][j];

  int d[h + 1][w + 1];
  f(i, 0, h + 1) f(j, 0, w + 1) d[i][j] = inf;

  set<pair<int, pii>> q;
  q.insert({0, {A[0], A[1]}});
  d[A[0]][A[1]] = 0;

  while (!q.empty()) {
    auto p = *q.begin();
    q.erase(p);

    int x = p.S.F;
    int y = p.S.S;

    if (inside(x + 1, y) && s[x + 1][y] == '.') {
      if (d[x][y] < d[x + 1][y]) {
        if (q.find({d[x + 1][y], {x + 1, y}}) != q.end())
          q.erase({d[x + 1][y], {x + 1, y}});
        d[x + 1][y] = d[x][y];
        q.insert({d[x + 1][y], {x + 1, y}});
      }
    }

    if (inside(x - 1, y) && s[x - 1][y] == '.') {
      if (d[x][y] < d[x - 1][y]) {
        if (q.find({d[x - 1][y], {x - 1, y}}) != q.end())
          q.erase({d[x - 1][y], {x - 1, y}});
        d[x - 1][y] = d[x][y];
        q.insert({d[x - 1][y], {x - 1, y}});
      }
    }

    if (inside(x, y + 1) && s[x][y + 1] == '.') {
      if (d[x][y] < d[x][y + 1]) {
        if (q.find({d[x][y + 1], {x, y + 1}}) != q.end())
          q.erase({d[x][y + 1], {x, y + 1}});
        d[x][y + 1] = d[x][y];
        q.insert({d[x][y + 1], {x, y + 1}});
      }
    }

    if (inside(x, y - 1) && s[x][y - 1] == '.') {
      if (d[x][y] < d[x][y - 1]) {
        if (q.find({d[x][y - 1], {x, y - 1}}) != q.end())
          q.erase({d[x][y - 1], {x, y - 1}});
        d[x][y - 1] = d[x][y];
        q.insert({d[x][y - 1], {x, y - 1}});
      }
    }

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (inside(i, j) && s[i][j] == '.') {
          if (d[x][y] + 1 < d[i][j]) {
            if (q.find({d[i][j], {i, j}}) != q.end())
              q.erase({d[i][j], {i, j}});
            d[i][j] = d[x][y] + 1;
            q.insert({d[i][j], {i, j}});
          }
        }
      }
    }
  }

  int ans = d[B[0]][B[1]];
  if (ans > 1e7)
    ans = -1;
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;

int h, w;
bool inside(int x, int y) { return x >= 1 && x <= h && y >= 1 && y <= w; }

int32_t main() {
  fast;
  cin >> h >> w;

  int A[2], B[2];
  cin >> A[0] >> A[1];
  cin >> B[0] >> B[1];

  char s[h + 1][w + 1];
  f(i, 1, h + 1) f(j, 1, w + 1) cin >> s[i][j];

  vector<int> d(1001011, inf);

  int start = (A[0] - 1) * 1010 + A[1];
  int end = (B[0] - 1) * 1010 + B[1];
  d[start] = 0;

  deque<int> q;

  q.push_front(start);

  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    int x = u / 1010 + 1;
    int y = u % 1010;

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (!(x == i && y == j) && inside(i, j) && s[i][j] == '.') {
          int num = (i - 1) * 1010 + j;
          if (d[u] + 1 < d[num]) {
            d[num] = d[u] + 1;
            q.push_back(num);
          }
        }
      }
    }

    if (inside(x + 1, y) && s[x + 1][y] == '.') {
      int num = (x)*1010 + y;
      if (d[u] < d[num]) {
        d[num] = d[u];
        q.push_front(num);
      }
    }

    if (inside(x, y + 1) && s[x][y + 1] == '.') {
      int num = (x - 1) * 1010 + y + 1;
      if (d[u] < d[num]) {
        d[num] = d[u];
        q.push_front(num);
      }
    }
  }

  int ans = d[end];
  if (ans > 1e7)
    ans = -1;
  cout << ans << "\\n";
}