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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
vector<vector<int>> a;
vector<int> b;
string s;
queue<int> qx;
queue<int> qy;
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  int i, j;
  for (i = 0; i < h; ++i) {
    cin >> s;
    for (j = 0; j < w; ++j) {
      if (s[j] == '.')
        b.push_back(-1);
      else
        b.push_back(-2);
    }
    a.push_back(b);
    b.clear();
  }
  qx.push(x1), qy.push(y1);
  int cnt = 0, sd = 1, len = 1, x, y;
  a[x1][y1] = 0;
  while (!qx.empty()) {
    for (j = 0; j < len; j++) {
      x = qx.front();
      y = qy.front();
      for (i = 1; i <= k; ++i) {
        if (x - i < 0)
          break;
        if (a[x - i][y] != -1 && a[x - i][y] != sd)
          break;
        if (a[x - i][y] == sd)
          continue;
        a[x - i][y] = sd;
        qx.push(x - i), qy.push(y);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (x + i >= h)
          break;
        if (a[x + i][y] != -1 && a[x + i][y] != sd)
          break;
        if (a[x + i][y] == sd)
          continue;
        a[x + i][y] = sd;
        qx.push(x + i), qy.push(y);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (y - i < 0)
          break;
        if (a[x][y - i] != -1 && a[x][y - i] != sd)
          break;
        if (a[x][y - i] == sd)
          continue;
        a[x][y - i] = sd;
        qx.push(x), qy.push(y - i);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (y + i >= w)
          break;
        if (a[x][y + i] != -1 && a[x][y + i] != sd)
          break;
        if (a[x][y + i] == sd)
          continue;
        a[x][y + i] = sd;
        qx.push(x), qy.push(y + i);
        cnt++;
      }
      if (a[x2][y2] != -1) {
        cout << a[x2][y2] << endl;
        return 0;
      }
      qx.pop();
      qy.pop();
    }
    sd++;
    len = cnt;
    cnt = 0;
  }
  /*for (i = 0; i < h; ++i) {
          for (j = 0; j < w; ++j) {
                  cout << a[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << a[x2][y2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
vector<vector<int>> a;
vector<int> b;
string s;
queue<int> qx;
queue<int> qy;
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  int i, j;
  for (i = 0; i < h; ++i) {
    cin >> s;
    for (j = 0; j < w; ++j) {
      if (s[j] == '.')
        b.push_back(-1);
      else
        b.push_back(-2);
    }
    a.push_back(b);
    b.clear();
  }
  qx.push(x1), qy.push(y1);
  int cnt = 0, sd = 1, len = 1, x, y;
  a[x1][y1] = 0;
  while (!qx.empty()) {
    for (j = 0; j < len; j++) {
      x = qx.front();
      y = qy.front();
      for (i = 1; i <= k; ++i) {
        if (x - i < 0)
          break;
        if (a[x - i][y] != -1 && a[x - i][y] != sd)
          break;
        a[x - i][y] = sd;
        qx.push(x - i), qy.push(y);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (x + i >= h)
          break;
        if (a[x + i][y] != -1 && a[x + i][y] != sd)
          break;
        a[x + i][y] = sd;
        qx.push(x + i), qy.push(y);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (y - i < 0)
          break;
        if (a[x][y - i] != -1 && a[x][y - i] != sd)
          break;
        a[x][y - i] = sd;
        qx.push(x), qy.push(y - i);
        cnt++;
      }
      for (i = 1; i <= k; ++i) {
        if (y + i >= w)
          break;
        if (a[x][y + i] != -1 && a[x][y + i] != sd)
          break;
        a[x][y + i] = sd;
        qx.push(x), qy.push(y + i);
        cnt++;
      }
      if (a[x2][y2] != -1) {
        cout << a[x2][y2] << endl;
        return 0;
      }
      qx.pop();
      qy.pop();
    }
    sd++;
    len = cnt;
    cnt = 0;
  }
  /*for (i = 0; i < h; ++i) {
          for (j = 0; j < w; ++j) {
                  cout << a[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << a[x2][y2] << endl;
  return 0;
}