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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

typedef pair<pair<int, int>, pair<int, pair<int, int>>> State;
const int INF = 1 << 29;

inline int dist(int x1, int y1, int x2, int y2, int K) {
  int dx = abs(x2 - x1);
  int dy = abs(y2 - y1);
  return (dx + K - 1) / K + (dy + K - 1) / K;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> data(H);
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  REP(i, H) { cin >> data[i]; }
  //  vector<vector<int> > a(H, vector<int>(W, 0));
  vector<vector<vector<pint>>> visited(
      H, vector<vector<pint>>(W, vector<pint>(4, make_pair(INF, 0))));
  priority_queue<State, vector<State>, greater<State>> q;
  //  int d = dist(x1, y1, x2, y2, K);
  REP(dir, 4) {
    visited[x1][y1][dir] = make_pair(0, 0);
    q.push(make_pair(make_pair(0, 0), make_pair(dir, make_pair(x1, y1))));
  }
  while (!q.empty()) {
    State cur = q.top();
    q.pop();
    const int cdir = cur.second.first;

    const int cx = cur.second.second.first;
    const int cy = cur.second.second.second;

    int cdist1 = cur.first.first;
    int cdist2 = cur.first.second;
    //    cout << cx+1 << " " << cy+1 << " " << cdir << "=" << cdist1 << "," <<
    //    cdist2 << endl;
    if (cx == x2 && cy == y2) {
      int ans = cdist1;
      if (cdist2 > 0)
        ans++;
      cout << ans << endl;
      return 0;
    }
    const int dx[4] = {1, 0, -1, 0};
    const int dy[4] = {0, -1, 0, 1};
#ifdef DEBUG
#endif
    const int nx = cx + dx[cdir];
    const int ny = cy + dy[cdir];
    if (nx < 0 || nx >= H || ny < 0 || ny >= W)
      continue;
    if (data[nx][ny] == '@')
      continue;
    for (int rot = -1; rot <= 1; rot++) {
      int ndir = (cdir + rot + 4) % 4;
      int ndist2 = cdist2;
      int ndist1 = cdist1;
      if (rot == 0) {
        ndist2++;
        if (ndist2 >= K) {
          ndist2 = 0;
          ndist1++;
        }
      } else {
        ndist2++;
        if (ndist2 > 0) {
          ndist2 = 0;
          ndist1++;
        }
      }
      if (rot != 0)
        ndist2 = 0;
      if (ndist1 > visited[nx][ny][ndir].first)
        continue;
      if (ndist1 == visited[nx][ny][ndir].first &&
          ndist2 >= visited[nx][ny][ndir].second)
        continue;
      visited[nx][ny][ndir] = make_pair(ndist1, ndist2);
      q.push(make_pair(make_pair(ndist1, ndist2),
                       make_pair(ndir, make_pair(nx, ny))));
    }
  }
  cout << -1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

typedef pair<int, pair<int, int>> State;
const int INF = 1 << 29;

inline int dist(int x1, int y1, int x2, int y2, int K) {
  int dx = abs(x2 - x1);
  int dy = abs(y2 - y1);
  return (dx + K - 1) / K + (dy + K - 1) / K;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> data(H);
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  REP(i, H) { cin >> data[i]; }
  vector<vector<int>> visited(H, vector<int>(W, INF));
  priority_queue<State, vector<State>, greater<State>> q;
  int d = dist(x1, y1, x2, y2, K);
  visited[x1][y1] = 0;
  q.push(make_pair(d, make_pair(x1, y1)));
  while (!q.empty()) {
    State cur = q.top();
    q.pop();
    const int cx = cur.second.first;
    const int cy = cur.second.second;
    int cd = visited[cx][cy];
    //    cout << cx << " " << cy << " " << cd << endl;
    if (cx == x2 && cy == y2) {
      cout << cd << endl;
      return 0;
    }
    for (int nx = cx + 1; nx < H; nx++) {
      const int ny = cy;
      if (data[nx][ny] == '@')
        break;
      int nd = cd + (abs(nx - cx) + K - 1) / K;
      if (nd >= visited[nx][ny])
        continue;
      visited[nx][ny] = nd;
      const int ed = nd + dist(nx, ny, x2, y2, K);
      q.push(make_pair(ed, make_pair(nx, ny)));
    }
    for (int nx = cx - 1; nx >= 0; nx--) {
      const int ny = cy;
      if (data[nx][ny] == '@')
        break;
      int nd = cd + (abs(nx - cx) + K - 1) / K;
      if (nd >= visited[nx][ny])
        continue;
      visited[nx][ny] = nd;
      const int ed = nd + dist(nx, ny, x2, y2, K);
      q.push(make_pair(ed, make_pair(nx, ny)));
    }
    for (int ny = cy + 1; ny < W; ny++) {
      const int nx = cx;
      if (data[nx][ny] == '@')
        break;
      int nd = cd + (abs(ny - cy) + K - 1) / K;
      if (nd >= visited[nx][ny])
        continue;
      visited[nx][ny] = nd;
      const int ed = nd + dist(nx, ny, x2, y2, K);
      q.push(make_pair(ed, make_pair(nx, ny)));
    }
    for (int ny = cy - 1; ny >= 0; ny--) {
      const int nx = cx;
      if (data[nx][ny] == '@')
        break;
      int nd = cd + (abs(ny - cy) + K - 1) / K;
      if (nd >= visited[nx][ny])
        continue;
      visited[nx][ny] = nd;
      const int ed = nd + dist(nx, ny, x2, y2, K);
      q.push(make_pair(ed, make_pair(nx, ny)));
    }
  }
  cout << -1 << endl;

  return 0;
}