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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  int stride = w + 1;
  int n = (h + 2) * stride;

  auto f = [&](int i, int j) { return stride * (i + 1) + (j + 1); };

  vector<char> c(n, -1);
  string s;
  for (int i = 0; i < h; i++) {
    cin >> s;
    for (int j = 0; j < w; j++) {
      c[f(i, j)] = s[j] != '.';
    }
  }

  vector<int> d(n, -1);
  vector<int> q(h * w);

  int j0 = 0, j1 = 0;
  d[q[j1++] = f(x1, y1)] = 0;

  while (j0 < j1) {
    int i = q[j0++];
    int d1 = d[i] + 1;

    for (int l : {-1, 1, -stride, stride}) {
      int i1 = i;
      for (int _ = 0; _ < k; _++) {
        i1 += l;
        if (c[i1])
          break;
        if (d[i1] >= 0) {
          if (d[i1] < d1)
            break;
          continue;
        }
        d[i1] = d1;
        q[j1++] = i1;
      }
    }
  }

  cout << d[f(x2, y2)] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

vector<array<int, 4>> l;
int h, w, k;

struct Graph {
  struct Vertex {
    int n, d;
  };
  struct Edge {
    int i, n;
  };
  Graph(int n) : v(n, {-1, -1}), q(n), n(n), m(0) {}
  void add_edge(int i, int j) {
    e.push_back({j, v[i].n});
    v[i].n = m;
    m++;
  }
  void bfs(int i0) {
    for (int i = 0; i < n; i++) {
      v[i].d = -1;
    }
    v[i0].d = 0;

    for (int z = 0; z < 4; z++) {
      if (int j = l[i0][z ^ 2]; j >= 0)
        l[j][z] = l[i0][z];
    }

    int j0 = 0, j1 = 0;
    q[j1++] = i0;

    while (j0 < j1) {
      int i2 = q[j0++];

      for (int u = 0; u < 4; u++) {
        int t = u % 2 == 0 ? 1 : w;

        for (int i = i2; i = l[i][u], i >= 0;) {
          if (abs(i - i2) / t > k)
            break;
          if (v[i].d >= 0)
            continue;

          v[i].d = v[i2].d + 1;
          q[j1++] = i;

          for (int z = 0; z < 4; z++) {
            if (int j = l[i][z ^ 2]; j >= 0)
              l[j][z] = l[i][z];
          }
        }
      }
    }
  }
  vector<Vertex> v;
  vector<Edge> e;
  vector<int> q;
  int n, m;
};

int main() {
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<char> c(h * w);
  l.assign(h * w, {-1, -1, -1, -1});

  auto f = [&](int i, int j) { return w * i + j; };

  string s;
  for (int i = 0; i < h; i++) {
    cin >> s;
    for (int j = 0; j < w; j++) {
      c[f(i, j)] = s[j] == '.';
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int k = f(i, j);
      if (j > 0) {
        if (c[k - 1] && c[k])
          l[k - 1][0] = k, l[k][2] = k - 1;
      }
      if (i > 0) {
        if (c[k - w] && c[k])
          l[k - w][1] = k, l[k][3] = k - w;
      }
    }
  }

  Graph g(f(h, w));
  g.bfs(f(x1, y1));

  cout << g.v[f(x2, y2)].d << endl;

  return 0;
}