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

// In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair

const int N = 1e6 + 10;

queue<pair<int, int>> q;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  char ch[h + 10][w + 10];
  int dis[h + 10][w + 10];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> ch[i][j];
      dis[i][j] = N;
    }
  }
  dis[x1][y1] = 0;
  q.push(mp(x1, y1));
  while (!q.empty()) {
    int x = q.front().X, y = q.front().Y;
    int d = dis[x][y];
    q.pop();
    int z = y - 1;
    while (max(1, y - k) <= z) {
      if (ch[x][z] != '.')
        break;
      if (dis[x][z] < d + 1)
        break;
      if (d + 1 < dis[x][z])
        q.push(mp(x, z));
      dis[x][z] = d + 1;
      z--;
    }
    z = y + 1;
    while (z <= min(y + k, w)) {
      if (ch[x][z] != '.')
        break;
      if (dis[x][z] < d + 1)
        break;
      if (d + 1 < dis[x][z])
        q.push(mp(x, z));
      dis[x][z] = d + 1;
      z++;
    }
    z = x - 1;
    while (max(1, x - k) <= z) {
      if (ch[z][y] != '.')
        break;
      if (dis[z][y] < d + 1)
        break;
      if (d + 1 < dis[z][y])
        q.push(mp(z, y));
      dis[z][y] = d + 1;
      z--;
    }
    z = x + 1;
    while (z <= min(x + k, h)) {
      if (ch[z][y] != '.')
        break;
      if (dis[z][y] < d + 1)
        break;
      if (d + 1 < dis[z][y])
        q.push(mp(z, y));
      dis[z][y] = d + 1;
      z++;
    }
  }
  cout << (dis[x2][y2] == N ? -1 : dis[x2][y2]);
  return 0;
}


Error: TLE

Fixed submission:

// In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair

const int N = 1e6 + 10;

set<int> r[N], c[N];
queue<pair<int, int>> q;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  int dis[h + 10][w + 10];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char ch;
      cin >> ch;
      dis[i][j] = N;
      if (ch == '.') {
        r[i].insert(j);
        c[j].insert(i);
      }
    }
  }
  dis[x1][y1] = 0;
  q.push(mp(x1, y1));
  r[x1].erase(y1);
  c[y1].erase(x1);
  while (!q.empty()) {
    int x = q.front().X, y = q.front().Y;
    int d = dis[x][y];
    q.pop();
    r[x].erase(y);
    c[y].erase(x);
    int z = y - 1;
    while (max(1, y - k) <= z) {
      if (r[x].count(z) == 0)
        break;
      if (d + 1 < dis[x][z]) {
        dis[x][z] = d + 1;
        q.push(mp(x, z));
      }
      // r[x].erase(z);
      // c[z].erase(x);
      z--;
    }
    z = y + 1;
    while (z <= min(y + k, w)) {
      if (r[x].count(z) == 0)
        break;
      if (d + 1 < dis[x][z]) {
        dis[x][z] = d + 1;
        q.push(mp(x, z));
      }
      // r[x].erase(z);
      // c[z].erase(x);
      z++;
    }
    z = x - 1;
    while (max(1, x - k) <= z) {
      if (c[y].count(z) == 0)
        break;
      if (d + 1 < dis[z][y]) {
        dis[z][y] = d + 1;
        q.push(mp(z, y));
      }
      // r[z].erase(y);
      // c[y].erase(z);
      z--;
    }
    z = x + 1;
    while (z <= min(x + k, h)) {
      if (c[y].count(z) == 0)
        break;
      if (d + 1 < dis[z][y]) {
        dis[z][y] = d + 1;
        q.push(mp(z, y));
      }
      // r[z].erase(y);
      // c[y].erase(z);
      z++;
    }
  }
  cout << (dis[x2][y2] == N ? -1 : dis[x2][y2]);
  return 0;
}