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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
char s[1000005];
using P = pair<int, int>;
int main() {
  int H, W, K, gx, gy;
  P start;
  scanf("%d %d %d\\n%d %d %d %d\\n", &H, &W, &K, &start.first, &start.second, &gx,
        &gy);
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; ++i) {
    fgets(s, 10000000, stdin);
    for (int j = 0; j < W; ++j) {
      dist[i][j] = (s[j] == '.') * 1e6;
    }
  }
  queue<P> que;
  dist[--start.first][--start.second] = 0;
  que.push(start);
  while (!que.empty()) {
    P x = que.front();
    que.pop();
    for (int i = 1; i <= K; ++i) {
      if (x.first - i < 0 ||
          dist[x.first - i][x.second] <= dist[x.first][x.second])
        break;
      if (dist[x.first - i][x.second] != dist[x.first][x.second] + 1) {
        que.push(P(x.first - i, x.second));
        dist[x.first - i][x.second] = dist[x.first][x.second] + 1;
      }
    }
    for (int i = 1; i <= K; ++i) {
      if (x.first + i >= H ||
          dist[x.first + i][x.second] <= dist[x.first][x.second])
        break;
      if (dist[x.first + i][x.second] != dist[x.first][x.second] + 1) {
        que.push(P(x.first + i, x.second));
        dist[x.first + i][x.second] = dist[x.first][x.second] + 1;
      }
    }
    for (int i = 1; i <= K; ++i) {
      if (x.second - i < 0 ||
          dist[x.first][x.second - i] <= dist[x.first][x.second])
        break;
      if (dist[x.first][x.second - i] != dist[x.first][x.second] + 1) {
        que.push(P(x.first, x.second - i));
        dist[x.first][x.second - i] = dist[x.first][x.second] + 1;
      }
    }
    for (int i = 1; i <= K; ++i) {
      if (x.second + i >= W ||
          dist[x.first][x.second + i] <= dist[x.first][x.second])
        break;
      if (dist[x.first][x.second + i] != dist[x.first][x.second] + 1) {
        que.push(P(x.first, x.second + i));
        dist[x.first][x.second + i] = dist[x.first][x.second] + 1;
      }
    }
  }
  if (dist[--gx][--gy] == 1e6)
    puts("-1");
  else
    printf("%d\\n", dist[gx][gy]);
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
char s[1000005];
using P = pair<int, int>;
int main() {
  int H, W, K, gx, gy;
  P start;
  scanf("%d %d %d\\n%d %d %d %d\\n", &H, &W, &K, &start.first, &start.second, &gx,
        &gy);
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; ++i) {
    fgets(s, 10000000, stdin);
    for (int j = 0; j < W; ++j) {
      dist[i][j] = (s[j] == '.') * 1e6;
    }
  }
  queue<P> que;
  dist[--start.first][--start.second] = 0;
  que.push(start);
  while (!que.empty()) {
    P x = que.front();
    que.pop();
    for (int i = 1; i <= K; ++i) {
      if (x.first - i < 0 ||
          dist[x.first - i][x.second] <= dist[x.first][x.second])
        break;
      dist[x.first - i][x.second] = dist[x.first][x.second] + 1;
      que.push(P(x.first - i, x.second));
    }
    for (int i = 1; i <= K; ++i) {
      if (x.first + i >= H ||
          dist[x.first + i][x.second] <= dist[x.first][x.second])
        break;
      dist[x.first + i][x.second] = dist[x.first][x.second] + 1;
      que.push(P(x.first + i, x.second));
    }
    for (int i = 1; i <= K; ++i) {
      if (x.second - i < 0 ||
          dist[x.first][x.second - i] <= dist[x.first][x.second])
        break;
      dist[x.first][x.second - i] = dist[x.first][x.second] + 1;
      que.push(P(x.first, x.second - i));
    }
    for (int i = 1; i <= K; ++i) {
      if (x.second + i >= W ||
          dist[x.first][x.second + i] <= dist[x.first][x.second])
        break;
      dist[x.first][x.second + i] = dist[x.first][x.second] + 1;
      que.push(P(x.first, x.second + i));
    }
  }
  if (dist[--gx][--gy] == 1e6)
    puts("-1");
  else
    printf("%d\\n", dist[gx][gy]);
}