Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

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

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef long double ld;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) ||    \
    (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)

const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repv(itr, v)                                                           \
  for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x)                                                              \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define fileio2(filename, name) fstream name(filename, ios::in | ios::out)
#define sqr(x) ((x) * (x))
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define SF scanf
#define PF printf

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const int maxn = 1000010;

int n, m, K, sx, sy, tx, ty;

int main() {
  cin >> n >> m >> K >> sx >> sy >> tx >> ty;

  vector<string> str(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> str[i];
    str[i] = " " + str[i];
  }

  vector<vector<int>> dist(n + 1, vector<int>(m + 1, INF));

  queue<pair<int, int>> q;
  q.push({sx, sy});
  dist[sx][sy] = 0;

  while (!q.empty()) {
    int x = q.front().fs, y = q.front().sc;
    q.pop();

    for (int k = 0; k < 4; k++)
      for (int l = 1; l <= K; l++) {
        int cx = x + dx[k] * l, cy = y + dy[k] * l;
        if (cx < 1 || cx > n || cy < 1 || cy > m || str[cx][cy] == '@')
          break;

        if (dist[x][y] + 1 < dist[cx][cy]) {
          dist[cx][cy] = dist[x][y] + 1;
          q.push({cx, cy});
        }
      }
  }

  cout << (dist[tx][ty] == INF ? -1 : dist[tx][ty]);

  return 0;
}

Fixed submission:
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

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

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef long double ld;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) ||    \
    (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)

const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repv(itr, v)                                                           \
  for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x)                                                              \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define fileio2(filename, name) fstream name(filename, ios::in | ios::out)
#define sqr(x) ((x) * (x))
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define SF scanf
#define PF printf

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const int maxn = 1000010;

int n, m, K, sx, sy, tx, ty;

int main() {
  cin >> n >> m >> K >> sx >> sy >> tx >> ty;

  vector<string> str(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> str[i];
    str[i] = " " + str[i];
  }

  vector<vector<int>> dist(n + 1, vector<int>(m + 1, INF));

  queue<pair<int, int>> q;
  q.push({sx, sy});
  dist[sx][sy] = 0;

  while (!q.empty()) {
    int x = q.front().fs, y = q.front().sc;
    q.pop();

    for (int k = 0; k < 4; k++)
      for (int l = 1; l <= K; l++) {
        int cx = x + dx[k] * l, cy = y + dy[k] * l;
        if (cx < 1 || cx > n || cy < 1 || cy > m || str[cx][cy] == '@' ||
            dist[x][y] + 1 > dist[cx][cy])
          break;

        if (dist[x][y] + 1 < dist[cx][cy]) {
          dist[cx][cy] = dist[x][y] + 1;
          q.push({cx, cy});
        }
      }
  }

  cout << (dist[tx][ty] == INF ? -1 : dist[tx][ty]);

  return 0;
}