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 randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

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

const ll MOD = 1e+9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll N = 1e+6 + 10;

vll adj[N];
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, m, k;
ll x1, g1, x2, g2;
string s[N];
ll ans = INF;
ll vis[N], visit_act[N];
struct Point {
  ll x, y, res;
};
//<<-----Implement Functions Here---->>//
void bfs() {
  vis[(x1 - 1) * m + g1] = 1;
  queue<Point> q;
  q.push({x1, g1, 0});

  while (q.size() > 0) {
    Point p = q.front();
    q.pop();
    ll x = p.x;
    ll y = p.y;
    ll res = p.res;

    visit_act[(x - 1) * m + y] = 1;
    if (x == x2 && y == g2) {
      ans = min(ans, res);
      break;
    }

    for (ll i = 0; i < 4; i++) {
      for (ll j = 1; j <= k; j++) {
        ll dx = x + j * dx4[i];
        ll dy = y + j * dy4[i];
        if (s[dx][dy] == '@' || visit_act[(dx - 1) * m + dy] == 1)
          break;
        if (vis[(dx - 1) * m + dy] == 0) {
          q.push({dx, dy, res + 1});
          vis[(dx - 1) * m + dy] = 1;
        }
      }
    }
  }
}
//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> m >> k;
  cin >> x1 >> g1 >> x2 >> g2;

  for (ll i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = "@" + s[i] + "@";
  }
  s[0] = s[n + 1] = string(m + 2, '@');
  bfs();
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }
}

Error: WA

Fixed submission:

#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 randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

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

const ll MOD = 1e+9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll N = 1e+6 + 10;

vll adj[N];
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, m, k;
ll x1, g1, x2, g2;
string s[N];
ll ans = INF;
ll vis[N], visit_act[N];
struct Point {
  ll x, y, res;
};
//<<-----Implement Functions Here---->>//
void bfs() {
  vis[(x1 - 1) * m + g1] = 1;
  queue<Point> q;
  q.push({x1, g1, 0});

  while (q.size() > 0) {
    Point p = q.front();
    q.pop();
    ll x = p.x;
    ll y = p.y;
    ll res = p.res;

    visit_act[(x - 1) * m + y] = 1;
    if (x == x2 && y == g2) {
      ans = min(ans, res);
      break;
    }

    for (ll i = 0; i < 4; i++) {
      for (ll j = 1; j <= k; j++) {
        ll dx = x + j * dx4[i];
        ll dy = y + j * dy4[i];
        if (visit_act[(dx - 1) * m + dy] == 1)
          break;
        if (s[dx][dy] == '@' || vis[(dx - 1) * m + dy] == 0) {
          q.push({dx, dy, res + 1});
          vis[(dx - 1) * m + dy] = 1;
        }
      }
    }
  }
}
//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> m >> k;
  cin >> x1 >> g1 >> x2 >> g2;

  for (ll i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = "@" + s[i] + "@";
  }
  s[0] = s[n + 1] = string(m + 2, '@');
  bfs();
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }
}