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

#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 1e6 + 5;
const ll mod = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, m, k, x1, x2, Y1, y2;
string s[N];

void bfs() {
  map<pll, int> ct;
  queue<pll> q;
  q.push({x1, Y1});
  ct[{x1, Y1}] = 1;
  while (q.size()) {
    pll x = q.front();
    q.pop();
    int c = k;
    for (int i = x.f - 1; i >= 0; i--) {
      if (s[i][x.s] == '@')
        break;
      if (ct[{i, x.s}] == 0) {
        q.push({i, x.s});
        ct[{i, x.s}] = ct[x] + 1;
      }
      if (ct[{i, x.s}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.f + 1; i < n; i++) {
      if (s[i][x.s] == '@')
        break;
      if (ct[{i, x.s}] == 0) {
        q.push({i, x.s});
        ct[{i, x.s}] = ct[x] + 1;
      }
      if (ct[{i, x.s}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.s - 1; i >= 0; i--) {
      if (s[x.f][i] == '@')
        break;
      if (ct[{x.f, i}] == 0) {
        q.push({x.f, i});
        ct[{x.f, i}] = ct[x] + 1;
      }
      if (ct[{x.f, i}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.s + 1; i < m; i++) {
      if (s[x.f][i] == '@')
        break;
      if (ct[{x.f, i}] == 0) {
        q.push({x.f, i});
        ct[{x.f, i}] = ct[x] + 1;
      }
      if (ct[{x.f, i}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
  }
  cout << ct[{x2, y2}] - 1 << endl;
}

void solve() {
  cin >> n >> m >> k;
  cin >> x1 >> Y1 >> x2 >> y2;
  x1--, Y1--, x2--, y2--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  bfs();
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 1e6 + 5;
const ll mod = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, m, k, x1, x2, Y1, y2;
string s[N];

ll bfs() {
  map<pll, int> ct;
  queue<pll> q;
  q.push({x1, Y1});
  ct[{x1, Y1}] = 1;
  while (q.size()) {
    pll x = q.front();
    q.pop();
    int c = k;
    for (int i = x.f - 1; i >= 0; i--) {
      if (s[i][x.s] == '@')
        break;
      if (ct[{i, x.s}] == 0) {
        q.push({i, x.s});
        ct[{i, x.s}] = ct[x] + 1;
      }
      if (ct[{i, x.s}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.f + 1; i < n; i++) {
      if (s[i][x.s] == '@')
        break;
      if (ct[{i, x.s}] == 0) {
        q.push({i, x.s});
        ct[{i, x.s}] = ct[x] + 1;
      }
      if (ct[{i, x.s}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.s - 1; i >= 0; i--) {
      if (s[x.f][i] == '@')
        break;
      if (ct[{x.f, i}] == 0) {
        q.push({x.f, i});
        ct[{x.f, i}] = ct[x] + 1;
      }
      if (ct[{x.f, i}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
    c = k;
    for (int i = x.s + 1; i < m; i++) {
      if (s[x.f][i] == '@')
        break;
      if (ct[{x.f, i}] == 0) {
        q.push({x.f, i});
        ct[{x.f, i}] = ct[x] + 1;
      }
      if (ct[{x.f, i}] < ct[x] + 1)
        break;
      c--;
      if (c == 0)
        break;
    }
  }
  cout << ct[{x2, y2}] - 1 << endl;
}

void solve() {
  cin >> n >> m >> k;
  cin >> x1 >> Y1 >> x2 >> y2;
  x1--, Y1--, x2--, y2--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  bfs();
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}