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

#define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 1e6 + 10;
char buf[N];
int main() {
  int n, m, len;
  scanf("%d%d%d", &n, &m, &len);
  int sx, sy, tx, ty;
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", buf);
    s[i] = string(buf);
  }
  vector<vector<int>> d(n, vector<int>(m, -1));
  vector<vector<int>> L(n, vector<int>(m, -1));
  vector<vector<int>> R(n, vector<int>(m, -1));
  vector<vector<int>> U(n, vector<int>(m, -1));
  vector<vector<int>> D(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0, k; j < m; j = k) {
      for (; j < m && s[i][j] == '@'; ++j)
        ;
      if (j == m)
        break;
      for (k = j + 1; k < m && s[i][k] != '@'; ++k) {
        R[i][k - 1] = k;
        L[i][k] = k - 1;
      }
    }
  }
  for (int j = 0; j < m; ++j) {
    for (int i = 0, k; i < n; i = k) {
      for (; i < n && s[i][j] == '@'; ++i)
        ;
      if (i == n)
        break;
      for (k = i + 1; k < n && s[k][j] != '@'; ++k) {
        D[k - 1][j] = k;
        U[k][j] = k - 1;
      }
    }
  }
  d[sx][sy] = 0;
  queue<ii> Q;
  Q.push({sx, sy});
  if (R[sx][sy] != -1)
    L[sx][R[sx][sy]] = L[sx][sy];
  if (L[sx][sy] != -1)
    R[sx][L[sx][sy]] = R[sx][sy];
  if (D[sx][sy] != -1)
    U[D[sx][sy]][sy] = U[sx][sy];
  if (U[sx][sy] != -1)
    D[U[sx][sy]][sy] = D[sx][sy];
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    // trace(x, y, d[x][y]);
    Q.pop();
    int nx, ny, tt;
    for (nx = x, ny = L[x][y]; ny != -1 && y - ny <= len; ny = L[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
    }
    for (nx = x, ny = R[x][y]; ny != -1 && ny - y <= len; ny = R[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
    }
    for (ny = y, nx = U[x][y]; nx != -1 && x - nx <= len; nx = U[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
    for (ny = y, nx = D[x][y]; nx != -1 && nx - x <= len; nx = D[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
  }
  // for (int i = 0; i < n; ++i) {
  //   for (int j = 0; j < m; ++j) {
  //     cout << d[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  printf("%d\\n", d[tx][ty]);
  return 0;
}


Error: TLE

Fixed submission:

#define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 1e6 + 10;
char buf[N];
int main() {
  int n, m, len;
  scanf("%d%d%d", &n, &m, &len);
  int sx, sy, tx, ty;
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", buf);
    s[i] = string(buf);
  }
  vector<vector<int>> d(n, vector<int>(m, -1));
  vector<vector<int>> L(n, vector<int>(m, -1));
  vector<vector<int>> R(n, vector<int>(m, -1));
  vector<vector<int>> U(n, vector<int>(m, -1));
  vector<vector<int>> D(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0, k; j < m; j = k) {
      for (; j < m && s[i][j] == '@'; ++j)
        ;
      if (j == m)
        break;
      for (k = j + 1; k < m && s[i][k] != '@'; ++k) {
        R[i][k - 1] = k;
        L[i][k] = k - 1;
      }
    }
  }
  for (int j = 0; j < m; ++j) {
    for (int i = 0, k; i < n; i = k) {
      for (; i < n && s[i][j] == '@'; ++i)
        ;
      if (i == n)
        break;
      for (k = i + 1; k < n && s[k][j] != '@'; ++k) {
        D[k - 1][j] = k;
        U[k][j] = k - 1;
      }
    }
  }
  d[sx][sy] = 0;
  queue<ii> Q;
  Q.push({sx, sy});
  if (R[sx][sy] != -1)
    L[sx][R[sx][sy]] = L[sx][sy];
  if (L[sx][sy] != -1)
    R[sx][L[sx][sy]] = R[sx][sy];
  if (D[sx][sy] != -1)
    U[D[sx][sy]][sy] = U[sx][sy];
  if (U[sx][sy] != -1)
    D[U[sx][sy]][sy] = D[sx][sy];
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    // trace(x, y, d[x][y]);
    Q.pop();
    int nx, ny;
    for (nx = x, ny = L[x][y]; ny != -1 && y - ny <= len; ny = L[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
    for (nx = x, ny = R[x][y]; ny != -1 && ny - y <= len; ny = R[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
    for (ny = y, nx = U[x][y]; nx != -1 && x - nx <= len; nx = U[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
    for (ny = y, nx = D[x][y]; nx != -1 && nx - x <= len; nx = D[nx][ny]) {
      if (d[nx][ny] != -1)
        continue;
      d[nx][ny] = d[x][y] + 1;
      Q.push({nx, ny});
      if (R[nx][ny] != -1)
        L[nx][R[nx][ny]] = L[nx][ny];
      if (L[nx][ny] != -1)
        R[nx][L[nx][ny]] = R[nx][ny];
      if (D[nx][ny] != -1)
        U[D[nx][ny]][ny] = U[nx][ny];
      if (U[nx][ny] != -1)
        D[U[nx][ny]][ny] = D[nx][ny];
    }
  }
  // for (int i = 0; i < n; ++i) {
  //   for (int j = 0; j < m; ++j) {
  //     cout << d[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  printf("%d\\n", d[tx][ty]);
  return 0;
}