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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// FUCK.......MISSED AN EASY ONE ....SHITTT

#define int long long
// #define ll long long
#define N 1005
#define M 1000000009
#define pi pair<int, int>
#define F first
#define S second
#define pb push_back
#define ld long double
#define inf 200000000009
#define pii pair<pi, pi>
//||

//"\\n"
vector<string> s;
int n, m, k;
vector<vector<vector<int>>> vis, d, vis2;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int check(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == '@')
    return 0;
  return 1;
}
void bfs(int x, int y) {
  int j;
  deque<pii> q; // x,y,dir,dist(k waala)
  q.push_front({{x, y}, {0, 0}});
  q.push_front({{x, y}, {1, 0}});
  q.push_front({{x, y}, {2, 0}});
  q.push_front({{x, y}, {3, 0}});

  d[x][y][0] = 1;
  d[x][y][1] = 1;
  d[x][y][2] = 1;
  d[x][y][3] = 1;

  vis[x][y][0] = 1;
  vis[x][y][1] = 1;
  vis[x][y][2] = 1;
  vis[x][y][3] = 1;

  while (!q.empty()) {
    pii x = q.front();

    int x1 = x.F.F;
    int y1 = x.F.S;
    int d1 = x.S.F;
    int dist = x.S.S;

    q.pop_front();

    if (vis2[x1][y1][d1])
      continue;
    vis2[x1][y1][d1] = 1;

    for (j = 0; j < 4; j++) {
      if (check(x1 + dx[j], y1 + dy[j])) {
        // cout<<x1+dx[j]<<" "<<y1+dy[j]<<endl;
        if (j == d1 && dist < k &&
            d[x1 + dx[j]][y1 + dy[j]][j] > d[x1][y1][d1]) {
          q.push_front({{x1 + dx[j], y1 + dy[j]}, {j, dist + 1}});
          d[x1 + dx[j]][y1 + dy[j]][j] =
              min(d[x1 + dx[j]][y1 + dy[j]][j], d[x1][y1][d1]);
          vis[x1 + dx[j]][y1 + dy[j]][j] = 1;
        }

        else if (d[x1 + dx[j]][y1 + dy[j]][j] > d[x1][y1][d1] + 1) {
          q.push_back({{x1 + dx[j], y1 + dy[j]}, {j, 1}});

          d[x1 + dx[j]][y1 + dy[j]][j] =
              min(d[x1 + dx[j]][y1 + dy[j]][j], d[x1][y1][d1] + 1);
          vis[x1 + dx[j]][y1 + dy[j]][j] = 1;
        }
      }
    }
  }
}

main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int i, ans = (int)1e9, j;
  cin >> n >> m >> k;
  s.resize(n);
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<int>> temp;
  temp.assign(m, vector<int>(4, 0));
  vis.assign(n, temp);
  d.assign(n, temp);
  vis2.assign(n, temp);

  for (i = 0; i < n; i++)
    cin >> s[i];

  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      for (int gg = 0; gg < 4; gg++)
        d[i][j][gg] = (int)1e9;

  x1--;
  y1--;
  x2--;
  y2--;
  bfs(x1, y1);
  if (x1 == x2 && y1 == y2) {
    cout << 0 << endl;
    return 0;
  }

  // cout<<d[0][2][1]<<endl;
  for (j = 0; j < 4; j++)
    ans = min(ans, d[x2][y2][j]);
  if (ans == (int)1e9)
    cout << -1;
  else
    cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 1e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);

#endif
}

vector<vector<char>> vcr;
vector<vii> dis;

class node {
public:
  int x;
  int y;
  int dir;
  int rm;
  node(int cx, int cy, int cdir, int crm) {
    x = cx;
    y = cy;
    dir = cdir;
    rm = crm;
  }
};

struct compare {
  bool operator()(node &a, node &b) { return a.x < b.x; }
};
void solve() {

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  vcr.assign(n + 1, vector<char>(m + 1, 'a'));

  dis.assign(n + 1, vii(m + 1, big));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> vcr[i][j];
  }

  priority_queue<node, vector<node>, compare> bfs;

  bfs.push(node(x1, y1, 0, k));
  dis[x1][y1] = 0;

  while (sz(bfs) > 0) {
    int cx = bfs.top().x;
    int cy = bfs.top().y;
    int cdir = bfs.top().dir;
    int crm = bfs.top().rm;
    bfs.pop();

    if (cx + 1 <= n && vcr[cx + 1][cy] != '@') {
      if (cdir == 4) {
        if (crm > 0 && dis[cx + 1][cy] >= dis[cx][cy])
          dis[cx + 1][cy] = dis[cx][cy], bfs.push(node(cx + 1, cy, 4, crm - 1));
        if (dis[cx + 1][cy] >= dis[cx][cy] + 1)
          dis[cx + 1][cy] = dis[cx][cy] + 1,
                   bfs.push(node(cx + 1, cy, 4, k - 1));
      } else {
        if (dis[cx + 1][cy] >= dis[cx][cy] + 1)
          dis[cx + 1][cy] = dis[cx][cy] + 1,
                   bfs.push(node(cx + 1, cy, 4, k - 1));
      }
    }
    if (cx - 1 >= 1 && vcr[cx - 1][cy] != '@') {
      if (cdir == 2) {
        if (crm > 0 && dis[cx - 1][cy] >= dis[cx][cy])
          dis[cx - 1][cy] = dis[cx][cy], bfs.push(node(cx - 1, cy, 2, crm - 1));
        if (dis[cx - 1][cy] >= dis[cx][cy] + 1)
          dis[cx - 1][cy] = dis[cx][cy] + 1,
                   bfs.push(node(cx - 1, cy, 2, k - 1));
      } else {
        if (dis[cx - 1][cy] >= dis[cx][cy] + 1)
          dis[cx - 1][cy] = dis[cx][cy] + 1,
                   bfs.push(node(cx - 1, cy, 2, k - 1));
      }
    }
    if (cy - 1 >= 1 && vcr[cx][cy - 1] != '@') {
      if (cdir == 3) {
        if (crm > 0 && dis[cx][cy - 1] >= dis[cx][cy])
          dis[cx][cy - 1] = dis[cx][cy], bfs.push(node(cx, cy - 1, 3, crm - 1));
        if (dis[cx][cy - 1] >= dis[cx][cy] + 1)
          dis[cx][cy - 1] = dis[cx][cy] + 1,
                       bfs.push(node(cx, cy - 1, 3, k - 1));
      } else {
        if (dis[cx][cy - 1] >= dis[cx][cy] + 1)
          dis[cx][cy - 1] = dis[cx][cy] + 1,
                       bfs.push(node(cx, cy - 1, 3, k - 1));
      }
    }
    if (cy + 1 <= m && vcr[cx][cy + 1] != '@') {
      if (cdir == 1) {
        if (crm > 0 && dis[cx][cy + 1] >= dis[cx][cy])
          dis[cx][cy + 1] = dis[cx][cy], bfs.push(node(cx, cy + 1, 1, crm - 1));
        if (dis[cx][cy + 1] >= dis[cx][cy] + 1)
          dis[cx][cy + 1] = dis[cx][cy] + 1,
                       bfs.push(node(cx, cy + 1, 1, k - 1));
      } else {
        if (dis[cx][cy + 1] >= dis[cx][cy] + 1)
          dis[cx][cy + 1] = dis[cx][cy] + 1,
                       bfs.push(node(cx, cy + 1, 1, k - 1));
      }
    }
  }

  if (dis[x2][y2] == big) {
    cout << "-1\\n";
    return;
  }

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

int32_t main() {
  // pr_init();
  fastio;
  solve();
}