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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ALL(X) X.begin(), X.end()
using lint = long long;
template <typename T> void print_container(std::vector<T> vec) {
  for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
}

class Step {
public:
  lint y;
  lint x;
  lint step;
};

int main() {
  lint h, w, k;
  scanf("%lld", &h);
  scanf("%lld", &w);
  scanf("%lld", &k);

  lint sy, sx, gy, gx;
  scanf("%lld", &sy);
  scanf("%lld", &sx);
  scanf("%lld", &gy);
  scanf("%lld", &gx);

  std::vector<std::vector<lint>> maze(h, std::vector<lint>(w, 0));
  std::vector<std::vector<lint>> seen(h, std::vector<lint>(w, 1000000000));

  for (int i = 0; i < h; ++i) {
    std::string s;
    std::cin >> s;

    for (int j = 0; j < w; ++j) {
      if (s[j] == '@') {
        maze[i][j] = 1;
      }
    }
  }

  std::vector<std::pair<lint, lint>> dirs;
  dirs.emplace_back(1, 0);
  dirs.emplace_back(-1, 0);
  dirs.emplace_back(0, 1);
  dirs.emplace_back(0, -1);

  std::deque<Step> q;

  q.push_back(Step{sy - 1, sx - 1, 0});
  seen[sy - 1][sx - 1] = 0;

  lint done = 0;
  lint ans = -1;

  while (!q.empty()) {
    auto cur = q.front();
    q.pop_front();
    lint nx_step = cur.step + 1;
    for (auto dir : dirs) {
      lint nx_y = cur.y;
      lint nx_x = cur.x;
      for (int i = 0; i < k; ++i) {
        nx_y += dir.first;
        nx_x += dir.second;
        if (0 <= nx_y && nx_y < h && 0 <= nx_x && nx_x < w &&
            maze[nx_y][nx_x] != 1 && seen[nx_y][nx_x] >= nx_step) {
          if (nx_y == (gy - 1) && nx_x == (gx - 1)) {
            done = 1;
            ans = nx_step;
          }
          if (seen[nx_y][nx_x] > nx_step) {
            q.push_back(Step{nx_y, nx_x, nx_step});
          }
          seen[nx_y][nx_x] = nx_step;
        } else {
          break;
        }
      }
    }
    if (done == 1) {
      break;
    }
  }

  if (ans < 0) {
    std::cout << -1 << "\\n";
  } else {
    std::cout << ans << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ALL(X) X.begin(), X.end()
using lint = long long;
template <typename T> void print_container(std::vector<T> vec) {
  for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
}

class Step {
public:
  lint y;
  lint x;
  lint step;
};

int main() {
  lint h, w, k;
  scanf("%lld", &h);
  scanf("%lld", &w);
  scanf("%lld", &k);

  lint sy, sx, gy, gx;
  scanf("%lld", &sy);
  scanf("%lld", &sx);
  scanf("%lld", &gy);
  scanf("%lld", &gx);

  std::vector<std::vector<lint>> maze(h, std::vector<lint>(w, 0));
  std::vector<std::vector<lint>> seen(h, std::vector<lint>(w, 1000000000));

  for (int i = 0; i < h; ++i) {
    std::string s;
    std::cin >> s;

    for (int j = 0; j < w; ++j) {
      if (s[j] == '@') {
        maze[i][j] = 1;
      }
    }
  }

  std::vector<std::pair<lint, lint>> dirs;
  dirs.emplace_back(1, 0);
  dirs.emplace_back(-1, 0);
  dirs.emplace_back(0, 1);
  dirs.emplace_back(0, -1);

  std::deque<Step> q;

  q.push_back(Step{sy - 1, sx - 1, 0});
  seen[sy - 1][sx - 1] = 1;

  lint done = 0;
  lint ans = -1;

  while (!q.empty()) {
    auto cur = q.front();
    q.pop_front();
    lint nx_step = cur.step + 1;
    for (auto dir : dirs) {
      lint nx_y = cur.y;
      lint nx_x = cur.x;
      for (int i = 0; i < k; ++i) {
        nx_y += dir.first;
        nx_x += dir.second;
        if (0 <= nx_y && nx_y < h && 0 <= nx_x && nx_x < w &&
            maze[nx_y][nx_x] != 1 && seen[nx_y][nx_x] >= nx_step) {
          if (nx_y == (gy - 1) && nx_x == (gx - 1)) {
            done = 1;
            ans = nx_step;
          }
          seen[nx_y][nx_x] = nx_step;
          q.push_back(Step{nx_y, nx_x, nx_step});
        } else {
          break;
        }
      }
    }
    if (done == 1) {
      break;
    }
  }

  if (ans < 0) {
    std::cout << -1 << "\\n";
  } else {
    std::cout << ans << "\\n";
  }

  return 0;
}