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

// skip until line 65
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define BR "\\n"
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define ALL(cont) begin(cont), end(cont)
#define AS_MOD(a, b) ((((a) % (b)) + (b)) % (b))
#define MO(a) AS_MOD(a, mod)
#define FEACH(it, cont) for (auto(it) = begin(cont); it != end(cont); ++it)
#define FEACHR(it, cont) for (auto(it) = rbegin(cont); it != rend(cont); ++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll()                                                                \\
  ([]() {                                                                      \\
    ll s;                                                                      \\
    scanf("%lld", &s);                                                         \\
    return s;                                                                  \\
  })()
#define getld()                                                                \\
  ([]() {                                                                      \\
    ld s;                                                                      \\
    scanf("%Lf", &s);                                                          \\
    return s;                                                                  \\
  })()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define MAX(V, T) V = max(V, T)
#define MIN(V, T) V = min(V, T)

#define DumpArray(cont, from, to)                                              \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i] << ")";                            \\
  }                                                                            \\
  cout << endl;
#define DumpPairs(cont, from, to)                                              \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i].fi << "," << cont[__i].se << ")";  \\
  }                                                                            \\
  cout << endl;
#define PrintArray(cont, from, to)                                             \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i] << ")";                            \\
  }                                                                            \\
  cout << endl;
#define PrintPairs(cont, from, to)                                             \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i].fi << "," << cont[__i].se << ")";  \\
  }                                                                            \\
  cout << endl;
#define Print(v) cout << v << endl;
#define PDBG() cout << "Line:" << __LINE__ << endl;
#define PrintArray2D(cont, fh, th, fw, tw)                                     \\
  for (int __i = fh; __i < th; ++__i) {                                        \\
    for (int __j = fw; __j < tw; ++__j) {                                      \\
      cout << cont[__i][__j] << " ";                                           \\
    }                                                                          \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define Resize2D(cont, H, W, V)                                                \\
  cont.resize(H);                                                              \\
  for (int i = 0; i < H; ++i)                                                  \\
    cont[i].resize(W, V);
#define Resize3D(cont, H, W, D, V)                                             \\
  cont.resize(H);                                                              \\
  for (int i = 0; i < H; ++i)                                                  \\
    cont[i].resize(W);                                                         \\
  for (int i = 0; i < H; ++i)                                                  \\
    for (int j = 0; j < W; ++j)                                                \\
      cont[i][j].resize(D, V);
/*
 * ***_bound(6)
 * 5 6 6 6 6 6 7 7 7
 *   ^lower    ^upper
 * void reverse(c.begin(), c.end())
 *
 * graph
 * seg tree
 * dp
 * brute force
 */
struct Comparator {
  int operator()(int l, int r) const { return r < l; }
};
struct Vec;
// set<int, Comparator> S;

// solution below
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
ll H, W, K;
P beg, en;
vector<vector<bool>> C;
vector<vector<ll>> Dist;
vector<vector<bool>> Visited;
vector<vector<vector<ll>>> Rem;
ll INF = 1000 * 1000 + 10;
int main() {
  cout.precision(15);
  cout << fixed;
  cin >> H >> W >> K;
  Resize2D(C, H, W, false);
  Resize2D(Dist, H, W, INF);
  Resize2D(Visited, H, W, false) cin >> beg.fi >> beg.se >> en.fi >> en.se;
  beg.fi--;
  beg.se--;
  en.fi--;
  en.se--;
  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) {
      if (s[j] == '.')
        C[i][j] = true;
    }
  }
  queue<P> Q;
  REP(i, 4) {
    Dist[beg.fi][beg.se] = 0;
    Q.push(P(beg.fi, beg.se));
  }
  // cout<<"HHello"<<endl;
  while (!Q.empty()) {
    // cout<<"H"<<Q.size()<<endl;
    // cout<<"T"<<endl;
    P o = Q.front();
    // cout<< o.fi<<","<<o.se<<" "<<p.se;
    Q.pop();
    ll bd = Dist[o.fi][o.se];
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    for (int j = 0; j < 4; ++j) {
      P cp = o;
      for (int i = 0; i < K; ++i) {
        cp.fi += dx[j];
        cp.se += dy[j];
        if (cp.fi < 0 || cp.fi >= H || cp.se < 0 || cp.se >= W)
          break;
        if (!C[cp.fi][cp.se])
          break;
        if (Visited[cp.fi][cp.se]) {
          if (Dist[cp.fi][cp.se] < bd + 1)
            break;
          continue;
        }
        // if (Dist[cp.fi][cp.se][j]<=bd+1)break;
        Q.push(cp);
        Visited[cp.fi][cp.se] = true;
        Dist[cp.fi][cp.se] = bd + 1;
      }
    }
  }

  ll ans = INF;
  ans = Dist[en.fi][en.se];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}


Error: TLE

Fixed submission:

// skip until line 65
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define BR "\\n"
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define ALL(cont) begin(cont), end(cont)
#define AS_MOD(a, b) ((((a) % (b)) + (b)) % (b))
#define MO(a) AS_MOD(a, mod)
#define FEACH(it, cont) for (auto(it) = begin(cont); it != end(cont); ++it)
#define FEACHR(it, cont) for (auto(it) = rbegin(cont); it != rend(cont); ++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll()                                                                \\
  ([]() {                                                                      \\
    ll s;                                                                      \\
    scanf("%lld", &s);                                                         \\
    return s;                                                                  \\
  })()
#define getld()                                                                \\
  ([]() {                                                                      \\
    ld s;                                                                      \\
    scanf("%Lf", &s);                                                          \\
    return s;                                                                  \\
  })()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define MAX(V, T) V = max(V, T)
#define MIN(V, T) V = min(V, T)

#define DumpArray(cont, from, to)                                              \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i] << ")";                            \\
  }                                                                            \\
  cout << endl;
#define DumpPairs(cont, from, to)                                              \\
  for (int __i = from; __i < to; ++__i) {                                      \\
    cout << "(" << __i << ": " << cont[__i].fi << "," << cont[__i].se << ")";  \\
  }                                                                            \\
  cout << endl;
/*
 * ***_bound(6)
 * 5 6 6 6 6 6 7 7 7
 *   ^lower    ^upper
 * void reverse(c.begin(), c.end())
 *
 * graph
 * seg tree
 * dp
 * brute force
 */
struct Comparator {
  int operator()(int l, int r) const { return r < l; }
};
struct Vec;
// set<int, Comparator> S;

// solution below
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
ll H, W, K;
P beg, en;
vector<vector<bool>> C;
vector<vector<vector<ll>>> Dist;
vector<vector<vector<bool>>> Visited;
vector<vector<vector<ll>>> Rem;
ll INF = 1000 * 1000 + 10;
int main() {
  cout.precision(15);
  cout << fixed;
  cin >> H >> W >> K;
  C.resize(H);
  Dist.resize(H);
  Visited.resize(H);
  Rem.resize(H);
  REP(i, H) {
    C[i].resize(W, false);
    Dist[i].resize(W);
    Visited[i].resize(W);
    Rem[i].resize(W);
    REP(j, W) {
      Dist[i][j].resize(4, INF);
      Visited[i][j].resize(4, false);
      Rem[i][j].resize(4);
    }
  }
  cin >> beg.fi >> beg.se >> en.fi >> en.se;
  beg.fi--;
  beg.se--;
  en.fi--;
  en.se--;
  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) {
      if (s[j] == '.')
        C[i][j] = true;
    }
  }
  queue<PP> Q;
  REP(i, 4) {
    Dist[beg.fi][beg.se][i] = 0;
    Rem[beg.fi][beg.se][i] = 0;
    Visited[beg.fi][beg.se][i] = true;
    Q.push(PP(P(beg.fi, beg.se), i));
  }
  // cout<<"HHello"<<endl;
  while (!Q.empty()) {
    // cout<<"H"<<Q.size()<<endl;
    // cout<<"T"<<endl;
    PP p = Q.front();
    P o = p.fi;
    // cout<< o.fi<<","<<o.se<<" "<<p.se;
    Q.pop();
    ll bd = INF;
    REP(i, 4)
    bd = min(bd, Dist[o.fi][o.se][i]);
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    for (int j = 0; j < 4; ++j) {
      P cp = o;
      for (int i = 0; i < K; ++i) {
        cp.fi += dx[j];
        cp.se += dy[j];
        if (cp.fi < 0 || cp.fi >= H || cp.se < 0 || cp.se >= W)
          break;
        if (!C[cp.fi][cp.se])
          break;
        if (Visited[cp.fi][cp.se][j])
          continue;
        // if (Dist[cp.fi][cp.se][j]<=bd+1)break;
        Q.push({cp, j});
        Visited[cp.fi][cp.se][j] = true;
        Dist[cp.fi][cp.se][j] = bd + 1;
      }
    }
  }

  ll ans = INF;
  REP(i, 4)
  ans = min(ans, Dist[en.fi][en.se][i]);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}