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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void debugF(ostream &os) { os << endl; }
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) {
  os << " " << H;
  debugF(os, T...);
}
#define debug(...)                                                             \\
  cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); os << (i > 0 ? " " : "") << v[i++])
    ;
  return os;
}

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int inf = 1e9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, i, j;
  cin >> h >> w >> k;
  int x0, y0, x1, y1;
  cin >> x0 >> y0 >> x1 >> y1;
  x0--;
  y0--;
  x1--;
  y1--;
  char grid[h][w];
  for (i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (j = 0; j < w; j++) {
      grid[i][j] = s[j];
    }
  }
  vvi dist(h, vi(w, inf));
  vvi used(h, vi(w));
  dist[x0][y0] = 0;
  used[x0][y0] = 1;
  queue<pii> Q;
  Q.push({x0, y0});
  int x, y;
  while (!Q.empty()) {
    tie(x, y) = Q.front();
    Q.pop();
    used[x][y] = 2;
    if (x == x1 && y == y1)
      break;
    for (int dd = 0; dd < 4; dd++) {
      i = x + dx[dd];
      j = y + dy[dd];
      while ((i >= 0 && i < h) && (j >= 0 && j < w) && grid[i][j] != '@') {
        if (used[i][j] == 2)
          break;
        if (abs(i - x) > k || abs(j - y) > k)
          break;
        if (!used[i][j]) {
          dist[i][j] = dist[x][y] + 1;
          used[i][j] = 1;
          Q.push({i, j});
        }
        i += dx[dd];
        j += dy[dd];
      }
    }
  }

  if (dist[x1][y1] == inf) {
    cout << "-1\\n";
  } else {
    cout << dist[x1][y1] << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void debugF(ostream &os) { os << endl; }
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) {
  os << " " << H;
  debugF(os, T...);
}
#define debug(...)                                                             \\
  cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); os << (i > 0 ? " " : "") << v[i++])
    ;
  return os;
}

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int inf = 1e9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, i, j;
  cin >> h >> w >> k;
  int x0, y0, x1, y1;
  cin >> x0 >> y0 >> x1 >> y1;
  x0--;
  y0--;
  x1--;
  y1--;
  char grid[h][w];
  for (i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (j = 0; j < w; j++) {
      grid[i][j] = s[j];
    }
  }
  vvi dist(h, vi(w, inf));
  vvb used(h, vb(w));
  dist[x0][y0] = 0;
  used[x0][y0] = true;
  queue<pii> Q;
  Q.push({x0, y0});
  int x, y;
  while (!Q.empty()) {
    tie(x, y) = Q.front();
    Q.pop();
    if (x == x1 && y == y1)
      break;
    for (int dd = 0; dd < 4; dd++) {
      i = x + dx[dd];
      j = y + dy[dd];
      while ((i >= 0 && i < h) && (j >= 0 && j < w) && grid[i][j] != '@') {
        if (abs(i - x) > k || abs(j - y) > k)
          break;
        if (!used[i][j]) {
          dist[i][j] = dist[x][y] + 1;
          used[i][j] = true;
          Q.push({i, j});
        }
        i += dx[dd];
        j += dy[dd];
      }
    }
  }

  if (dist[x1][y1] == inf) {
    cout << "-1\\n";
  } else {
    cout << dist[x1][y1] << "\\n";
  }

  return 0;
}