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

#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
constexpr int INF = 1000000000;
int h, w, k, stx, sty, gox, goy;
vector<vector<char>> c;
vector<vector<int>> U, D, L, R, d;
void init_table() {
  rep(j, w) {
    if (c[0][j] == '@')
      U[0][j] = -1;
    rep(i, h - 1) {
      if (c[i + 1][j] == '@')
        U[i + 1][j] = -1;
      else
        U[i + 1][j] = U[i][j] + 1;
    }
  }
  rep(j, w) {
    if (c[h - 1][j] == '@')
      D[h - 1][j] = -1;
    for (int i = h - 2; i >= 0; i--) {
      if (c[i][j] == '@')
        D[i][j] = -1;
      else
        D[i][j] = D[i + 1][j] + 1;
    }
  }
  rep(i, h) {
    if (c[i][0] == '@')
      L[i][0] = -1;
    rep(j, w - 1) {
      if (c[i][j + 1] == '@')
        L[i][j + 1] = -1;
      else
        L[i][j + 1] = L[i][j] + 1;
    }
  }
  rep(i, h) {
    if (c[i][w - 1] == '@')
      R[i][w - 1] = -1;
    for (int j = w - 2; j >= 0; j--) {
      if (c[i][j] == '@')
        R[i][j] = -1;
      else
        R[i][j] = R[i][j + 1] + 1;
    }
  }
}
struct si {
  int x, y, cost;
};
void BFS() {
  queue<si> q;
  q.push({stx, sty, 0});
  d[stx][sty] = 0;
  while (!q.empty()) {
    si p = q.front();
    q.pop();
    int x = p.x, y = p.y;
    if (d[x][y] != p.cost)
      continue;
    for (int i = x - 1; i >= max(0, x - min(k, U[x][y])); i--) {
      if (d[i][y] > d[x][y] + 1) {
        d[i][y] = d[x][y] + 1;
        q.push({i, y, d[i][y]});
      } else if (d[i][y] <= d[x][y])
        break;
    }
    for (int i = x + 1; i <= min(h - 1, x + min(k, D[x][y])); i++) {
      if (d[i][y] > d[x][y] + 1) {
        d[i][y] = d[x][y] + 1;
        q.push({i, y, d[i][y]});
      } else if (d[i][y] <= d[x][y])
        break;
    }
    for (int j = y - 1; j >= max(0, y - min(k, L[x][y])); j--) {
      if (d[x][j] > d[x][y] + 1) {
        d[x][j] = d[x][y] + 1;
        q.push({x, j, d[x][j]});
      } else if (d[x][j] <= d[x][y])
        break;
    }
    for (int j = y + 1; j <= min(w - 1, y + min(k, R[x][y])); j++) {
      if (d[x][j] > d[x][y] + 1) {
        d[x][j] = d[x][y] + 1;
        q.push({x, j, d[x][j]});
      } else if (d[x][j] <= d[x][y])
        break;
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  c.resize(h, vector<char>(w, '.'));
  U.resize(h, vector<int>(w, 0));
  D.resize(h, vector<int>(w, 0));
  L.resize(h, vector<int>(w, 0));
  R.resize(h, vector<int>(w, 0));
  d.resize(h, vector<int>(w, INF));
  cin >> stx >> sty >> gox >> goy;
  stx--, sty--, gox--, goy--;
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  init_table();
  BFS();
  if (d[gox][goy] == INF)
    puts("-1");
  else
    cout << d[gox][goy] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
constexpr int INF = 1000000000;
int h, w, k, stx, sty, gox, goy;
vector<vector<char>> c;
vector<vector<int>> U, D, L, R, d;
void init_table() {
  rep(j, w) {
    if (c[0][j] == '@')
      U[0][j] = -1;
    rep(i, h - 1) {
      if (c[i + 1][j] == '@')
        U[i + 1][j] = -1;
      else
        U[i + 1][j] = U[i][j] + 1;
    }
  }
  rep(j, w) {
    if (c[h - 1][j] == '@')
      D[h - 1][j] = -1;
    for (int i = h - 2; i >= 0; i--) {
      if (c[i][j] == '@')
        D[i][j] = -1;
      else
        D[i][j] = D[i + 1][j] + 1;
    }
  }
  rep(i, h) {
    if (c[i][0] == '@')
      L[i][0] = -1;
    rep(j, w - 1) {
      if (c[i][j + 1] == '@')
        L[i][j + 1] = -1;
      else
        L[i][j + 1] = L[i][j] + 1;
    }
  }
  rep(i, h) {
    if (c[i][w - 1] == '@')
      R[i][w - 1] = -1;
    for (int j = w - 2; j >= 0; j--) {
      if (c[i][j] == '@')
        R[i][j] = -1;
      else
        R[i][j] = R[i][j + 1] + 1;
    }
  }
}
struct si {
  int x, y, cost;
};
void BFS() {
  queue<si> q;
  q.push({stx, sty, 0});
  d[stx][sty] = 0;
  while (!q.empty()) {
    si p = q.front();
    q.pop();
    int x = p.x, y = p.y;
    if (d[x][y] != p.cost)
      continue;
    for (int i = max(0, x - min(k, U[x][y])); i <= x - 1; i++) {
      if (d[i][y] > d[x][y] + 1) {
        d[i][y] = d[x][y] + 1;
        q.push({i, y, d[i][y]});
      }
    }
    for (int i = min(h - 1, x + min(k, D[x][y])); i >= x + 1; i--) {
      if (d[i][y] > d[x][y] + 1) {
        d[i][y] = d[x][y] + 1;
        q.push({i, y, d[i][y]});
      }
    }
    for (int j = max(0, y - min(k, L[x][y])); j <= y - 1; j++) {
      if (d[x][j] > d[x][y] + 1) {
        d[x][j] = d[x][y] + 1;
        q.push({x, j, d[x][j]});
      }
    }
    for (int j = min(w - 1, y + min(k, R[x][y])); j >= y + 1; j--) {
      if (d[x][j] > d[x][y] + 1) {
        d[x][j] = d[x][y] + 1;
        q.push({x, j, d[x][j]});
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  c.resize(h, vector<char>(w, '.'));
  U.resize(h, vector<int>(w, 0));
  D.resize(h, vector<int>(w, 0));
  L.resize(h, vector<int>(w, 0));
  R.resize(h, vector<int>(w, 0));
  d.resize(h, vector<int>(w, INF));
  cin >> stx >> sty >> gox >> goy;
  stx--, sty--, gox--, goy--;
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  init_table();
  BFS();
  if (d[gox][goy] == INF)
    puts("-1");
  else
    cout << d[gox][goy] << endl;
  return 0;
}