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

#include <cstdio>
#include <queue>
#include <string>
#include <unordered_set>

using namespace std;

int h, w, k;
int x1, y1, x2, y2;
char tmp[1000006];
string c[1000006];
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
unordered_set<int> visited[1000006];

struct custom {
  int r, c, d;
};

queue<pair<custom, pair<int, bool>>> q;

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  for (int i = 0; i < h; i++) {
    scanf("%s", tmp);
    c[i] = tmp;
  }
  for (int j = 1; j <= h; j++) {
    visited[j].reserve(w);
  }
  for (int i = 0; i < 4; i++) {
    q.push({{x1, y1, i}, {0, false}});
    visited[x1].insert(x2);
  }
  int ans = -1;
  while (!q.empty()) {
    custom wtf = q.front().first;
    int dis = q.front().second.first;
    bool can_continue = q.front().second.second;
    q.pop();
    int x = wtf.r;
    int y = wtf.c;
    int d = wtf.d;
    if (x == x2 && y == y2) {
      ans = dis;
      break;
    }
    for (int i = 0; i < 4; i++) {
      if ((i == d && !can_continue) || d == i + 2 || i == d + 2) {
        continue;
      }
      int nx = x + dx[i];
      int ny = y + dy[i];
      int j = 1;
      while (1 <= nx && nx <= h && 1 <= ny && ny <= w &&
             c[nx - 1][ny - 1] == '.' && j <= k) {
        if (visited[nx].find(ny) == visited[nx].end()) {
          if (j == k) {
            q.push({{nx, ny, i}, {dis + 1, true}});
          } else {
            q.push({{nx, ny, i}, {dis + 1, false}});
          }
          visited[nx].insert(ny);
        }
        nx += dx[i];
        ny += dy[i];
        j++;
      }
    }
  }
  printf("%d\\n", ans);
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <string>
#include <unordered_set>

using namespace std;

int h, w, k;
int x1, y1, x2, y2;
char tmp[1000006];
string c[1000006];
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
unordered_set<int> visited[4][1000006];

struct custom {
  int r, c, d;
};

queue<pair<custom, pair<int, bool>>> q;

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  for (int i = 0; i < h; i++) {
    scanf("%s", tmp);
    c[i] = tmp;
  }
  for (int i = 0; i < 4; i++) {
    for (int j = 1; j <= h; j++) {
      visited[i][j].reserve(w);
    }
  }
  for (int i = 0; i < 4; i++) {
    q.push({{x1, y1, i}, {0, false}});
    visited[i][x1].insert(x2);
  }
  int ans = -1;
  while (!q.empty()) {
    custom wtf = q.front().first;
    int dis = q.front().second.first;
    bool can_continue = q.front().second.second;
    q.pop();
    int x = wtf.r;
    int y = wtf.c;
    int d = wtf.d;
    if (x == x2 && y == y2) {
      ans = dis;
      break;
    }
    for (int i = 0; i < 4; i++) {
      if ((i == d && !can_continue) || d == i + 2 || i == d + 2) {
        continue;
      }
      int nx = x + dx[i];
      int ny = y + dy[i];
      int j = 1;
      while (1 <= nx && nx <= h && 1 <= ny && ny <= w &&
             c[nx - 1][ny - 1] == '.' && j <= k) {
        if (visited[i][nx].find(ny) == visited[i][nx].end()) {
          if (j == k) {
            q.push({{nx, ny, i}, {dis + 1, true}});
          } else {
            q.push({{nx, ny, i}, {dis + 1, false}});
          }
          visited[i][nx].insert(ny);
        }
        nx += dx[i];
        ny += dy[i];
        j++;
      }
    }
  }
  printf("%d\\n", ans);
}