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

# 1 "f.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "f.cpp"
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1

# 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1
#include <bits/stdc++.h>
# 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1
#include <ext/pb_ds/assoc_container.hpp>
# 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1
#include <ext/pb_ds/tree_policy.hpp>
# 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2

using namespace std;
using namespace __gnu_pbds;

using ii = pair<long long, long long>;
using vi = vector<long long>;
using vd = vector<long double>;
using vb = vector<bool>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvd = vector<vd>;
using vvb = vector<vb>;
using vvii = vector<vii>;
# 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
inline long long mod(const long long n,
                     const long long m = ((long long)1e9 + 7)) {
  return (n % m + m) % m;
}

/**
 * Euclidean GCD.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long gcd(const long long a, const long long b) {
  if (a < 0 or b < 0)
    return gcd(abs(a), abs(b));
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

/**
 * Euclidean derivated LCM.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long lcm(const long long a, const long long b) {
  // same as a*b/gcd(a, b) but avoiding overflow.
  return a / gcd(a, b) * b;
}
# 2 "f.cpp" 2

int32_t main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL));
  long long n, m, k;
  cin >> n >> m >> k;

  long long x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vvi ans(n, vi(m, ((long long)1e9 + 1))), cnt(n, vi(m));
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < m; ++j) {
      char c;
      cin >> c;
      if (c == '@')
        ans[i][j] = -1;
    }
  }
  ans[x1][y1] = 0;
  cnt[x1][y1] = 1;
  queue<ii> q;
  q.push({x1, y1});
  while (!q.empty()) {
    long long x = q.front().first, y = q.front().second;
    q.pop();

    // right
    for (long long i = 1; i <= k and y + i < m and
                          ans[x][y + i] >= ans[x][y] and cnt[x][y + i] < 4;
         ++i) {
      cnt[x][y + i]++;
      ans[x][y + i] = min(ans[x][y + i], ans[x][y] + 1);
      q.push({x, y + i});
    }

    // left
    for (long long i = 1; i <= k and y - i >= 0 and
                          ans[x][y - i] >= ans[x][y] + 1 and cnt[x][y - i] < 4;
         ++i) {
      cnt[x][y - i]++;
      ans[x][y - i] = min(ans[x][y - i], ans[x][y] + 1);
      q.push({x, y - i});
    }

    // down
    for (long long i = 1; i <= k and x + i < n and
                          ans[x + i][y] >= ans[x][y] + 1 and cnt[x + i][y] < 4;
         ++i) {
      cnt[x + i][y]++;
      ans[x + i][y] = min(ans[x + i][y], ans[x][y] + 1);
      q.push({x + i, y});
    }

    // up
    for (long long i = 1; i <= k and x - i >= 0 and
                          ans[x - i][y] >= ans[x][y] + 1 and cnt[x - i][y] < 4;
         ++i) {
      cnt[x - i][y]++;
      ans[x - i][y] = min(ans[x - i][y], ans[x][y] + 1);
      q.push({x - i, y});
    }
  }
  cout << (ans[x2][y2] == ((long long)1e9 + 1) ? -1 : ans[x2][y2]) << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

# 1 "f.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "f.cpp"
# 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1

# 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1
#include <bits/stdc++.h>
# 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1
#include <ext/pb_ds/assoc_container.hpp>
# 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2
# 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1
#include <ext/pb_ds/tree_policy.hpp>
# 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2

using namespace std;
using namespace __gnu_pbds;

using ii = pair<long long, long long>;
using vi = vector<long long>;
using vd = vector<long double>;
using vb = vector<bool>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvd = vector<vd>;
using vvb = vector<vb>;
using vvii = vector<vii>;
# 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp"
inline long long mod(const long long n,
                     const long long m = ((long long)1e9 + 7)) {
  return (n % m + m) % m;
}

/**
 * Euclidean GCD.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long gcd(const long long a, const long long b) {
  if (a < 0 or b < 0)
    return gcd(abs(a), abs(b));
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

/**
 * Euclidean derivated LCM.
 *
 * Time Complexity: O(log(min(a, b))).
 * Space Complexity: O(log(min(a, b))).
 */
long long lcm(const long long a, const long long b) {
  // same as a*b/gcd(a, b) but avoiding overflow.
  return a / gcd(a, b) * b;
}
# 2 "f.cpp" 2

int32_t main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL));
  long long n, m, k;
  cin >> n >> m >> k;

  long long x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vvi ans(n, vi(m, ((long long)1e9 + 1))), cnt(n, vi(m));
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < m; ++j) {
      char c;
      cin >> c;
      if (c == '@')
        ans[i][j] = -1;
    }
  }
  ans[x1][y1] = 0;
  cnt[x1][y1] = 1;
  queue<ii> q;
  q.push({x1, y1});
  while (!q.empty()) {
    long long x = q.front().first, y = q.front().second;
    q.pop();

    // right
    for (long long i = 1; i <= k and y + i < m and ans[x][y + i] >= ans[x][y];
         ++i) {
      if (cnt[x][y + i] == 4)
        continue;
      cnt[x][y + i]++;
      ans[x][y + i] = min(ans[x][y + i], ans[x][y] + 1);
      q.push({x, y + i});
    }

    // left
    for (long long i = 1;
         i <= k and y - i >= 0 and ans[x][y - i] >= ans[x][y] + 1; ++i) {
      if (cnt[x][y - i] == 4)
        continue;
      cnt[x][y - i]++;
      ans[x][y - i] = min(ans[x][y - i], ans[x][y] + 1);
      q.push({x, y - i});
    }

    // down
    for (long long i = 1;
         i <= k and x + i < n and ans[x + i][y] >= ans[x][y] + 1; ++i) {
      if (cnt[x + i][y] == 4)
        continue;
      cnt[x + i][y]++;
      ans[x + i][y] = min(ans[x + i][y], ans[x][y] + 1);
      q.push({x + i, y});
    }

    // up
    for (long long i = 1;
         i <= k and x - i >= 0 and ans[x - i][y] >= ans[x][y] + 1; ++i) {
      if (cnt[x - i][y] == 4)
        continue;
      cnt[x - i][y]++;
      ans[x - i][y] = min(ans[x - i][y], ans[x][y] + 1);
      q.push({x - i, y});
    }
  }
  cout << (ans[x2][y2] == ((long long)1e9 + 1) ? -1 : ans[x2][y2]) << "\\n";
  return 0;
}