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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 1000100
int H, W, K, sx, sy, ex, ey;
string s[MAX];

vector<vector<vector<ll>>> mini;

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

struct Data {
  int x, y, dir;
  ll cost;
  bool operator<(const Data &data) const {
    if (cost != data.cost)
      return cost > data.cost;
    return x < data.x;
  }
};

struct Data2 {
  int x, y;
};

int bf() {
  vector<vector<int>> mini2;
  rep(i, H) {
    vector<int> tmp(W, INT_MAX);
    mini2.pb(tmp);
  }
  deque<Data2> deq;
  deq.pb((Data2){sx, sy});
  mini2[sy][sx] = 0;
  while (!deq.empty()) {
    Data2 d = deq.front();
    deq.pop_front();
    rep(i, 4) {
      int nx = d.x + dx[i], ny = d.y + dy[i];
      int rem = K - 1;
      while ((0 <= nx && nx < W && 0 <= ny && ny < H) && s[ny][nx] != '@' &&
             rem >= 0) {
        if (!(mini2[ny][nx] == INT_MAX || mini2[ny][nx] == mini2[d.y][d.x] + 1))
          break;
        mini2[ny][nx] = mini2[d.y][d.x] + 1;
        deq.pb((Data2){nx, ny});
        nx = nx + dx[i];
        ny = ny + dy[i];
        --rem;
      }
    }
  }
  if (mini2[ey][ex] == INT_MAX)
    return -1;
  else
    return mini2[ey][ex];
}

ll get(ll v) {
  if (v % K == 0)
    return v;
  return ((ll)(v / K) + 1LL) * K;
}
/*
void print_mini() {
  rep(i,H) {
    rep(j,W) {
      if( mini[i][j] == LLONG_MAX ) {
        cout << string(5-1,' ') << "X";
      } else {
        cout << string(5-(int)to_string(mini[i][j]).size(),' ') << mini[i][j];
      }
    } puts("");
  }
}
*/
void solve() {
  mini.clear();
  mini.resize(H, vector<vector<ll>>(W, vector<ll>(4, LLONG_MAX)));
  priority_queue<Data> Q;
  // rep(i,4) Q.push((Data){sx,sy,i,1.0/(double)K});
  rep(i, 4) {
    mini[sy][sx][i] = 0;
    Q.push((Data){sx, sy, i, 0});
  }
  while (!Q.empty()) {
    Data d = Q.top();
    Q.pop();
    rep(i, 4) {
      int nx = d.x + dx[i], ny = d.y + dy[i];
      if (!(0 <= nx && nx < W && 0 <= ny && ny < H))
        continue;
      if ((i + 2) % 4 == d.dir)
        continue;
      if (s[ny][nx] == '@')
        continue;
      ll ncost = d.cost;
      if (d.dir == i)
        ++ncost;
      else
        ncost = get(ncost) + 1LL;
      if (ncost < mini[ny][nx][i]) {
        mini[ny][nx][i] = ncost;
        Q.push((Data){nx, ny, i, ncost});
      }
    }
  }
  // print_mini();
  ll ans = -1;
  // ll bf_ans = bf();
  ll tmp = LLONG_MAX;
  rep(i, 4) tmp = min(tmp, mini[ey][ex][i]);
  if (tmp != LLONG_MAX)
    ans = (ll)(get(tmp) / K);
  cout << ans << endl;
  /*
  if( ans != bf_ans ) {
    cout << "Answer: " << bf_ans << ", Your = " << ans << endl;
    cout << "WRONG ANSWER:" << endl;
    cout << H << " " << W << " " << K << endl;
    cout << sy + 1 << " " << sx + 1 << " " << ey + 1 << " " << ex + 1 << endl;
    rep(i,H) cout << s[i] << endl;
    assert( ans == bf_ans );
  }
  */
}

int main() {
  cin >> H >> W >> K;
  cin >> sy >> sx >> ey >> ex;
  --sy, --sx, --ey, --ex;
  rep(i, H) cin >> s[i];
  solve();
  /*
  const int sz = 4;
  rep(i,sz) s[i] = string(sz,'?');
  REP(k,1,5) {
    H = sz, W = sz;
    K = k;
    rep(bit,(1<<(sz*sz))) {
      rep(i,H) {
        rep(j,W) {
          s[i][j] = ( ((bit>>(j+i*W))&1) ? '.' : '@' );
        }
      }
      rep(SX,sz) {
        rep(SY,sz) {
          sx = SX, sy = SY;
          if( s[sy][sx] == '@' ) continue;
          rep(EX,sz) {
            rep(EY,sz) {
              ex = EX, ey = EY;
              if( s[ey][ex] == '@' ) continue;
              solve();
            }
          }
        }
      }
    }
  }
  */
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 1000100
int H, W, K, sx, sy, ex, ey;
string s[MAX];

vector<vector<int>> mini;

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

struct Data {
  int x, y;
};

void solve() {
  rep(i, H) {
    vector<int> tmp(W, INT_MAX);
    mini.pb(tmp);
  }
  deque<Data> deq;
  deq.pb((Data){sx, sy});
  mini[sy][sx] = 0;
  while (!deq.empty()) {
    Data d = deq.front();
    deq.pop_front();
    rep(i, 4) {
      int nx = d.x + dx[i], ny = d.y + dy[i];
      int rem = K - 1;
      while ((0 <= nx && nx < W && 0 <= ny && ny < H) && s[ny][nx] != '@' &&
             rem >= 0) {
        if (!(mini[ny][nx] == INT_MAX || mini[ny][nx] == mini[d.y][d.x] + 1))
          break;
        mini[ny][nx] = mini[d.y][d.x] + 1;
        deq.pb((Data){nx, ny});
        nx = nx + dx[i];
        ny = ny + dy[i];
        --rem;
      }
    }
    if (mini[ey][ex] != INT_MAX) {
      cout << mini[ey][ex] << endl;
      return;
    }
  }
  /*
  rep(i,H) {
    rep(j,W) {
      if( mini[i][j] == INT_MAX ) {
        cout << string(5-1,' ') << "X";
      } else {
        cout << string(5-(int)to_string(mini[i][j]).size(),' ') <<  mini[i][j];
      }
    }  puts("");
  }
  */
  if (mini[ey][ex] == INT_MAX)
    puts("-1");
  else
    cout << mini[ey][ex] << endl;
}

int main() {
  cin >> H >> W >> K;
  cin >> sy >> sx >> ey >> ex;
  --sy, --sx, --ey, --ex;
  rep(i, H) cin >> s[i];
  solve();
  return 0;
}