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

// solution for ABC170F
// author : lynmisakura

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

#define REP(i, n) for (int(i) = 0; (i) < (n); i++)

using ll = long long;
using P = pair<int, int>;

int h, w, k;

int s[2], t[2];
vector<string> g;

int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int INF = 1e9;

void input() {
  cin >> h >> w >> k;
  cin >> s[0] >> s[1] >> t[0] >> t[1];
  s[0]--, s[1]--, t[0]--, t[1]--;

  REP(i, h) {
    string s;
    cin >> s;
    g.push_back(s);
  }
}

P dist[4000010];

int id(int x, int y, int d) { return 4 * (x * w + y) + d; }

int x_, y_, d_;
void get_posi(int id) {
  d_ = id % 4;
  id /= 4;
  y_ = id % w;
  x_ = id / w;
}

bool inner(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }

void print() {
  REP(i, h) {
    REP(j, w) {
      int res = INF;
      REP(k, 4)
      res = min(res, dist[id(i, j, k)].first + (dist[id(i, j, k)].second > 0));
      cout << setw(5) << (res == INF ? "INF" : to_string(res));
    }
    cout << '\\n';
  }
}

bool cmp(int now, int next_) {

  P tmp = dist[now];

  int p = tmp.first;
  int q = tmp.second;

  if (next_ % 4 == d_) {
    q++;
    if (q == k) {
      q = 0;
      p++;
    }

  } else {
    if (q == 0) {
      q++;
      if (q == k) {
        p++;
        q = 0;
      }
    } else
      p++, q = 1;
  }

  if (dist[next_].first > p ||
      (dist[next_].first == p && dist[next_].second > q)) {
    dist[next_] = (P){p, q};
    return true;
  } else
    return false;
}

int main(void) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  input();

  REP(i, h * w * 4) dist[i].first = 1000000000;

  REP(i, 4) dist[id(s[0], s[1], i)] = make_pair(0, 0);

  // print();
  //  BFS
  queue<int> q;
  REP(i, 4) { q.push(id(s[0], s[1], i)); }

  while (q.size()) {
    int now = q.front();
    q.pop();

    get_posi(now);

    REP(d, 4) {
      int nx = x_ + dx[d];
      int ny = y_ + dy[d];

      if (inner(nx, ny)) {

        if (g[nx][ny] == '@')
          continue;

        int nid = id(nx, ny, d);
        if (cmp(now, nid)) {
          q.push(nid);
        }
      }
    }
  }

  int ans = INF;
  REP(i, 4) {
    // cout << dist[id(t[0],t[1],i)].first << '\\n';
    ans = min(ans, dist[id(t[0], t[1], i)].first +
                       (int)(dist[id(t[0], t[1], i)].second > 0));
  }

  // print();

  if (ans == INF) {
    cout << -1 << '\\n';
  } else
    cout << ans << '\\n';
}


Error: TLE

Fixed submission:

/*
 * welcome to my code!
 * ------------------------------------------
 * author : lynmisakura(twitter : @andoreiji11)
 */

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

using ll = long long;

// macro templates

#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define dv(TYPE, VAL)                                                          \\
  TYPE VAL;                                                                    \\
  cin >> VAL;
#define vi(TYPE, NAME, SIZE, INIT) vector<TYPE> NAME(SIZE, (INIT))
#define vin(TYPE, NAME, SIZE)                                                  \\
  vector<TYPE> NAME(SIZE);                                                     \\
  rep(i, SIZE) cin >> NAME[i]
#define vout(ARRAY)                                                            \\
  for (auto i : ARRAY)                                                         \\
    cout << i << ' ';                                                          \\
  cout << '\\n'
#define vvi(TYPE, NAME, LOW, COL, INIT)                                        \\
  vector<vector<TYPE>> NAME(LOW, vector<TYPE>(COL, INIT))
#define vpi(T1, T2, NAME, SIZE, I1, I2)                                        \\
  vector<pair<T1, T2>> NAME(SIZE, make_pair(I1, I2))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define dup(x, y) (x + y - 1) / (y)
#define sz(v) (int64_t) v.size()
#define CUM(arr, cum)                                                          \\
  vector<ll> cum(sz(a) + 1, 0);                                                \\
  rep(i, sz(a)) cum[i + 1] = cum[i] + 1
#define PY(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "YES" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define PN(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "NO" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define PYN(COND) cout << ((COND) ? "YES" : "NO") << '\\n';
#define Py(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "Yes" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define Pn(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "No" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define Pyn(COND)                                                              \\
  cout << ((COND) ? "Yes" : "No") << '\\n';                                     \\
  return

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pi acos(-1)
#define fi first
#define se second
#define pq priority_queue

#define MULTITEST                                                              \\
  ;                                                                            \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  rep(_, t) main2();
#define IOB                                                                    \\
  ;                                                                            \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return false;
}

// templates end

int64_t h, w, k;
int64_t s[2], t[2];

vector<string> g;
vector<vector<int>> dist;
int64_t dx[4] = {1, 0, -1, 0};
int64_t dy[4] = {0, -1, 0, 1};

void dfs(int64_t x, int64_t y, int64_t d = 0) {
  dist[x][y] = d;
  rep(dir, 4) {
    for (int i = 1; i <= k; i++) {
      int64_t nx = x + dx[dir] * i;
      int64_t ny = y + dy[dir] * i;
      if (nx >= 0 and nx < h and ny >= 0 and ny < w) {
        if (g[nx][ny] == '@')
          break;
        if (dist[nx][ny] > dist[x][y] + 1) {
          dfs(nx, ny, d + 1);
        }
      }
    }
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  dist.resize(h, vector<int>(w, INT_MAX));
  cin >> s[0] >> s[1] >> t[0] >> t[1];
  s[0]--, s[1]--, t[0]--, t[1]--;
  rep(i, h) {
    string foo;
    cin >> foo;
    g.pb(foo);
  }

  dfs(s[0], s[1]);
  if (dist[t[0]][t[1]] == INT_MAX) {
    cout << -1 << '\\n';
    return 0;
  }
  cout << dist[t[0]][t[1]] << '\\n';
}