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

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple

using P = pair<int, int>;

int solve(int H, int W, int K, P start, P goal, vector<string> cells) {
  vector<vector<int>> steps(H, vector<int>(W, 1e9));

  queue<P> q;
  q.push(start);
  steps[start.second][start.first] = 0;

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

  int cnt = 0;
  while (!q.empty()) {

    // REP(h, H) {
    //     REP(w, W) {
    //         if (cells[h][w] == '@') {
    //             cout << "@ ";
    //         }
    //         else if (steps[h][w] == 1e9) {
    //             cout << "X ";
    //         }
    //         else {
    //             cout << steps[h][w] << " ";
    //         }
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    P cur = q.front();
    q.pop();
    int cur_x = cur.first;
    int cur_y = cur.second;

    int cur_step = steps[cur_y][cur_x];
    if (cur == goal) {
      return cur_step;
    }

    // if (cnt % 1000 == 0) {
    //     cout << "q size " << SIZE(q) << ", step - " << cur_step << ", cnt = "
    //     << cnt << endl;
    // }
    cnt++;

    REP(d, 4) {
      FOR(k, 1, K + 1) {
        int next_x = cur_x + dx[d] * k;
        int next_y = cur_y + dy[d] * k;
        if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H) {
          break;
        }
        if (cells[next_y][next_x] == '@')
          break;

        int next_step = steps[next_y][next_x];

        // 改善の見込みがないなら終了
        if (next_step <= cur_step)
          break;

        // さらにその次のセルで改善する可能性がある
        if (next_step <= cur_step + 1)
          continue;

        // cout << "next_step: " << next_step << " -> " << cur_step + 1 << endl;

        steps[next_y][next_x] = cur_step + 1;
        q.push(mp(next_x, next_y));
      }
    }
  }

  return -1;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // while(1){
  //     int H = 1000;
  //     int W = 1000;
  //     int K = rand() % 1000 + 1;
  //     P start, goal;
  //     start.first = rand() % W;
  //     start.second = rand() % W;
  //     goal.first = rand() % W;
  //     goal.second = rand() % W;
  //     vector<string> cells(H);
  //     REP(h, H) {
  //         string s;
  //         REP(w, W) {
  //             s += (rand() % 3 == 1) ? "@" : ".";
  //         }
  //         cells[h] = s;
  //     }
  //     cout << solve(H, W, K, start, goal, cells) << endl;

  //}

  int H, W, K;
  cin >> H >> W >> K;
  P start, goal;
  cin >> start.second >> start.first >> goal.second >> goal.first;
  --start.first;
  --start.second;
  --goal.first;
  --goal.second;

  vector<string> cells(H);
  REP(h, H) cin >> cells[h];

  cout << solve(H, W, K, start, goal, cells) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

using P = pair<int, int>;

int solve(int H, int W, int K, P start, P goal, vector<string> cells) {
  vector<vector<int>> steps(H, vector<int>(W, 1e9));

  queue<P> q;
  q.push(start);
  steps[start.second][start.first] = 0;

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

  int cnt = 0;
  while (!q.empty()) {

    // REP(h, H) {
    //     REP(w, W) {
    //         if (cells[h][w] == '@') {
    //             cout << "@ ";
    //         }
    //         else if (steps[h][w] == 1e9) {
    //             cout << "X ";
    //         }
    //         else {
    //             cout << steps[h][w] << " ";
    //         }
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    P cur = q.front();
    q.pop();
    int cur_x = cur.first;
    int cur_y = cur.second;

    int cur_step = steps[cur_y][cur_x];
    if (cur == goal) {
      return cur_step;
    }

    // if (cnt % 1000 == 0) {
    //     cout << "q size " << SIZE(q) << ", step - " << cur_step << ", cnt = "
    //     << cnt << endl;
    // }
    cnt++;

    REP(d, 4) {
      FOR(k, 1, K + 1) {
        int next_x = cur_x + dx[d] * k;
        int next_y = cur_y + dy[d] * k;
        if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H) {
          break;
        }
        if (cells[next_y][next_x] == '@')
          break;

        int next_step = steps[next_y][next_x];

        if (next_step <= cur_step + 1) {
          continue;
        }
        // cout << "next_step: " << next_step << " -> " << cur_step + 1 << endl;

        steps[next_y][next_x] = cur_step + 1;
        q.push(mp(next_x, next_y));
      }
    }
  }

  return -1;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // while(1){
  //     int H = 1000;
  //     int W = 1000;
  //     int K = rand() % 1000 + 1;
  //     P start, goal;
  //     start.first = rand() % W;
  //     start.second = rand() % W;
  //     goal.first = rand() % W;
  //     goal.second = rand() % W;
  //     vector<string> cells(H);
  //     REP(h, H) {
  //         string s;
  //         REP(w, W) {
  //             s += (rand() % 3 == 1) ? "@" : ".";
  //         }
  //         cells[h] = s;
  //     }
  //     cout << solve(H, W, K, start, goal, cells) << endl;

  //}

  int H, W, K;
  cin >> H >> W >> K;
  P start, goal;
  cin >> start.second >> start.first >> goal.second >> goal.first;
  --start.first;
  --start.second;
  --goal.first;
  --goal.second;

  vector<string> cells(H);
  REP(h, H) cin >> cells[h];

  cout << solve(H, W, K, start, goal, cells) << endl;

  return 0;
}