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

#include <algorithm>     //sort,二分探索,など
#include <bitset>        //固定長bit集合
#include <cmath>         //pow,logなど
#include <complex>       //複素数
#include <deque>         //両端アクセスのキュー
#include <functional>    //sortのgreater
#include <iomanip>       //setprecision(浮動小数点の出力の誤差)
#include <iostream>      //入出力
#include <iterator>      //集合演算(積集合,和集合,差集合など)
#include <map>           //map(辞書)
#include <numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include <queue>         //キュー
#include <set>           //集合
#include <stack>         //スタック
#include <string>        //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility>       //pair
#include <vector>        //可変長配列

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define erep(i, n) for (int i = 1; i <= (n); ++i)
#define brep(i, n) for (int i = n; i >= 0; i--)
#define pb push_back
using namespace std;
using P = pair<int, int>;
using ll = long long;
using Graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  int x1, x2, y1, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<vector<int>> dist_pre(h, vector<int>(w, -1));

  dist[x1][y1] = 0;
  dist_pre[x1][y1] = 0;

  vector<string> f(h);

  rep(i, h) { cin >> f[i]; }

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1)); // スタートを push

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();
    // cout << "xy" << x << y << endl;

    for (int direction = 0; direction < 4; ++direction) {
      for (int step = 1; step <= k; step++) {
        int next_x = x + step * dx[direction];
        int next_y = y + step * dy[direction];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (f[next_x][next_y] == '@')
          break;
        if (dist[next_x][next_y] != -1 &&
            dist[next_x][next_y] < dist[x][y] + 1) {
          // cout << next_x << " " <<  next_y << endl;
          break;
        }
        if (dist[next_x][next_y] == -1) {
          // cout << next_x << next_y << endl;
          que.push(make_pair(next_x, next_y));
          dist[next_x][next_y] = dist[x][y] + 1;
          // if(next_x == x2 && next_y == y2){
          //     cout << dist[x2][y2] << endl;
          //     return 0;
          // }
        }
      }
    }
  }

  cout << dist[x2][y2] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>     //sort,二分探索,など
#include <bitset>        //固定長bit集合
#include <cmath>         //pow,logなど
#include <complex>       //複素数
#include <deque>         //両端アクセスのキュー
#include <functional>    //sortのgreater
#include <iomanip>       //setprecision(浮動小数点の出力の誤差)
#include <iostream>      //入出力
#include <iterator>      //集合演算(積集合,和集合,差集合など)
#include <map>           //map(辞書)
#include <numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include <queue>         //キュー
#include <set>           //集合
#include <stack>         //スタック
#include <string>        //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility>       //pair
#include <vector>        //可変長配列

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define erep(i, n) for (int i = 1; i <= (n); ++i)
#define brep(i, n) for (int i = n; i >= 0; i--)
#define pb push_back
using namespace std;
using P = pair<int, int>;
using ll = long long;
using Graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  int x1, x2, y1, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<vector<int>> dist_pre(h, vector<int>(w, -1));

  dist[x1][y1] = 0;
  dist_pre[x1][y1] = 0;

  vector<string> f(h);

  rep(i, h) { cin >> f[i]; }

  // たどり着くかどうか
  queue<pair<int, int>> que_pre;
  que_pre.push(make_pair(x1, y1)); // スタートを push

  while (!que_pre.empty()) {
    pair<int, int> current_pos = que_pre.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que_pre.pop();

    for (int direction = 0; direction < 4; ++direction) {
      int next_x = x + dx[direction];
      int next_y = y + dy[direction];
      if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
        continue;
      if (f[next_x][next_y] == '@')
        continue;

      if (dist_pre[next_x][next_y] == -1) {
        que_pre.push(make_pair(next_x, next_y));
        dist_pre[next_x][next_y] = dist_pre[x][y] + 1;
      }
    }
  }

  if (dist_pre[x2][y2] == -1) {
    cout << -1 << endl;
    return 0;
  }

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1)); // スタートを push

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();

    for (int direction = 0; direction < 4; ++direction) {
      for (int step = 1; step <= k; step++) {
        int next_x = x + step * dx[direction];
        int next_y = y + step * dy[direction];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (f[next_x][next_y] == '@')
          break;

        if (dist[next_x][next_y] == -1) {
          que.push(make_pair(next_x, next_y));
          dist[next_x][next_y] = dist[x][y] + 1;
          if (next_x == x2 && next_y == y2) {
            cout << dist[x2][y2] << endl;
            return 0;
          }
        }
      }
    }
  }
}