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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000000

int main() {
  int H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  int cost[H][W];
  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) cost[i][j] = s[j] == '.' ? INF : -1;
  }

  cost[x1][y1] = 0;
  queue<pii> que;
  que.push({x1, y1});
  while (!que.empty()) {
    pii f = que.front();
    que.pop();
    int h = f.first, w = f.second, nc = cost[h][w];

    FOR(i, h + 1, min(h + K + 1, H)) {
      if (cost[i][w] == INF) {
        que.push({i, w});
        cost[i][w] = nc + 1;
      } else if (cost[i][w] < nc) {
        break;
      }
    }
    RFOR(i, h - 1, max(h - K - 1, -1)) {
      if (cost[i][w] == INF) {
        que.push({i, w});
        cost[i][w] = nc + 1;
      } else if (cost[i][w] < nc) {
        break;
      }
    }
    FOR(i, w + 1, min(w + K + 1, W)) {
      if (cost[h][i] == INF) {
        que.push({h, i});
        cost[h][i] = nc + 1;
      } else if (cost[h][i] < nc) {
        break;
      }
    }
    RFOR(i, w - 1, max(w - K - 1, -1)) {
      if (cost[h][i] == INF) {
        que.push({h, i});
        cost[h][i] = nc + 1;
      } else if (cost[h][i] < nc) {
        break;
      }
    }
  }

  if (cost[x2][y2] == INF)
    cout << -1 << endl;
  else
    cout << cost[x2][y2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000000

int main() {
  int H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  int cost[H][W];
  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) cost[i][j] = s[j] == '.' ? INF : -1;
  }

  cost[x1][y1] = 0;
  queue<pii> que;
  que.push({x1, y1});
  while (!que.empty()) {
    pii f = que.front();
    que.pop();
    int h = f.first, w = f.second, nc = cost[h][w];

    FOR(i, h + 1, min(h + K + 1, H)) {
      if (cost[i][w] == INF) {
        que.push({i, w});
        cost[i][w] = nc + 1;
      } else if (cost[i][w] == -1) {
        break;
      }
    }
    RFOR(i, h - 1, max(h - K - 1, -1)) {
      if (cost[i][w] == INF) {
        que.push({i, w});
        cost[i][w] = nc + 1;
      } else if (cost[i][w] == -1) {
        break;
      }
    }
    FOR(i, w + 1, min(w + K + 1, W)) {
      if (cost[h][i] == INF) {
        que.push({h, i});
        cost[h][i] = nc + 1;
      } else if (cost[h][i] == -1) {
        break;
      }
    }
    RFOR(i, w - 1, max(w - K - 1, -1)) {
      if (cost[h][i] == INF) {
        que.push({h, i});
        cost[h][i] = nc + 1;
      } else if (cost[h][i] == -1) {
        break;
      }
    }
  }

  if (cost[x2][y2] == INF)
    cout << -1 << endl;
  else
    cout << cost[x2][y2] << endl;
  return 0;
}