Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fori(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define int long long
#define F first
#define S second
#define mp make_pair
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  cout.precision(15);

int mod = 1e9 + 7;

int pow(int a, int b) {
  int ans = 1;
  a %= mod;
  while (b) {
    if (b % 2) {
      ans *= a;
      ans %= mod;
    }
    b /= 2;
    a *= a;
    a %= mod;
  }
  return ans;
}

int inv(int a) { return pow(a, mod - 2); }

int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }

struct cmp {
  bool operator()(pair<int, int> a, pair<int, int> b) const {
    int x = a.F;
    int y = b.F;
    return x >= y;
  }
};

int h, k, w, x, y, u, v;
vector<string> mat;
vector<vector<int>> vis;

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

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

void bfs() {
  queue<pair<int, int>> q;
  vis[x][y] = 1;
  q.push({x, y});

  while (q.size()) {
    auto cur = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      for (int i = 1; i <= k; i++) {
        int nx = cur.F + dx[d] * i;
        int ny = cur.S + dy[d] * i;
        if (!valid(nx, ny) || mat[nx][ny] == '@')
          break;
        if (vis[nx][ny])
          continue;
        vis[nx][ny] = vis[cur.F][cur.S] + 1;
        q.push({nx, ny});
      }
    }
  }
}

int32_t main() {
  FAST;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    cin >> h >> w >> k >> x >> y >> u >> v;
    x--, y--, u--, v--;
    vis.resize(h);
    fori(i, h) vis[i].resize(w);
    fori(i, h) fori(j, w) vis[i][j] = 0;
    string s;
    fori(i, h) cin >> s, mat.pb(s);
    bfs();
    if (!vis[u][v])
      cout << "-1";
    else
      cout << vis[u][v] - 1;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fori(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define int long long
#define F first
#define S second
#define mp make_pair
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  cout.precision(15);

int mod = 1e9 + 7;

int pow(int a, int b) {
  int ans = 1;
  a %= mod;
  while (b) {
    if (b % 2) {
      ans *= a;
      ans %= mod;
    }
    b /= 2;
    a *= a;
    a %= mod;
  }
  return ans;
}

int inv(int a) { return pow(a, mod - 2); }

int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }

struct cmp {
  bool operator()(pair<int, int> a, pair<int, int> b) const {
    int x = a.F;
    int y = b.F;
    return x >= y;
  }
};

int h, k, w, x, y, u, v;
vector<string> mat;
vector<vector<int>> vis;

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

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

void bfs() {
  queue<pair<int, int>> q;
  vis[x][y] = 1;
  q.push({x, y});

  while (q.size()) {
    auto cur = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      for (int i = 1; i <= k; i++) {
        int nx = cur.F + dx[d] * i;
        int ny = cur.S + dy[d] * i;
        if (!valid(nx, ny) || mat[nx][ny] == '@')
          break;
        if (vis[nx][ny] && vis[nx][ny] < 1 + vis[cur.F][cur.S])
          break;
        if (vis[nx][ny])
          continue;
        vis[nx][ny] = vis[cur.F][cur.S] + 1;
        q.push({nx, ny});
      }
    }
  }
}

int32_t main() {
  FAST;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    cin >> h >> w >> k >> x >> y >> u >> v;
    x--, y--, u--, v--;
    vis.resize(h);
    fori(i, h) vis[i].resize(w);
    fori(i, h) fori(j, w) vis[i][j] = 0;
    string s;
    fori(i, h) cin >> s, mat.pb(s);
    bfs();
    if (!vis[u][v])
      cout << "-1";
    else
      cout << vis[u][v] - 1;
  }

  return 0;
}