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

#include <algorithm>
#include <cstdio>
#include <deque>
#include <tuple>
#include <utility>

using namespace std;

typedef tuple<int, int, int, int, int> iiiii;

#define INF 1000000005

int dirr[] = {-1, 0, 1, 0}, dirc[] = {0, 1, 0, -1};

int H, W, K;
int x1, y1, x2, y2;
deque<iiiii> bfs;
int ans = INF;

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  char grid[H + 5][W + 5];
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      scanf(" %c", &grid[i][j]);
  int dist[4][H + 5][W + 5];
  for (int i = 0; i < 4; i++)
    for (int j = 1; j <= H; j++)
      for (int k = 1; k <= W; k++)
        dist[i][j][k] = INF;
  for (int i = 0; i < 4; i++) {
    dist[i][x1][y1] = 1;
    bfs.emplace_front(i, K, 1, x1, y1);
  }
  while (!bfs.empty()) {
    int dir, remK, ud, ur, uc;
    tie(dir, remK, ud, ur, uc) = bfs.front();
    bfs.pop_front();
    // printf("dir: %d; remK: %d; dist: %d, u: (%d %d)\\n", dir, remK, ud, ur,
    // uc);
    for (int i = 0; i < 4; i++) {
      int vr = ur + dirr[i], vc = uc + dirc[i];
      if (vr <= 0 || vr > H || vc <= 0 || vc > W)
        continue;
      if (grid[vr][vc] == '@')
        continue;
      if (i == dir && remK > 0) {
        if (dist[i][vr][vc] > ud) {
          dist[i][vr][vc] = ud;
          bfs.emplace_front(i, remK - 1, dist[i][vr][vc], vr, vc);
        }
      } else {
        if (dist[i][vr][vc] > ud + 1) {
          dist[i][vr][vc] = ud + 1;
          bfs.emplace_back(i, K - 1, dist[i][vr][vc], vr, vc);
        }
      }
    }
  }
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[i][x2][y2]);
  if (ans == INF)
    printf("-1\\n");
  else
    printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> ii;

#define INF 1000000005

int dirr[] = {1, 0, -1, 0}, dirc[] = {0, -1, 0, 1};

int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
vector<vector<int>> dist;
vector<vector<bool>> visited;
queue<ii> bfs;

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 0; i <= H + 5; i++)
    c.push_back(vector<char>(W + 5));
  for (int i = 0; i <= H + 5; i++)
    dist.push_back(vector<int>(W + 5));
  for (int i = 0; i <= H + 5; i++)
    visited.push_back(vector<bool>(W + 5));
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      scanf(" %c", &c[i][j]);
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      dist[i][j] = INF;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      visited[i][j] = false;
  dist[x1][y1] = 0;
  bfs.emplace(x1, y1);
  while (!bfs.empty()) {
    int ur, uc;
    tie(ur, uc) = bfs.front();
    bfs.pop();
    if (visited[ur][uc])
      continue;
    visited[ur][uc] = true;
    // printf("%d %d:", ur, uc);
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int vr = ur + dirr[i] * j, vc = uc + dirc[i] * j;
        // printf(" (%d %d)", vr, vc);
        if (vr <= 0 || vr > H || vc <= 0 || vc > W)
          break;
        if (visited[vr][vc])
          break;
        if (c[vr][vc] == '@')
          break;
        if (dist[vr][vc] > dist[ur][uc] + 1) {
          dist[vr][vc] = dist[ur][uc] + 1;
          bfs.emplace(vr, vc);
        }
      }
    }
    // printf("\\n");
  }
  if (dist[x2][y2] != INF)
    printf("%d\\n", dist[x2][y2]);
  else
    printf("-1\\n");
  return 0;
}