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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
const int INF = 2012345678;
const ll LLINF = 4012345678012345678LL;
const ll MOD = 1000000007; // 998244353; //
const ld PI = 3.1415926535898;
const ld EPS = 1e-9;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll expo(ll b, ll p, ll m) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * b) % m;
    b = (b * b) % m;
    p >>= 1;
  }
  return res;
}
inline ll modinv(ll a, ll m) { return expo(a, m - 2, m); }

int main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  int xa, ya, xb, yb;
  scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
  xa--;
  ya--;
  xb--;
  yb--;
  char grid[h + 5][w + 5];
  for (int i = 0; i < h; i++) {
    scanf("%s", grid[i]);
  }
  pair<int, int> dist[4][h][w];
  for (int a = 0; a < 4; a++) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        dist[a][i][j] = mp(INF, INF);
      }
    }
  }
  int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  deque<pair<int, int>> q;
  q.push_back(mp(xa, ya));
  for (int a = 0; a < 4; a++)
    dist[a][xa][ya] = mp(0, k);
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = cur.fi + dx[i], ny = cur.se + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (grid[nx][ny] == '@')
        continue;
      for (int j = 0; j < 4; j++) {
        if (i == j) {
          if (dist[j][cur.fi][cur.se].se == k) {
            if (dist[i][nx][ny].fi > dist[j][cur.fi][cur.se].fi + 1) {
              dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
              q.push_back(mp(nx, ny));
            } else if (dist[i][nx][ny].fi == dist[j][cur.fi][cur.se].fi + 1) {
              if (dist[i][nx][ny].se > 1) {
                dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
                q.push_back(mp(nx, ny));
              }
            }
          } else {
            if (dist[i][nx][ny].fi > dist[j][cur.fi][cur.se].fi) {
              dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi,
                                   dist[j][cur.fi][cur.se].se + 1);
              q.push_back(mp(nx, ny));
            } else if (dist[i][nx][ny].fi == dist[j][cur.fi][cur.se].fi) {
              if (dist[i][nx][ny].se > dist[j][cur.fi][cur.se].se + 1) {
                dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi,
                                     dist[j][cur.fi][cur.se].se + 1);
                q.push_back(mp(nx, ny));
              }
            }
          }
        } else {
          if (dist[i][nx][ny].fi > dist[j][cur.fi][cur.se].fi + 1) {
            dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
            q.push_back(mp(nx, ny));
          } else if (dist[i][nx][ny].fi == dist[j][cur.fi][cur.se].fi + 1) {
            if (dist[i][nx][ny].se > 1) {
              dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
              q.push_back(mp(nx, ny));
            }
          }
        }
      }
    }
  }
  /*
  for(int a=0;a<4;a++){
          for(int i=0;i<h;i++){
                  for(int j=0;j<w;j++)printf("%d ",dist[a][i][j].fi);
                  printf("\\n");
          }
          printf("\\n");
  }
   */
  int ans = INF;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[i][xb][yb].fi);
  if (ans >= INF)
    printf("-1");
  else
    printf("%d", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
const int INF = 2012345678;
const ll LLINF = 4012345678012345678LL;
const ll MOD = 1000000007; // 998244353; //
const ld PI = 3.1415926535898;
const ld EPS = 1e-9;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll expo(ll b, ll p, ll m) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * b) % m;
    b = (b * b) % m;
    p >>= 1;
  }
  return res;
}
inline ll modinv(ll a, ll m) { return expo(a, m - 2, m); }

int main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  int xa, ya, xb, yb;
  scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
  xa--;
  ya--;
  xb--;
  yb--;
  char grid[h + 5][w + 5];
  for (int i = 0; i < h; i++) {
    scanf("%s", grid[i]);
  }
  pair<int, int> dist[4][h][w];
  for (int a = 0; a < 4; a++) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        dist[a][i][j] = mp(INF, INF);
      }
    }
  }
  int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  deque<pair<int, int>> q;
  q.push_back(mp(xa, ya));
  for (int a = 0; a < 4; a++)
    dist[a][xa][ya] = mp(0, k);
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = cur.fi + dx[i], ny = cur.se + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (grid[nx][ny] == '@')
        continue;
      bool b = 0, f = 0;
      for (int j = 0; j < 4; j++) {
        if (i == j) {
          if (dist[j][cur.fi][cur.se].se == k) {
            dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
            b = 1;
          } else {
            if (dist[i][nx][ny].fi > dist[j][cur.fi][cur.se].fi) {
              dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi,
                                   dist[j][cur.fi][cur.se].se + 1);
              f = 1;
            } else if (dist[i][nx][ny].fi == dist[j][cur.fi][cur.se].fi) {
              if (dist[i][nx][ny].se > dist[j][cur.fi][cur.se].se + 1) {
                dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi,
                                     dist[j][cur.fi][cur.se].se + 1);
                f = 1;
              }
            }
          }
        } else {
          if (dist[i][nx][ny].fi > dist[j][cur.fi][cur.se].fi + 1) {
            dist[i][nx][ny] = mp(dist[j][cur.fi][cur.se].fi + 1, 1);
            b = 1;
          }
        }
      }
      if (f) {
        q.push_front(mp(nx, ny));
      } else if (b) {
        q.push_back(mp(nx, ny));
      }
    }
  }
  /*
  for(int a=0;a<4;a++){
          for(int i=0;i<h;i++){
                  for(int j=0;j<w;j++)printf("%d ",dist[a][i][j].fi);
                  printf("\\n");
          }
          printf("\\n");
  }
  // */
  int ans = INF;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dist[i][xb][yb].fi);
  }
  if (ans >= INF)
    printf("-1");
  else
    printf("%d", ans);
}