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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>,
                 greater<tuple<int, int, int, int>>>
      que;
  que.push(make_tuple(0, x1, y1, 0));
  que.push(make_tuple(0, x1, y1, 1));
  que.push(make_tuple(0, x1, y1, 2));
  que.push(make_tuple(0, x1, y1, 3));
  int res[H][W][4];
  // map<tuple<int, int, int>, int> amap;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < 4; ++k) {
        res[i][j][k] = inf;
      }
    }
  }
  tuple<int, int, int, int> state;
  int x, y, cost, dir;
  while (!que.empty()) {
    state = que.top();
    que.pop();
    x = get<1>(state);
    y = get<2>(state);
    cost = get<0>(state);
    dir = get<3>(state);
    if (res[x][y][dir] <= cost)
      continue;
    res[x][y][dir] = cost;
    if (dir == 0) {
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (res[x + 1][y][0] > cost + 1) {
          que.push(make_tuple(cost + 1, x + 1, y, 0));
        }
      }
      if (y > 0 && S[x][y - 1] != '@') {
        if (res[x][y - 1][1] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y - 1, 1));
        }
      }
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (res[x][y + 1][3] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y + 1, 3));
        }
      }
    }
    if (dir == 1) {
      if (y > 0 && S[x][y - 1] != '@') {
        if (res[x][y - 1][1] > cost + 1) {
          que.push(make_tuple(cost + 1, x, y - 1, 1));
        }
      }
      if (x > 0 && S[x - 1][y] != '@') {
        if (res[x - 1][y][2] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x - 1, y, 2));
        }
      }
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (res[x + 1][y][0] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x + 1, y, 0));
        }
      }
    }
    if (dir == 2) {
      if (x > 0 && S[x - 1][y] != '@') {
        if (res[x - 1][y][2] > cost + 1) {
          que.push(make_tuple(cost + 1, x - 1, y, 2));
        }
      }
      if (y > 0 && S[x][y - 1] != '@') {
        if (res[x][y - 1][1] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y - 1, 1));
        }
      }
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (res[x][y + 1][3] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y + 1, 3));
        }
      }
    }
    if (dir == 3) {
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (res[x][y + 1][3] > cost + 1) {
          que.push(make_tuple(cost + 1, x, y + 1, 3));
        }
      }
      if (x > 0 && S[x - 1][y] != '@') {
        if (res[x - 1][y][2] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x - 1, y, 2));
        }
      }
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (res[x + 1][y][0] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x + 1, y, 0));
        }
      }
    }
  }
  /**
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      for(int k=1;k<=4;++k) {
        cout<<amap[make_tuple(i, j, k)]<<" ";
      }
      cout<<" ";
    }
    cout<<endl;
  }
  **/
  int ans = inf;
  for (int i = 0; i < 4; ++i)
    ans = min(ans, res[x2][y2][i]);
  if (ans == inf)
    cout << -1 << endl;
  else if (ans % K == 0)
    cout << ans / K << endl;
  else
    cout << ans / K + 1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>,
                 greater<tuple<int, int, int, int>>>
      que;
  que.push(make_tuple(0, x1, y1, 1));
  que.push(make_tuple(0, x1, y1, 2));
  que.push(make_tuple(0, x1, y1, 3));
  que.push(make_tuple(0, x1, y1, 4));
  map<tuple<int, int, int>, int> amap;
  map<tuple<int, int, int>, int> bmap;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 1; k <= 4; ++k) {
        amap[make_tuple(i, j, k)] = inf;
      }
    }
  }
  tuple<int, int, int, int> state;
  int x, y, cost, dir;
  while (!que.empty()) {
    state = que.top();
    que.pop();
    x = get<1>(state);
    y = get<2>(state);
    cost = get<0>(state);
    dir = get<3>(state);
    if (amap[make_tuple(x, y, dir)] <= cost)
      continue;
    amap[make_tuple(x, y, dir)] = cost;
    if (dir == 1) {
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (amap[make_tuple(x + 1, y, 1)] > cost + 1) {
          que.push(make_tuple(cost + 1, x + 1, y, 1));
        }
      }
      if (y > 0 && S[x][y - 1] != '@') {
        if (amap[make_tuple(x, y - 1, 2)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y - 1, 2));
        }
      }
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (amap[make_tuple(x, y + 1, 4)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y + 1, 4));
        }
      }
    }
    if (dir == 2) {
      if (y > 0 && S[x][y - 1] != '@') {
        if (amap[make_tuple(x, y - 1, 2)] > cost + 1) {
          que.push(make_tuple(cost + 1, x, y - 1, 2));
        }
      }
      if (x > 0 && S[x - 1][y] != '@') {
        if (amap[make_tuple(x - 1, y, 3)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x - 1, y, 3));
        }
      }
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (amap[make_tuple(x + 1, y, 1)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x + 1, y, 1));
        }
      }
    }
    if (dir == 3) {
      if (x > 0 && S[x - 1][y] != '@') {
        if (amap[make_tuple(x - 1, y, 3)] > cost + 1) {
          que.push(make_tuple(cost + 1, x - 1, y, 3));
        }
      }
      if (y > 0 && S[x][y - 1] != '@') {
        if (amap[make_tuple(x, y - 1, 2)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y - 1, 2));
        }
      }
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (amap[make_tuple(x, y + 1, 4)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x, y + 1, 4));
        }
      }
    }
    if (dir == 4) {
      if (y < W - 1 && S[x][y + 1] != '@') {
        if (amap[make_tuple(x, y + 1, 4)] > cost + 1) {
          que.push(make_tuple(cost + 1, x, y + 1, 4));
        }
      }
      if (x > 0 && S[x - 1][y] != '@') {
        if (amap[make_tuple(x - 1, y, 3)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x - 1, y, 3));
        }
      }
      if (x < H - 1 && S[x + 1][y] != '@') {
        if (amap[make_tuple(x + 1, y, 1)] > cost + (K - cost % K) % K + 1) {
          que.push(make_tuple(cost + (K - cost % K) % K + 1, x + 1, y, 1));
        }
      }
    }
  }
  /**
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      for(int k=1;k<=4;++k) {
        cout<<amap[make_tuple(i, j, k)]<<" ";
      }
      cout<<" ";
    }
    cout<<endl;
  }
  **/
  int ans = inf;
  for (int i = 1; i <= 4; ++i)
    ans = min(ans, amap[make_tuple(x2, y2, i)]);
  if (ans == inf)
    cout << -1 << endl;
  else if (ans % K == 0)
    cout << ans / K << endl;
  else
    cout << ans / K + 1 << endl;
}