alexjercan/bugnet huggingface dataset:
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int people;
  cin >> people;

  unsigned long int *row;
  row = (unsigned long int *)malloc(people * sizeof(unsigned long int));

  for (int i = 0; i < people; i++) {
    cin >> row[i];
  }
  long long stool = 0;
  for (int i = 0; i < people - 1; i++) {
    if (row[i] > row[i + 1]) {
      stool += row[i] - row[i + 1];
      row[i + 1] += row[i] - row[i + 1];
    }
  }
  cout << stool;

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int people;
  cin >> people;

  long int *row;
  row = (long int *)malloc(people * sizeof(int));

  for (int i = 0; i < people; i++) {
    cin >> row[i];
  }
  int stool = 0;
  for (int i = 0; i < people - 1; i++) {
    if (row[i] > row[i + 1]) {
      stool += row[i] - row[i + 1];
      row[i + 1] += row[i] - row[i + 1];
    }
  }
  cout << stool;

  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

int a[200005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long ans = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

int a[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long ans = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int a, b, c, d, tmp, max;
  string s;
  long long ans;
  a = b = c = d = ans = tmp = 0;

  cin >> a;
  vector<int> x(a);
  for (int i = 0; i < a; i++) {
    cin >> x[i];
  }

  for (int i = 1; i < a; i++) {
    if (x[i] < x[i - 1]) {
      tmp = x[i - 1] - x[i];
      x[i] += tmp;
      ans += tmp;
    }
  }
  cout << ans;
}


Error: -11

Fixed submission:

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

int main() {
  int a, b, c, d, ans, tmp, max;
  string s;

  a = b = c = d = ans = tmp = 0;

  cin >> a;
  vector<int> x(a);
  for (int i = 0; i < a; i++) {
    cin >> x[i];
  }

  tmp = x.size();
  for (int i = tmp; i >= -1; i--) {
    d++;
    max = *std::max_element(x.begin(), x.end() - d);
    if (x[i] < max) {
      ans += max = x[i];
    }
  }
  cout << ans;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL a[200005];
LL ans = 0;
int main() {
  LL n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%lld", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[10005];
int ans = 0;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%d", ans);
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
using ll = long long;
int n;
ll t;
int a[200002];
void inp() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < a[i - 1]) {
      t += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
}
void process() { cout << t; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("test.inp","r", stdin);
  inp();
  process();
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
using ll = long long;
int n, i, t;
int a[100001];
void inp() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < a[i - 1]) {
      t += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
}
void process() { cout << t; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("test.inp","r", stdin);
  inp();
  process();
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, lst = 0, a;
  ll s = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    if (a > lst)
      lst = a;
    else
      s += lst - a;
  }
  cout << s;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, s = 0;
  int arr[n];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  for (int i = 0; i < n - 1; ++i)
    if (arr[n - 1] - arr[i] > 0)
      s++;

  cout << s;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define nl endl
#define in cin
#define out cout
#define ll long long
// sort(y,y+j, greater<int>());
ll x[1000000], y[1000000], a[1000000];
int main() {
  ll b, c, d, e, f, i, j, n, p;
  in >> n;
  for (i = 0; i < n; i++) {
    in >> a[i];
  }
  ll count = 0;
  for (i = 1; i < n; i++) {

    if (a[i] < a[i - 1]) {
      p = (a[i - 1] - a[i]);

      count += p;
      a[i] += p;
    }
  }
  out << count << nl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define nl endl
#define in cin
#define out cout
#define ll long long
// sort(y,y+j, greater<int>());
ll x[1000000], y[1000000];
int main() {
  ll a[100000], b, c, d, e, f, i, j, n, p;
  in >> n;
  for (i = 0; i < n; i++) {
    in >> a[i];
  }
  int count = 0;
  for (int i = 1; i < n; i++) {

    if (a[i] < a[i - 1]) {
      int p = (a[i - 1] - a[i]);

      count += p;
      a[i] += p;
    }
  }
  out << count << nl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, v, i, s = 0, nr;
  vector<ll> a;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> v;
    a.push_back(v);
  }
  for (i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      nr = a[i - 1] - a[i];
      s += nr;
      a[i] += nr;
    }
  }
  cout << s << endl;
  a.clear();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, a[10000], i, s = 0, nr;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      nr = a[i - 1] - a[i];
      s += nr;
      a[i] += nr;
    }
  }
  cout << s << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int64_t ans = 0;
  int64_t M = A.at(0);
  for (int i = 1; i < N; i++) {
    if (M > A.at(i)) {
      ans = ans + (M - A.at(i));
    } else {
      M = A.at(i);
    }
  }
  cout << ans << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A(N);
  vector<int> B(N);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i) = A.at(i);
  }
  int ans = 0;
  int M = A.at(0);
  for (int i = 1; i < N; i++) {
    if (M > A.at(i)) {
      ans = ans + (M - A.at(i));
    } else {
      M = A.at(i);
    }
  }
  cout << ans << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  long long sum = 0;
  for (int i = 0, maxnum = 0; i < n; i++) {
    maxnum = max(a.at(i), maxnum);
    sum += maxnum - a.at(i);
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  long long sum = 0;
  for (int i = 0, maxnum = 0; i < n;) {
    maxnum = max(a.at(i), maxnum);
    sum += maxnum - a.at(i);
  }
  cout << sum << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  long long ans = 0, tmp = 0;
  for (int i = 0; i < N; ++i) {
    tmp = max(tmp, A[i]);
    ans += tmp - A[i];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> v(N);
  for (int i = 0; i < N; ++i)
    cin >> v[i];

  long long ans = 0, tmp;
  for (int i = N - 1; i >= 1; --i) {
    tmp = 0;
    for (int j = i; j >= 0; --j) {
      tmp = max(tmp, v[j]);
    }
    ans += tmp - v[i];
  }
  cout << ans << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int max = a[0];
  long long aftersum = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > max)
      max = a[i];
    if (a[i] < max) {
      a[i] = max;
    }
    aftersum += a[i];
  }
  cout << aftersum - sum << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = a[0];
  int s = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] > max)
      max = a[i];
    else if (a[i] < max) {
      for (int k = 1; k < max; k++) {
        if (a[i] + k == max) {
          s += k;
          break;
        }
      }
      a[i] = max;
    }
  }
  cout << s << endl;
}
Problem p02578 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int INF = 1001001001;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  ll ans = 0;
  rep(i, N) cin >> A.at(i);
  int maxtmp = 0;
  rep(i, N) {
    maxtmp = max(maxtmp, A.at(i));
    ans += maxtmp - A.at(i);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int INF = 1001001001;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  ll ans = 0;
  rep(i, N) cin >> A.at(i);
  B.at(1) = A.at(0);
  ans += B.at(1) - A.at(1);
  for (int i = 1; i < N - 1; i++) {
    int maxtmp = 0;
    maxtmp = max(A.at(i + 1), B.at(i));
    B.at(i + 1) = maxtmp;
    ans += B.at(i + 1) - A.at(i + 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p02578 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a[n], ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int sa = a[i + 1] - a[i];
    if (sa < 0) {
      ans -= sa;
      a[i + 1] -= sa;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 10e5;
int n, a[MAXN], ans = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      while (a[j] < a[i]) {
        ans++;
        a[j]++;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define mem(a, b) memset(a, b, sizeof a)
#define mp make_pair

using namespace std;
const int N = 1e3 + 5, MI = 1e6;
int n, m, ok[N][N], cn, cm, dn, dm, dis[N][N], vis[N][N];
deque<pair<int, int>> q;
int chk(int x, int y) { return x && x <= n && y && y <= m && ok[x][y]; }
int chk(int x, int y, int v) {
  if (v < dis[x][y])
    return dis[x][y] = v, 1;
  return 0;
}
int main() {
#ifdef local
  freopen("in.txt", "r", stdin);
#endif
  mem(dis, 0x3f);
  cin >> n >> m >> cn >> cm >> dn >> dm;
  char s[N];
  rep(i, 1, n) {
    scanf("%s", s + 1);
    rep(j, 1, m) ok[i][j] = s[j] == '.';
  }
  dis[cn][cm] = 0, q.push_back(mp(cn, cm));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    if (vis[x][y])
      continue;
    vis[x][y] = 1;
    if (x == dn && y == dm)
      return !(cout << dis[x][y]);
    rep(i, -1, 1) if (chk(x + i, y) && chk(x + i, y, dis[x][y]))
        q.push_front(mp(x + i, y));
    rep(i, -1, 1) if (chk(x, y + i) && chk(x, y + i, dis[x][y]))
        q.push_front(mp(x, y + i));
    rep(i, -2, 2)
        rep(j, -2, 2) if (chk(x + i, y + j) && chk(x + i, y + j, dis[x][y] + 1))
            q.push_back(mp(x + i, y + j));
  }
  return puts("-1"), 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define mem(a, b) memset(a, b, sizeof a)
#define mp make_pair

using namespace std;
const int N = 1e3 + 5, MI = 1e6;
int n, m, ok[N][N], cn, cm, dn, dm, dis[N][N];
deque<pair<int, int>> q;
int chk(int x, int y) { return x && x <= n && y && y <= m && ok[x][y]; }
int chk(int x, int y, int v) {
  if (v < dis[x][y])
    return dis[x][y] = v, 1;
  return 0;
}
int main() {
#ifdef local
  freopen("in.txt", "r", stdin);
#endif
  mem(dis, 0x3f);
  cin >> n >> m >> cn >> cm >> dn >> dm;
  char s[N];
  rep(i, 1, n) {
    scanf("%s", s + 1);
    rep(j, 1, m) ok[i][j] = s[j] == '.';
  }
  dis[cn][cm] = 0, q.push_back(mp(cn, cm));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    if (x == dn && y == dm)
      return !(cout << dis[x][y]);
    q.pop_front();
    rep(i, -1, 1) if (chk(x + i, y) && chk(x + i, y, dis[x][y]))
        q.push_front(mp(x + i, y));
    rep(i, -1, 1) if (chk(x, y + i) && chk(x, y + i, dis[x][y]))
        q.push_front(mp(x, y + i));
    rep(i, -2, 2)
        rep(j, -2, 2) if (chk(x + i, y + j) && chk(x + i, y + j, dis[x][y] + 1))
            q.push_back(mp(x + i, y + j));
  }
  return puts("-1"), 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1e9;

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    if (dist[i][j] != d)
      continue;
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  const int INF = 1e9;

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];

    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];

      if (ni < 0 || h <= ni || nj < 0 || w <= nj) {
        continue;
      }
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d) {
        continue;
      }

      dist[nj][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || h <= ni || nj < 0 || w <= nj) {
          continue;
        }
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  const int INF = 1e9;

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];

    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];

      if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
        continue;
      }
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d) {
        continue;
      }

      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
          continue;
        }
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  const int INF = 1e9;

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];

    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];

      if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
        continue;
      }
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d) {
        continue;
      }

      dist[nj][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
          continue;
        }
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}