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

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define inf 1000000000000000000
#define N 1000000007
#define mp make_pair
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
template <class L, class R> ostream &operator<<(ostream &cout, pair<L, R> P) {
  return cout << '(' << P.x << ',' << P.y << ')';
}
template <class T> ostream &operator<<(ostream &cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V)
    cout << v << ' ';
  return cout << ']';
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node {
  int x, y;
  int cost;
  int dir;
  int fuel;
};
struct cmp {
  bool operator()(const node &n, const node &m) const {
    if (n.cost != m.cost) {
      return n.cost < m.cost;
    } else {
      if (n.fuel != m.fuel) {
        return n.fuel > m.fuel;
      }
      return mp(n.dir, mp(n.x, n.y)) < mp(m.dir, mp(m.x, m.y));
    }
  }
};
// edge cases when n=1 , m=1. wrong variable in loop. if<->while;merge
// resize;LLONG_MAX
signed main() {
  speed
      // global var
      int n,
      m, k;
  cin >> n >> m >> k;
  int x, y, a, b;
  cin >> x >> y >> a >> b;
  --x;
  --y;
  --a;
  --b;

  vector<string> inp(n);
  vector<vector<vector<int>>> cost(n,
                                   vector<vector<int>>(m, vector<int>(4, inf)));
  vector<vector<vector<bool>>> vis(n,
                                   vector<vector<bool>>(m, vector<bool>(4, 0)));
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
  }
  if (mp(x, y) == mp(a, b)) {
    cout << 0 << "\\n";
    return 0;
  }

  set<node, cmp> st;
  node curr;
  curr.x = x;
  curr.y = y;
  curr.cost = 0, curr.dir = 0, curr.fuel = 0; // seed
  st.insert(curr);

  while (!st.empty()) {
    auto pres = *st.begin();
    st.erase(st.begin());

    if (vis[pres.x][pres.y][pres.dir]) {
      continue;
    }

    // cout<<pres.x<<' '<<pres.y<<" "<<pres.dir<<" "<<pres.fuel<<"
    // "<<pres.cost<<"\\n";

    vis[pres.x][pres.y][pres.dir] = 1;
    cost[pres.x][pres.y][pres.dir] = pres.cost;
    if (pres.fuel != 0) {
      int nx = pres.x + dx[pres.dir];
      int ny = pres.y + dy[pres.dir];
      if (nx >= 0 and nx < n and ny >= 0 and ny < m and inp[nx][ny] == '.') {
        node nxt = pres;
        nxt.x = nx;
        nxt.y = ny;
        nxt.fuel--;
        st.insert(nxt);
      }
    }
    for (int _ = 0; _ < 4; ++_) {
      node nxt = pres;

      nxt.x += dx[_];
      nxt.y += dy[_];
      if (!(nxt.x >= 0 and nxt.y >= 0 and nxt.x < n and nxt.y < m and
            inp[nxt.x][nxt.y] == '.')) {
        continue;
      }
      nxt.dir = _;
      nxt.cost++;
      nxt.fuel = k - 1;
      st.insert(nxt);
    }
  }
  int ans = inf;
  for (int _ = 0; _ < 4; ++_) {
    ans = min(ans, cost[a][b][_]);
  }
  if (ans == inf) {
    cout << -1 << "\\n";
  } else {
    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define inf 1000000000000000000
#define N 1000000007
#define mp make_pair
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
template <class L, class R> ostream &operator<<(ostream &cout, pair<L, R> P) {
  return cout << '(' << P.x << ',' << P.y << ')';
}
template <class T> ostream &operator<<(ostream &cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V)
    cout << v << ' ';
  return cout << ']';
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node {
  int x, y;
  int cost;
  int dir;
  int fuel;
};
struct cmp {
  bool operator()(const node &n, const node &m) const {
    if (n.cost != m.cost) {
      return n.cost < m.cost;
    } else {
      if (n.fuel != m.fuel) {
        return n.fuel > m.fuel;
      }
      return mp(n.dir, mp(n.x, n.y)) < mp(m.dir, mp(m.x, m.y));
    }
  }
};
// edge cases when n=1 , m=1. wrong variable in loop. if<->while;merge
// resize;LLONG_MAX
signed main() {
  speed
      // global var
      int n,
      m, k;
  cin >> n >> m >> k;
  int x, y, a, b;
  cin >> x >> y >> a >> b;
  --x;
  --y;
  --a;
  --b;

  vector<string> inp(n);
  vector<vector<vector<int>>> cost(n,
                                   vector<vector<int>>(m, vector<int>(4, inf)));
  vector<vector<vector<bool>>> vis(n,
                                   vector<vector<bool>>(m, vector<bool>(4, 0)));
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
  }
  if (mp(x, y) == mp(a, b)) {
    cout << 0 << "\\n";
    return 0;
  }

  set<node, cmp> st;
  node curr;
  curr.x = x;
  curr.y = y;
  curr.cost = 0, curr.dir = 0, curr.fuel = 0; // seed
  st.insert(curr);

  while (!st.empty()) {
    auto pres = *st.begin();
    st.erase(st.begin());

    if (vis[pres.x][pres.y][pres.dir]) {
      continue;
    }

    // cout<<pres.x<<' '<<pres.y<<" "<<pres.dir<<" "<<pres.fuel<<"
    // "<<pres.cost<<"\\n";

    vis[pres.x][pres.y][pres.dir] = 1;
    cost[pres.x][pres.y][pres.dir] =
        min(pres.cost, cost[pres.x][pres.y][pres.dir]);
    if (pres.fuel != 0) {
      int nx = pres.x + dx[pres.dir];
      int ny = pres.y + dy[pres.dir];
      if (nx >= 0 and nx < n and ny >= 0 and ny < m and inp[nx][ny] == '.') {
        node nxt = pres;
        nxt.x = nx;
        nxt.y = ny;
        nxt.fuel--;
        st.insert(nxt);
      }
    } else {
      vis[pres.x][pres.y][pres.dir] = 0;
    }
    for (int _ = 0; _ < 4; ++_) {
      node nxt = pres;
      nxt.dir = _;
      nxt.cost++;
      nxt.fuel = k;
      st.insert(nxt);
    }
  }
  int ans = inf;
  for (int _ = 0; _ < 4; ++_) {
    ans = min(ans, cost[a][b][_]);
  }
  if (ans == inf) {
    cout << -1 << "\\n";
  } else {
    cout << ans << "\\n";
  }
}