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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int H, W, K;
int x1, y1, x2, y2;
string c[1000000];

vector<int> dist[1000000];

const int INF = 1e9;

void solve() {
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int h = p.first, w = p.second;
    {
      int l = max(0, w - K);
      int r = min(W - 1, w + K);
      for (int i = w - 1; i >= l; i--) {
        if (c[h][i] == '@')
          break;
        if (dist[h][i] <= dist[h][w])
          break;
        if (dist[h][i] == INF) {
          dist[h][i] = dist[h][w] + 1;
          que.push(P(h, i));
        }
      }
      for (int i = w + 1; i <= r; i++) {
        if (c[h][i] == '@')
          break;
        if (dist[h][i] <= dist[h][w])
          break;
        if (dist[h][i] == INF) {
          dist[h][i] = dist[h][w] + 1;
          que.push(P(h, i));
        }
      }
    }

    {
      int l = max(0, h - K);
      int r = min(H - 1, h + K);
      for (int i = h - 1; i >= l; i--) {
        if (c[i][w] == '@')
          break;
        if (dist[i][w] <= dist[h][w])
          break;
        if (dist[i][w] == INF) {
          dist[i][w] = dist[h][w] + 1;
          que.push(P(i, w));
        }
      }
      for (int i = h + 1; i <= r; i++) {
        if (c[i][w] == '@')
          break;
        if (dist[i][w] <= dist[h][w])
          break;
        if (dist[i][w] == INF) {
          dist[i][w] = dist[h][w] + 1;
          que.push(P(i, w));
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++)
    cin >> c[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i].push_back(1e9);
    }
  }
  solve();
  if (dist[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[x2][y2] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int H, W, K;
int x1, y1, x2, y2;
string c[1000000];

vector<int> dist[1000000];

const int INF = 1e9;

void solve() {
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int h = p.first, w = p.second;
    {
      int l = max(0, w - K);
      int r = min(W - 1, w + K);
      for (int i = w - 1; i >= l; i--) {
        if (c[h][i] == '@')
          break;
        if (dist[h][i] <= dist[h][w])
          break;
        dist[h][i] = dist[h][w] + 1;
        que.push(P(h, i));
      }
      for (int i = w + 1; i <= r; i++) {
        if (c[h][i] == '@')
          break;
        if (dist[h][i] <= dist[h][w])
          break;
        dist[h][i] = dist[h][w] + 1;
        que.push(P(h, i));
      }
    }

    {
      int l = max(0, h - K);
      int r = min(H - 1, h + K);
      for (int i = h - 1; i >= l; i--) {
        if (c[i][w] == '@')
          break;
        if (dist[i][w] <= dist[h][w])
          break;
        dist[i][w] = dist[h][w] + 1;
        que.push(P(i, w));
      }
      for (int i = h + 1; i <= r; i++) {
        if (c[i][w] == '@')
          break;
        if (dist[i][w] <= dist[h][w])
          break;
        dist[i][w] = dist[h][w] + 1;
        que.push(P(i, w));
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++)
    cin >> c[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i].push_back(1e9);
    }
  }
  solve();
  if (dist[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[x2][y2] << endl;
  }
}