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

#include <bits/stdc++.h>
#include <iostream>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef pair<P, PP> PPP;
ll a, b, c, d, e;
ll h, w, k, sx, sy, gx, gy, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, ans, nx,
                            ny;
priority_queue<PPP, vector<PPP>, greater<PPP>> que;
string s[1000000];
int main(void) {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  P dp[h][w][4];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = {1e9, 0};
      }
    }
  }
  for (int i = 0; i < h; i++)
    cin >> s[i];
  for (int i = 0; i < 4; i++) {
    que.push({{1, 0}, {{sx, sy}, i}});
  }
  while (que.size()) {
    PPP p = que.top();
    que.pop();
    a = p.F.F;
    b = p.F.S;
    c = p.S.F.F;
    d = p.S.F.S;
    e = p.S.S;
    P q = {a, b};
    if (dp[c][d][e] <= q)
      continue;
    dp[c][d][e] = q;
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
    if (k > b) {
      nx = c + dx[e], ny = d + dy[e];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] == '.') {
        P o = {a, b + 1};
        if (dp[nx][ny][e] > o)
          que.push({{a, b + 1}, {{nx, ny}, e}});
      }
    } else {
      nx = c + dx[e], ny = d + dy[e];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] == '.') {
        P o = {a + 1, 1};
        if (dp[nx][ny][e] > o)
          que.push({{a + 1, 1}, {{nx, ny}, e}});
      }
    }
    for (int i = 0; i < 4; i++) {
      P o = {a + 1, 0};
      if (dp[c][d][i] > o)
        que.push({{a + 1, 0}, {{c, d}, i}});
    }
  }
  ans = 1e9;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dp[gx][gy][i].F);
  }
  if (ans == 1e9) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef pair<P, PP> PPP;
ll a, b, c, d, e;
ll h, w, k, sx, sy, gx, gy, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, ans, nx,
                            ny;
priority_queue<PPP, vector<PPP>, greater<PPP>> que;
string s[1000000];
int main(void) {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  P dp[h][w][4];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = {1e9, 0};
      }
    }
  }
  for (int i = 0; i < h; i++)
    cin >> s[i];
  for (int i = 0; i < 4; i++) {
    que.push({{1, 0}, {{sx, sy}, i}});
  }
  while (que.size()) {
    PPP p = que.top();
    que.pop();
    a = p.F.F;
    b = p.F.S;
    c = p.S.F.F;
    d = p.S.F.S;
    e = p.S.S;
    P q = {a, b};
    if (dp[c][d][e] <= q)
      continue;
    dp[c][d][e] = q;
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
    if (k > b) {
      nx = c + dx[e], ny = d + dy[e];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] == '.') {
        que.push({{a, b + 1}, {{nx, ny}, e}});
      }
    } else {
      nx = c + dx[e], ny = d + dy[e];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] == '.') {
        que.push({{a + 1, 1}, {{nx, ny}, e}});
      }
    }
    for (int i = 0; i < 4; i++) {
      que.push({{a + 1, 0}, {{c, d}, i}});
    }
  }
  ans = 1e9;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dp[gx][gy][i].F);
  }
  if (ans == 1e9) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}