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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000LL
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

class Graph_class {
public:
  bool UDLR_and_corner = false;
  int N;
  int M;
  int K;
  int x1, x2, y1, y2;
  vector<vector<int>> xy;

  vector<vector<vector<pii>>> vdist;

  void read_edge();
  void adjini();
  void visited_ini();
  void Dijkstra(pii v_ini);
  void solve();
};

void Graph_class::read_edge() {
  cin >> N >> M >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  xy.resize(N);
  rep(i, N) {
    xy[i].resize(M);
    string S;
    cin >> S;
    rep(j, M) {
      if (S[j] == '.')
        xy[i][j] = 0;
      else
        xy[i][j] = 1;
    }
  }
  return;
}

void Graph_class::adjini() {
  vdist.resize(N);
  rep(i, N) {
    vdist[i].resize(M);
    rep(j, M) { vdist[i][j].resize(4, make_pair(InfL, InfL)); }
  }
  return;
}

void Graph_class::Dijkstra(pii v_ini) {
  priority_queue<tuple<int, int, int, int, int>,
                 vector<tuple<int, int, int, int, int>>,
                 greater<tuple<int, int, int, int, int>>>
      xdist_que;
  // 回数, 歩数, x, y, 向き
  vdist[v_ini.first][v_ini.second][0] = make_pair(0, 0);
  xdist_que.push(make_tuple(0, 0, v_ini.first, v_ini.second, 0));

  while (!xdist_que.empty()) {
    tuple<int, int, int, int, int> xdist_que_tmp = xdist_que.top();
    xdist_que.pop();
    int dist_tmp = get<0>(xdist_que_tmp);
    int k_tmp = get<1>(xdist_que_tmp);
    int i0 = get<2>(xdist_que_tmp);
    int j0 = get<3>(xdist_que_tmp);
    int d0 = get<4>(xdist_que_tmp);
    if ((ll)vdist[i0][j0][d0].first * InfL + (ll)vdist[i0][j0][d0].second <
        (ll)dist_tmp * InfL + (ll)k_tmp)
      continue;
    int dist_new, k_new, i1, j1, d1;
    rep(type, 2) {
      dist_new = dist_tmp;
      k_new = k_tmp;
      i1 = i0;
      j1 = j0;
      d1 = d0;
      switch (type) {
      case 0:
        // 直進
        switch (d0) {
        case 0:
          i1++;
          break;
        case 1:
          i1--;
          break;
        case 2:
          j1++;
          break;
        case 3:
          j1--;
          break;
        }
        if (i1 < 0 || i1 >= N || j1 < 0 || j1 >= M)
          continue;
        if (xy[i1][j1] == 1)
          continue;
        k_new++;
        if (k_new >= K) {
          k_new = 0;
          dist_new++;
        }
        break;
      case 1:
        // 回転
        d1++;
        d1 %= 4;
        if (k_new > 0) {
          k_new = 0;
          dist_new++;
        }
        break;
      }
      if ((ll)vdist[i1][j1][d1].first * InfL + (ll)vdist[i1][j1][d1].second >
          (ll)dist_new * InfL + (ll)k_new) {
        vdist[i1][j1][d1].first = dist_new;
        vdist[i1][j1][d1].second = k_new;
        xdist_que.push(make_tuple(dist_new, k_new, i1, j1, d1));
      }
    }
  }
  return;
}

void Graph_class::solve() {
  Dijkstra(make_pair(x1, y1));
  int ans = InfL;
  rep(d, 4) {
    int anstmp = vdist[x2][y2][d].first;
    if (vdist[x2][y2][d].second > 0)
      anstmp++;
    ans = min(ans, anstmp);
  }
  if (ans == InfL)
    ans = -1;
  cout << ans << endl;
  return;
}

int main() {
  Graph_class graph;
  graph.read_edge();
  graph.adjini();
  graph.solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

class Graph_class {
public:
  bool UDLR_and_corner = false;
  int N;
  int M;
  int K;
  int x1, x2, y1, y2;
  vector<vector<int>> xy;

  vector<vector<int>> DP;
  vector<vl> vdist;

  vector<vb> v_visited;
  deque<pii> v_que;

  void read_edge();
  void adjini();
  void visited_ini();
  void graph_BFS(pii BFS_ini);
  void graph_DFS(pii DFS_ini);
  void ReRooting(pii root);
  void solve();
};

void Graph_class::read_edge() {
  cin >> N >> M >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  xy.resize(N);
  rep(i, N) {
    xy[i].resize(M);
    string S;
    cin >> S;
    rep(j, M) {
      if (S[j] == '.')
        xy[i][j] = 0;
      else
        xy[i][j] = 1;
    }
  }
  return;
}

void Graph_class::adjini() {
  vdist.resize(N);
  DP.resize(N);
  v_visited.resize(N);
  rep(i, N) {
    vdist[i].resize(M, -1);
    DP[i].resize(M, 0);
    v_visited[i].resize(M, false);
  }
  visited_ini();
  return;
}

void Graph_class::visited_ini() {
  rep(i, N) {
    rep(j, M) { v_visited[i][j] = false; }
  }
  v_que.clear();
}

void Graph_class::graph_DFS(pii BFS_ini) {
  v_que.clear();
  v_que.push_back(BFS_ini);
  v_visited[BFS_ini.first][BFS_ini.second] = true;

  // option
  vdist[BFS_ini.first][BFS_ini.second] = 0;
  // cout << BFS_ini.first << " " << BFS_ini.second << endl;

  while (!v_que.empty()) {
    pii ij0 = v_que.back();
    v_que.pop_back();
    int i0 = ij0.first;
    int j0 = ij0.second;
    for (int idiff = -1; idiff <= 1; idiff++) {
      for (int jdiff = -1; jdiff <= 1; jdiff++) {
        if (UDLR_and_corner) {
          if ((idiff == 0) && (jdiff == 0)) // 8 (UDLR + corner)
            continue;
        } else {
          if ((idiff == 0) == (jdiff == 0)) // 4 (UDLR)
            continue;
        }
        for (int k = 1; k <= K; k++) {
          int i1 = i0 + idiff * k;
          int j1 = j0 + jdiff * k;
          if (i1 < 0 || i1 >= N || j1 < 0 || j1 >= M)
            break;
          pii ij1 = make_pair(i1, j1);
          if (xy[i1][j1] == 0 &&
              (vdist[i1][j1] == -1 || vdist[i1][j1] > vdist[i0][j0])) {
            v_visited[i1][j1] = true;
            v_que.push_back(ij1);

            // option
            vdist[i1][j1] = vdist[i0][j0] + 1;
            // cout << i1 << " " << j1 << endl;
          } else
            break;
        }
      }
    }
  }
  return;
}

void Graph_class::solve() {
  graph_DFS(make_pair(x1, y1));
  cout << vdist[x2][y2] << endl;
  return;
}

int main() {
  Graph_class graph;
  graph.read_edge();
  graph.adjini();
  graph.solve();
  return 0;
}