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

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<vector<Node>> V;
struct Node {
  int x;
  int y;
  int upx;
  int updist = 1;
  int downx;
  int downdist = 1;
  int lefty;
  int leftdist = 1;
  int righty;
  int rightdist = 1;
  int dist = INF;
  bool valid = true;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  REP(i, H) cin >> c[i];
  V = vector<vector<Node>>(H + 2, vector<Node>(W + 2));
  REP(i, H + 2) {
    REP(j, W + 2) {
      V[i][j].x = i;
      V[i][j].y = j;
      if (i == 0 || i == H + 1 || j == 0 || j == W + 1 ||
          c[i - 1][j - 1] == '@')
        V[i][j].valid = false;
      V[i][j].upx = i - 1;
      V[i][j].downx = i + 1;
      V[i][j].lefty = j - 1;
      V[i][j].righty = j + 1;
    }
  }
  deque<Node> que;
  V[x1][y1].dist = 0;
  que.push_back(V[x1][y1]);

  while (!que.empty()) {
    Node n = que.front();
    que.pop_front();
    int nowd = 0;
    int x = n.x;
    int y = n.y;
    if (x2 == x && y2 == y)
      break;
    // cout << x << " " << y << endl;
    vector<Node> tmp;
    while (true) {
      nowd += V[x][y].updist;
      x = V[x][y].upx;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist <= n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      tmp.push_back(V[x][y]);
    }
    int distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].upx = x;
      distsum += tmp[i].updist;
      tmp[i].updist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].downdist;
      x = V[x][y].downx;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist <= n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }
    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].downx = x;
      distsum += tmp[i].downdist;
      tmp[i].downdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].leftdist;
      y = V[x][y].lefty;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist <= n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }

    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].lefty = y;
      distsum += tmp[i].leftdist;
      tmp[i].leftdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].rightdist;
      y = V[x][y].righty;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist <= n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }

    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].righty = y;
      distsum += tmp[i].rightdist;
      tmp[i].rightdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();
  }

  if (V[x2][y2].dist == INF) {
    cout << -1 << endl;
  } else {
    cout << V[x2][y2].dist << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<vector<Node>> V;
struct Node {
  int x;
  int y;
  int upx;
  int updist = 1;
  int downx;
  int downdist = 1;
  int lefty;
  int leftdist = 1;
  int righty;
  int rightdist = 1;
  int dist = INF;
  bool valid = true;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  REP(i, H) cin >> c[i];
  V = vector<vector<Node>>(H + 2, vector<Node>(W + 2));
  REP(i, H + 2) {
    REP(j, W + 2) {
      V[i][j].x = i;
      V[i][j].y = j;
      if (i == 0 || i == H + 1 || j == 0 || j == W + 1 ||
          c[i - 1][j - 1] == '@')
        V[i][j].valid = false;
      V[i][j].upx = i - 1;
      V[i][j].downx = i + 1;
      V[i][j].lefty = j - 1;
      V[i][j].righty = j + 1;
    }
  }
  deque<Node> que;
  V[x1][y1].dist = 0;
  que.push_back(V[x1][y1]);

  while (!que.empty()) {
    Node n = que.front();
    que.pop_front();
    int nowd = 0;
    int x = n.x;
    int y = n.y;
    if (x2 == x && y2 == y)
      break;
    // cout << x << " " << y << endl;
    vector<Node> tmp;
    while (true) {
      nowd += V[x][y].updist;
      x = V[x][y].upx;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist < n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      tmp.push_back(V[x][y]);
    }
    int distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].upx = x;
      distsum += tmp[i].updist;
      tmp[i].updist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].downdist;
      x = V[x][y].downx;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist < n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }
    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].downx = x;
      distsum += tmp[i].downdist;
      tmp[i].downdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].leftdist;
      y = V[x][y].lefty;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist < n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }

    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].lefty = y;
      distsum += tmp[i].leftdist;
      tmp[i].leftdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();

    nowd = 0;
    x = n.x;
    y = n.y;
    while (true) {
      nowd += V[x][y].rightdist;
      y = V[x][y].righty;
      if (nowd > K)
        break;
      if (!V[x][y].valid)
        break;
      if (V[x][y].dist < n.dist)
        break;
      if (V[x][y].dist != INF)
        continue;
      V[x][y].dist = n.dist + 1;
      que.push_back(V[x][y]);
    }

    distsum = 0;
    IREP(i, tmp.size()) {
      tmp[i].righty = y;
      distsum += tmp[i].rightdist;
      tmp[i].rightdist = distsum;
      que.push_back(tmp[i]);
    }
    tmp.clear();
  }

  if (V[x2][y2].dist == INF) {
    cout << -1 << endl;
  } else {
    cout << V[x2][y2].dist << endl;
  }
}