Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
const int N = 1e6 + 5;
const ll M = (ll)1e8 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

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

queue<pii> q;
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  /// cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    int h, w, k, x1, y1, x2, y2;
    cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
    --x1, --y1, --x2, --y2;
    string s[h + 5];
    bool visit[h + 5][w + 5];
    for (int i = 0; i < h; ++i) {
      cin >> s[i];
    }
    vector<vi> dis(h + 5, vi(w + 5, M));
    memset(visit, 0, sizeof(visit));
    dis[x1][y1] = 0;
    q.push({x1, y1});
    while (!q.empty()) {
      pii p = q.front();
      q.pop();
      if (!visit[p.first][p.second]) {
        visit[p.first][p.ss] = true;
        for (int i = 0; i < 4; ++i) {
          for (int j = 1; j <= k; ++j) {
            int x = p.first + (j * dx[i]), y = p.second + (j * dy[i]);
            if (x < 0 || x >= h || y < 0 || y >= w)
              break;
            if (s[x][y] == '@')
              break;
            if (dis[x][y] > dis[p.first][p.second] + 1) {
              dis[x][y] = dis[p.first][p.second] + 1;
            }
            q.push({x, y});
          }
        }
      }
    }
    if (dis[x2][y2] == M) {
      cout << -1 << endl;
    } else {
      cout << dis[x2][y2] << endl;
    }
  }
  return 0;
}


Fixed submission:
/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
const int N = 1e6 + 5;
const ll M = (ll)1e8 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

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

queue<pii> q;
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  /// cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    int h, w, k, x1, y1, x2, y2;
    cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
    --x1, --y1, --x2, --y2;
    string s[h + 5];
    bool visit[h + 5][w + 5];
    for (int i = 0; i < h; ++i) {
      cin >> s[i];
    }
    vector<vi> dis(h + 5, vi(w + 5, M));
    memset(visit, 0, sizeof(visit));
    dis[x1][y1] = 0;
    q.push({x1, y1});
    while (!q.empty()) {
      pii p = q.front();
      q.pop();
      if (!visit[p.first][p.second]) {
        visit[p.first][p.ss] = true;
        for (int i = 0; i < 4; ++i) {
          for (int j = 1; j <= k; ++j) {
            int x = p.first + (j * dx[i]), y = p.second + (j * dy[i]);
            if (x < 0 || x >= h || y < 0 || y >= w)
              break;
            if (s[x][y] == '@' || dis[x][y] <= dis[p.ff][p.ss])
              break;
            if (dis[x][y] > dis[p.first][p.second] + 1) {
              dis[x][y] = dis[p.first][p.second] + 1;
            }
            q.push({x, y});
          }
        }
      }
    }
    if (dis[x2][y2] == M) {
      cout << -1 << endl;
    } else {
      cout << dis[x2][y2] << endl;
    }
  }
  return 0;
}