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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
// max_element

int H, W, K;
int X1, Y1, X2, Y2;
bool C[1000010];
int result[4000010];
bool used[4000010];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int Ind(int x, int y) { return x + y * W; }

int Ind(int x, int y, int d) { return x * 4 + y * W * 4 + d; }

bool IsIn(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

//---------- ダイクストラ pair<index,len> ----------
void dijkstra() {
  result[Ind(X1, Y1, 0)] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q; // len, index
  q.push(pair<int, int>(0, Ind(X1, Y1, 0)));

  while (q.size() > 0) {
    int len, index;
    tie(len, index) = q.top();
    q.pop();
    if (used[index])
      continue;
    used[index] = true;

    int cd = index % 4;
    int x = (index / 4) % W;
    int y = (index / 4) / W;

    int rd = (cd + 1) % 4;
    int rlen = ceil(len / (double)K) * K;
    int rindex = Ind(x, y, rd);
    result[rindex] = rlen;
    q.push(pair<int, int>(rlen, rindex));

    int nx = x + dx[cd];
    int ny = y + dy[cd];
    if (IsIn(nx, ny) && !C[Ind(nx, ny)]) {
      int nindex = Ind(nx, ny, cd);
      result[nindex] = len + 1;
      q.push(pair<int, int>(len + 1, nindex));
    }
  }
}

signed main() {
  cin >> H >> W >> K;
  cin >> Y1 >> X1 >> Y2 >> X2;
  Y1--;
  X1--;
  Y2--;
  X2--;
  REP(y, H) {
    string s;
    cin >> s;
    REP(x, W) { C[Ind(x, y)] = (s[x] == '@'); }
  }

  for (int i = 0; i < 4000010; i++) {
    result[i] = INT64_MAX;
  }

  dijkstra();

  int m = INT_MAX;
  for (int d = 0; d < 4; d++) {
    int res = result[Ind(X2, Y2, d)];
    m = min(m, res);
  }

  if (m == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ceil(m / (double)K) << endl;
  }
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
// #define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
// max_element

int H, W, K;
int X1, Y1, X2, Y2;

int _dx[] = {1, 0, -1, 0};
int _dy[] = {0, 1, 0, -1};

int Ind(int x, int y) { return y * W + x; }

bool IsIn(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

bool _map[1000010];
bool _flg[1000010];
int _warp[1000010][4];

signed main() {
  cin >> H >> W >> K;
  cin >> Y1 >> X1 >> Y2 >> X2;
  X1--;
  Y1--;
  X2--;
  Y2--;

  for (int y = 0; y < H; y++) {
    string s;
    cin >> s;
    for (int x = 0; x < W; x++) {
      bool f = (s[x] == '@');
      _map[Ind(x, y)] = f;
    }
  }

  queue<int> q;
  q.push(X1);
  q.push(Y1);
  _flg[Ind(X1, Y1)] = true;
  int step = 1;
  while (q.size() > 0) {
    int q_size = q.size() / 2;
    for (int i = 0; i < q_size; i++) {
      int x = q.front();
      q.pop();
      int y = q.front();
      q.pop();

      for (int d = 0; d < 4; d++) {
        int init_k = _warp[Ind(x, y)][d];
        int k;
        for (k = init_k; k <= K; k++) {
          int dx = x + _dx[d] * k;
          int dy = y + _dy[d] * k;

          if (dx == X2 && dy == Y2) {
            cout << step << endl;
            return 0;
          }

          bool is_in = IsIn(dx, dy);
          int ind = Ind(dx, dy);
          if (!is_in || _map[ind]) {
            break;
          }

          if (!_flg[Ind(dx, dy)]) {
            _flg[Ind(dx, dy)] = true;
            q.push(dx);
            q.push(dy);
          }
        }

        for (int m = init_k; m < k; m++) {
          int dx = x + _dx[d] * m;
          int dy = y + _dy[d] * m;
          _warp[Ind(dx, dy)][d] = max((k - 1) - m, _warp[Ind(dx, dy)][d]);
        }

        // for (int d = 0; d < 4; d++)
        //{
        //	cout << "----" << d << "----" << endl;
        //	for (int y = 0; y < H; y++)
        //	{
        //		for (int x = 0; x < W; x++)
        //		{
        //			cout << _warp[Ind(x, y)][d];
        //		}
        //		cout << endl;
        //	}
        //	cout << endl;
        // }
      }
    }
    step++;
  }
  cout << -1 << endl;
}