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

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int R, C, k;
int row[] = {1, 0, -1, 0, 1, -1, 1, -1};
int colm[] = {0, 1, 0, -1, 1, -1, -1, 1};
vector<string> maze(MAX);
void bfs(int input_x, int input_y, int tx, int ty) {
  queue<pair<int, int>> q;
  vector<vector<int>> dis(R + 1, vector<int>(C + 1, INT_MAX));
  dis[input_x][input_y] = 0;
  q.push(make_pair(input_x, input_y));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int u = x + row[i] * j;
        int v = y + colm[i] * j;
        if (0 <= u && u < R && 0 <= v && v < C) {
          if (maze[u][v] == '@' || dis[u][v] <= dis[x][y])
            break;
          if (dis[u][v] > dis[x][y] + 1) {
            dis[u][v] = dis[x][y] + 1;
            q.push(make_pair(u, v));
          }
        } else
          break;
      }
    }
  }
  if (dis[tx][ty] == INT_MAX)
    cout << "-1" << endl;
  else
    cout << dis[tx][ty] << endl;
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int x1,
      x2, y1, y2;
  cin >> R >> C >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < R; i++)
    cin >> maze[i];
  x1--;
  y1--;
  x2--;
  y2--;
  bfs(x1, y1, x2, y2);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int R, C, k;
int row[] = {1, 0, -1, 0, 1, -1, 1, -1};
int colm[] = {0, 1, 0, -1, 1, -1, -1, 1};
vector<string> maze(MAX);
void bfs(int input_x, int input_y, int tx, int ty) {
  queue<pair<int, int>> q;
  vector<vector<int>> dis(R + 1, vector<int>(C + 1, INT_MAX));
  dis[input_x][input_y] = 0;
  q.push(make_pair(input_x, input_y));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int u = x + row[i] * j;
        int v = y + colm[i] * j;
        if (0 <= u && u < R && 0 <= v && v < C) {
          if (maze[u][v] == '.') {
            if (dis[u][v] > dis[x][y] + 1) {
              dis[u][v] = dis[x][y] + 1;
              q.push(make_pair(u, v));
            }
          } else
            break;
        } else
          break;
      }
    }
  }
  if (dis[tx][ty] == INT_MAX)
    cout << "-1" << endl;
  else
    cout << dis[tx][ty] << endl;
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int x1,
      x2, y1, y2;
  cin >> R >> C >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < R; i++)
    cin >> maze[i];
  x1--;
  y1--;
  x2--;
  y2--;
  bfs(x1, y1, x2, y2);

  return 0;
}