Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define _for(i, a, b) for (int i = (a); i <= (b); i++)
#define _fIter(i, Istart, Iend) for (int i = Istart; i <= Iend; i++)
#define _fM(i, c) for (int i = c.begin(); i < c.end(); i++)
typedef int _Bx_INT32_0;
int n, m, k;
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, -1, 0, 1};

int main() {

  cin >> n >> m >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  char e[n + 4][m + 4];
  int dis[n + 4][m + 4];
  memset(dis, 0x55, sizeof dis);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> e[i][j];
  // cout<<3;
  // cout<<m-(n-k);
  typedef struct pos {
    int x;
    int y;
  } POS;
  queue<POS> q;
  POS f1234567 = {a, b};
  q.push(f1234567);
  dis[a][b] = 0;
  // cout<<8;
  while (!q.empty()) {
    // cout<<2;
    POS h = q.front();
    q.pop();
    int tx = h.x;
    int ty = h.y;
    for (int j = 1; j < 5; j++) {
      for (int i = 1; i <= min(k, max(n, m)); i++) {
        // printf("#%d %d %d
        // x=%d->%d,y=%d->%d\\n",cnt,i,j,tx,h.x+dx[j]*i,ty,h.y+dx[j]*i);
        tx = h.x + dx[j] * i;
        ty = h.y + dy[j] * i;
        if (tx <= 0 || ty <= 0 || tx > n || ty > m || e[tx][ty] == '@')
          break;
        if (dis[tx][ty] > dis[h.x][h.y] + 1) {
          q.push({tx, ty});
          dis[tx][ty] = dis[h.x][h.y] + 1;
        } else if (dis[tx][ty] < dis[h.x][h.y] + 1)
          break;
      }
    }
  }
  // for(int i=1;i<=n;i++)
  //{
  //    for(int j=1;j<=m;j++)
  //        cout<<dis[i][j]<<' ';
  //    cout<<endl;
  //}
  cout << (dis[c][d] == 0x55555555 ? -1 : dis[c][d]) << endl;
}
/*
3 5 2
3 2 3 4
.....
.@..@
..@..
*/

/*
9 9 8
1 1 9 9
.........
.@.......
..@......
...@.....
....@....
...@.@...
..@...@..
.@.....@.
.........
*/


Error: TLE

Fixed submission:

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

#define _for(i, a, b) for (int i = (a); i <= (b); i++)
#define _fIter(i, Istart, Iend) for (int i = Istart; i <= Iend; i++)
#define _fM(i, c) for (int i = c.begin(); i < c.end(); i++)
typedef int _Bx_INT32_0;
int n, m, k;
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, -1, 0, 1};

int main() {

  cin >> n >> m >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  char e[n + 4][m + 4];
  int dis[n + 4][m + 4];
  memset(dis, 0x55, sizeof dis);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> e[i][j];
  // cout<<3;
  // cout<<m-(n-k);
  typedef struct pos {
    int x;
    int y;
  } POS;
  queue<POS> q;
  POS f1234567 = {a, b};
  q.push(f1234567);
  dis[a][b] = 0;
  // cout<<8;
  while (!q.empty()) {
    // cout<<2;
    POS h = q.front();
    q.pop();
    int tx = h.x;
    int ty = h.y;
    for (int j = 1; j < 5; j++) {
      for (int i = 1; i <= min(k, max(n, m)); i++) {
        // printf("#%d %d %d
        // x=%d->%d,y=%d->%d\\n",cnt,i,j,tx,h.x+dx[j]*i,ty,h.y+dx[j]*i);
        tx = h.x + dx[j] * i;
        ty = h.y + dy[j] * i;
        if (tx <= 0 || ty <= 0 || tx > n || ty > m || e[tx][ty] == '@')
          break;
        if (dis[tx][ty] >= dis[h.x][h.y] + 1) {
          q.push({tx, ty});
          dis[tx][ty] = dis[h.x][h.y] + 1;
        } else
          break;
      }
    }
  }
  // for(int i=1;i<=n;i++)
  //{
  //    for(int j=1;j<=m;j++)
  //        cout<<dis[i][j]<<' ';
  //    cout<<endl;
  //}
  cout << (dis[c][d] == 0x55555555 ? -1 : dis[c][d]) << endl;
}
/*
3 5 2
3 2 3 4
.....
.@..@
..@..
*/

/*
9 9 8
1 1 9 9
.........
.@.......
..@......
...@.....
....@....
...@.@...
..@...@..
.@.....@.
.........
*/