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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int H_MAX = 1000005;
const int W_MAX = 1000005;
const int INF = 10000005;
vector<string> C;
vector<vector<int>> T;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  C.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> C[i];
  }
  T.assign(h, vector<int>(w, INF));
  T[x1][y1] = 0;
  int ans = -1;
  queue<pair<int, int>> Q;
  Q.push(make_pair(x1, y1));
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    int now = T[x][y];
    if (x == x2 && y == y2) {
      ans = now;
      break;
    }
    Q.pop();
    for (int i = 1; i <= k; i++) {
      if (x + i >= h || C[x + i][y] == '@')
        break;
      if (T[x + i][y] <= now)
        break;
      else if (T[x + i][y] == now + 1)
        continue;
      T[x + i][y] = now + 1;
      if (x + i == x2 && y == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x + i, y));
    }
    for (int i = 1; i <= k; i++) {
      if (x - i < 0 || C[x - i][y] == '@')
        break;
      if (T[x - i][y] <= now)
        break;
      else if (T[x - i][y] == now + 1)
        continue;
      T[x - i][y] = now + 1;
      if (x - i == x2 && y == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x - i, y));
    }
    for (int i = 1; i <= k; i++) {
      if (y + i >= w || C[x][y + i] == '@')
        break;
      if (T[x][y + i] <= now)
        break;
      else if (T[x][y + i] == now + 1)
        continue;
      T[x][y + i] = now + 1;
      if (x == x2 && y + i == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x, y + i));
    }
    for (int i = 1; i <= k; i++) {
      if (y - i < 0 || C[x][y - i] == '@')
        break;
      if (T[x][y - i] <= now)
        break;
      else if (T[x][y - i] == now + 1)
        continue;
      T[x][y - i] = now + 1;
      if (x == x2 && y - i == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x, y - i));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int H_MAX = 1000005;
const int W_MAX = 1000005;
vector<string> C;
vector<vector<int>> T;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  C.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> C[i];
  }
  T.assign(h, vector<int>(w, -1));
  T[x1][y1] = 0;
  int ans = -1;
  queue<pair<int, int>> Q;
  Q.push(make_pair(x1, y1));
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    int now = T[x][y];
    if (x == x2 && y == y2) {
      ans = now;
      break;
    }
    Q.pop();
    for (int i = 1; i <= k; i++) {
      if (x + i >= h || C[x + i][y] == '@')
        break;
      if (T[x + i][y] != -1)
        continue;
      T[x + i][y] = now + 1;
      if (x + i == x2 && y == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x + i, y));
    }
    for (int i = 1; i <= k; i++) {
      if (x - i < 0 || C[x - i][y] == '@')
        break;
      if (T[x - i][y] != -1)
        continue;
      T[x - i][y] = now + 1;
      if (x - i == x2 && y == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x - i, y));
    }
    for (int i = 1; i <= k; i++) {
      if (y + i >= w || C[x][y + i] == '@')
        break;
      if (T[x][y + i] != -1)
        continue;
      T[x][y + i] = now + 1;
      if (x == x2 && y + i == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x, y + i));
    }
    for (int i = 1; i <= k; i++) {
      if (y - i < 0 || C[x][y - i] == '@')
        break;
      if (T[x][y - i] != -1)
        continue;
      T[x][y - i] = now + 1;
      if (x == x2 && y - i == y2) {
        ans = now + 1;
        break;
      }
      Q.push(make_pair(x, y - i));
    }
  }

  cout << ans << endl;
  return 0;
}