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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define arep(i, v) for (auto i : v)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define fi first
#define se second
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline void dump(T &v) {
  irep(i, v) { cout << *i << ((i == --v.end()) ? '\\n' : ' '); }
}
inline string getline() {
  string s;
  getline(cin, s);
  return s;
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
struct io {
  io() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
};
const int INF = INT_MAX;
constexpr ll LLINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-11;

int h, w, k;
int x1, x2, Y1, y2;

// 各座標に格納したい情報を構造体にする
// 今回はX座標,Y座標,深さ(距離)を記述している
struct Corr {
  int x;
  int y;
  int muki;
};
queue<Corr> q;
int bfs(vs grid) {
  vector<vector<vpii>> ans(h, vector<vpii>(w, vpii(4, mp(INF, INF))));
  int dx[8] = {1, 0, -1, 0};
  int dy[8] = {0, 1, 0, -1};
  rep(i, 4) ans[Y1][x1][i] = mp(0, k);

  rep(i, 4) {
    Corr c = {x1, Y1, (int)i};
    q.push(c);
  }

  while (!q.empty()) {
    Corr now = q.front();
    q.pop();
    // cout<<now.y<<" "<<now.x<<endl;
    // rep(i,4)cout<<ans[now.y][now.x][i].fi<<" ";
    // cout<<endl;
    for (int i = 0; i < 4; i++) {
      if (now.muki == i) {
        // fin("A");
        int nextx = now.x + dx[i];
        int nexty = now.y + dy[i];

        // 次に探索する場所のX座標がはみ出した時
        if (nextx >= grid[0].size())
          continue;
        if (nextx < 0)
          continue;

        // 次に探索する場所のY座標がはみ出した時
        if (nexty >= grid.size())
          continue;
        if (nexty < 0)
          continue;
        if (grid[nexty][nextx] == '@')
          continue;
        // 次に探索する場所が既に探索済みの場合
        pair<int, int> p = ans[now.y][now.x][i];
        p.se++;
        if (p.se == k + 1) {
          p.se = 1;
          p.fi++;
        }
        if (ans[nexty][nextx][i] > p) {
          ans[nexty][nextx][i] = p;
          Corr next = {nextx, nexty, i};
          q.push(next);
        }
      } else {
        // fin("B");
        int nextx = now.x;
        int nexty = now.y;

        // 次に探索する場所のX座標がはみ出した時
        if (nextx >= grid[0].size())
          continue;
        if (nextx < 0)
          continue;

        // 次に探索する場所のY座標がはみ出した時
        if (nexty >= grid.size())
          continue;
        if (nexty < 0)
          continue;

        pair<int, int> p = ans[now.y][now.x][now.muki];
        p.fi++;
        p.se = 0;
        if (ans[nexty][nextx][i] > p) {
          ans[nexty][nextx][i] = p;
          Corr next = {nextx, nexty, i};
          q.push(next);
        }
      }
    }
    // cout<<now.x<<" " <<now.y<<endl;
    // rep(i,h){
    //     rep(j,w){
    //         if(ans[i][j][1].fi==INF)printf("  _");
    //         // else printf("%3d",ans[i][j][1].fi);
    //         else
    //         printf("%3d",min({ans[i][j][0],ans[i][j][1],ans[i][j][2],ans[i][j][3]}).fi);
    //     }
    //     cout<<endl;
    // }
    // fin('\\n');
    // rep(i,h){
    //     rep(j,w){
    //         if(ans[i][j][1].fi==INF)printf("  _");
    //         // else printf("%3d",ans[i][j][1].fi);
    //         else
    //         printf("%3d",min({ans[i][j][0],ans[i][j][1],ans[i][j][2],ans[i][j][3]}).se);
    //     }
    //     cout<<endl;
    // }
    // fin('\\n');
  }
  return min({ans[y2][x2][0], ans[y2][x2][1], ans[y2][x2][2], ans[y2][x2][3]})
      .fi;
}

int main() {

  cin >> h >> w >> k >> Y1 >> x1 >> y2 >> x2;
  vs v(h);
  rep(i, h) cin >> v[i];
  x1--;
  Y1--;
  x2--;
  y2--;

  int ans = bfs(v);
  if (ans == INF)
    fin(-1);
  else
    fin(ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define arep(i, v) for (auto i : v)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define fi first
#define se second
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline void dump(T &v) {
  irep(i, v) { cout << *i << ((i == --v.end()) ? '\\n' : ' '); }
}
inline string getline() {
  string s;
  getline(cin, s);
  return s;
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
struct io {
  io() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
};
const int INF = INT_MAX;
constexpr ll LLINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-11;

int h, w, k;
int x1, x2, Y1, y2;

// 各座標に格納したい情報を構造体にする
// 今回はX座標,Y座標,深さ(距離)を記述している
struct Corr {
  int x;
  int y;
  int depth;
  int rest;
  int muki;
};
int rmuki(int m) {
  if (m == 0)
    return 4;
  else if (m == 4)
    return 0;
  else if (m == 1)
    return 2;
  else
    return 1;
}
queue<Corr> q;
int bfs(vs &v, vector<vvi> &ans) {
  // このような記述をしておくと，この後のfor文が綺麗にかける
  int dx[8] = {1, 0, -1, 0};
  int dy[8] = {0, 1, 0, -1};
  while (!q.empty()) {
    Corr now = q.front();
    q.pop();
    if (now.x == x2 && now.y == y2) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int nextx = now.x + dx[i];
      int nexty = now.y + dy[i];
      int nextmuki = i;
      int nextrest;
      int nextdepth;
      if (now.rest == 0 && i == now.muki) {
        nextrest = k - 1;
        nextdepth = now.depth + 1;
      } else if (now.muki == nextmuki) {
        nextrest = now.rest - 1;
        nextdepth = now.depth;
      } else {
        nextrest = k - 1;
        nextdepth = now.depth + 1;
      }

      // 次に探索する場所のX座標がはみ出した時
      if (nextx >= (int)v[0].size())
        continue;
      if (nextx < 0)
        continue;

      // 次に探索する場所のY座標がはみ出した時
      if (nexty >= (int)v.size())
        continue;
      if (nexty < 0)
        continue;

      // 次に探索する場所が既に探索済みの場合
      if (v[nexty][nextx] == '@')
        continue;

      // cout<<now.x<<" "<<now.y<<" "<<nextx<<" "<<nexty<<" "<<nextmuki<<" ";
      // cout<<ans[nexty][nextx][nextmuki]<<" "<<nextdepth<<endl;
      if (ans[nexty][nextx][nextmuki] <= nextdepth) {
        continue;
      } else {
        ans[nexty][nextx][nextmuki] = nextdepth;
        chmin(ans[now.y][now.x][rmuki(nextmuki)], nextdepth + 1);
      }

      Corr next = {nextx, nexty, nextdepth, nextrest, nextmuki};
      q.push(next);
    }
    // cout<<now.x<<" " <<now.y<<endl;
    // rep(i,h){
    //     rep(j,w){
    //         int a=INF;
    //         rep(k,4){
    //             if(ans[i][j][k]!=-1)chmin(a,ans[i][j][k]);
    //         }
    //         if(a==INF)a=-1;
    //         // printf("%3d",a);
    //         if(ans[i][j][1]==INF)printf("  _");
    //         else printf("%3d",ans[i][j][1]);
    //     }
    //     cout<<endl;
    // }
    // fin('\\n');
  }
  return -1;
}

int main() {

  cin >> h >> w >> k >> Y1 >> x1 >> y2 >> x2;
  vs v(h);
  rep(i, h) cin >> v[i];
  x1--;
  Y1--;
  x2--;
  y2--;
  Corr c = {x1, Y1, 0, k, -1};
  q.push(c);
  vector<vvi> ans(h, vvi(w, vi(4, INF)));
  rep(i, 4) ans[Y1][x1][i] = 0;
  bfs(v, ans);
  int a = INF;
  rep(k, 4) {
    if (ans[y2][x2][k] != -1)
      chmin(a, ans[y2][x2][k]);
  }
  if (a == INF)
    a = -1;
  fin(a);
}