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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

using P = pair<int, int>;
void update(vector<vector<bool>> &is_visited, queue<P> &q,
            vector<vector<int>> &dist, int cx, int cy, int d) {
  if (!is_visited[cx][cy]) {
    is_visited[cx][cy] = true;
    dist[cx][cy] = d + 1;
    q.push(P(cx, cy));
  }
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;

  vector<string> grid(H);
  rep(i, H) { cin >> grid[i]; }

  const int INF = H * W;
  vector<vector<bool>> is_visited(H, vector<bool>(W, false));
  vector<vector<bool>> is_visited_x(H, vector<bool>(W, false));
  vector<vector<bool>> is_visited_y(H, vector<bool>(W, false));

  vector<vector<int>> dist(H, vector<int>(W, INF));
  is_visited[sx][sy] = is_visited_x[sx][sy] = is_visited_y[sx][sy] = true;
  dist[sx][sy] = 0;
  queue<P> q;
  q.push(P(sx, sy));

  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    int d = dist[x][y];

    for (int cx = x - 1; cx >= max(0, x - K) && grid[cx][y] != '@' &&
                         dist[x][y] < dist[cx][y]; //! is_visited_x[cx][y];
         cx--) {
      is_visited_x[cx][y] = true;
      update(is_visited, q, dist, cx, y, d);
    }
    for (int cx = x + 1; cx <= min(H - 1, x + K) && grid[cx][y] != '@' &&
                         dist[x][y] < dist[cx][y]; //! is_visited_x[cx][y];
         cx++) {
      is_visited_x[cx][y] = true;
      update(is_visited, q, dist, cx, y, d);
    }
    for (int cy = y - 1; cy >= max(0, y - K) && grid[x][cy] != '@' &&
                         dist[x][y] < dist[x][cy]; //! is_visited_y[x][cy];
         cy--) {
      is_visited_y[x][cy] = true;
      update(is_visited, q, dist, x, cy, d);
    }
    for (int cy = y + 1; cy <= min(W - 1, y + K) && grid[x][cy] != '@' &&
                         dist[x][y] < dist[x][cy]; //! is_visited_y[x][cy];
         cy++) {
      is_visited_y[x][cy] = true;
      update(is_visited, q, dist, x, cy, d);
    }
  }

  int result = dist[tx][ty];
  cout << (result == INF ? -1 : result) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

using P = pair<int, int>;
void update(vector<vector<bool>> &is_visited, queue<P> &q,
            vector<vector<int>> &dist, int cx, int cy, int d) {
  if (!is_visited[cx][cy]) {
    is_visited[cx][cy] = true;
    dist[cx][cy] = d + 1;
    q.push(P(cx, cy));
  }
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;

  vector<string> grid(H);
  rep(i, H) { cin >> grid[i]; }

  const int INF = H * W;
  vector<vector<bool>> is_visited(H, vector<bool>(W, false));
  vector<vector<int>> dist(H, vector<int>(W, INF));
  is_visited[sx][sy] = true;
  dist[sx][sy] = 0;
  queue<P> q;
  q.push(P(sx, sy));

  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    int d = dist[x][y];

    for (int cx = x - 1; cx >= max(0, x - K) && grid[cx][y] != '@'; cx--) {
      update(is_visited, q, dist, cx, y, d);
    }
    for (int cx = x + 1; cx <= min(H - 1, x + K) && grid[cx][y] != '@'; cx++) {
      update(is_visited, q, dist, cx, y, d);
    }
    for (int cy = y - 1; cy >= max(0, y - K) && grid[x][cy] != '@'; cy--) {
      update(is_visited, q, dist, x, cy, d);
    }
    for (int cy = y + 1; cy <= min(W - 1, y + K) && grid[x][cy] != '@'; cy++) {
      update(is_visited, q, dist, x, cy, d);
    }
  }

  int result = dist[tx][ty];
  cout << (result == INF ? -1 : result) << endl;
  return 0;
}