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

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), x.end()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \\
  for (int _i = 0; _i < (n); _i++)                                             \\
  ni(a[_i])
#define nal(a, n)                                                              \\
  for (int _i = 0; _i < (n); _i++)                                             \\
  nl(a[_i])
#define pri(n) printf("%d\\n", (n))
#define prl(n) printf("%lld\\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
vector<vector<int>> ar;
vector<vector<array<int, 4>>> dp;
char t[N];

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

int ok(int x, int y) {
  return 0 <= x && x < sz(ar) && 0 <= y && y < sz(ar[0]) && ar[x][y] == 0;
}

int main() {
  int h, w, k;
  scanf("%d %d %d", &h, &w, &k);
  int x1, x2, y1, y2;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  ar.resize(h);
  dp.resize(h);
  for (int i = 0; i < h; i++) {
    scanf("%s", t);
    ar[i].resize(w);
    dp[i].resize(w);
    for (int j = 0; j < w; j++) {
      ar[i][j] = t[j] == '@' ? 1 : 0;
      for (int l = 0; l < 4; l++)
        dp[i][j][l] = inf;
    }
  }
  deque<pii> pq;
  for (int i = 0; i < 4; i++)
    dp[x1][y1][i] = 0;
  pq.pb(mp(x1, y1));
  while (sz(pq)) {
    int x, y;
    tie(x, y) = pq.front();
    pq.pop_front();
    int tmp = inf;
    for (int i = 0; i < 4; i++)
      tmp = min(tmp, dp[x][y][i]);
    for (int i = 0; i < 4; i++)
      dp[x][y][i] = tmp;
    // cout << x << " " << y << endl;
    assert(tmp != inf);
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      for (int j = 1; j <= k && ok(nx, ny) && dp[nx][ny][i] >= tmp + 1; j++) {

        if (dp[nx][ny][i] > tmp + 1) {
          dp[nx][ny][i] = tmp + 1;
          pq.pb(mp(nx, ny));
        }
        // cout << i << " - " << j << endl;
        nx += dx[i], ny += dy[i];
      }
    }
  }
  int ans = inf;
  for (int l = 0; l < 4; l++)
    ans = min(ans, dp[x2][y2][l]);
  if (ans == inf)
    ans = -1;
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), x.end()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \\
  for (int _i = 0; _i < (n); _i++)                                             \\
  ni(a[_i])
#define nal(a, n)                                                              \\
  for (int _i = 0; _i < (n); _i++)                                             \\
  nl(a[_i])
#define pri(n) printf("%d\\n", (n))
#define prl(n) printf("%lld\\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
vector<vector<int>> ar;
vector<vector<array<int, 4>>> dp;
char t[N];

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

int ok(int x, int y) {
  return 0 <= x && x < sz(ar) && 0 <= y && y < sz(ar[0]) && ar[x][y] == 0;
}

int main() {
  int h, w, k;
  scanf("%d %d %d", &h, &w, &k);
  int x1, x2, y1, y2;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  ar.resize(h);
  dp.resize(h);
  for (int i = 0; i < h; i++) {
    scanf("%s", t);
    ar[i].resize(w);
    dp[i].resize(w);
    for (int j = 0; j < w; j++) {
      ar[i][j] = t[j] == '@' ? 1 : 0;
      for (int l = 0; l < 4; l++)
        dp[i][j][l] = inf;
    }
  }
  deque<pii> pq;
  for (int i = 0; i < 4; i++)
    dp[x1][y1][i] = 0;
  pq.pb(mp(x1, y1));
  while (sz(pq)) {
    int x, y;
    tie(x, y) = pq.front();
    pq.pop_front();
    int tmp = inf;
    for (int i = 0; i < 4; i++)
      tmp = min(tmp, dp[x][y][i]);
    for (int i = 0; i < 4; i++)
      dp[x][y][i] = tmp;
    // cout << x << " " << y << endl;
    assert(tmp != inf);
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      for (int j = 1; j <= k && ok(nx, ny) && dp[nx][ny][i] >= tmp + 1; j++) {
        dp[nx][ny][i] = tmp + 1;
        pq.pb(mp(nx, ny));
        // cout << i << " - " << j << endl;
        nx += dx[i], ny += dy[i];
      }
    }
  }
  int ans = inf;
  for (int l = 0; l < 4; l++)
    ans = min(ans, dp[x2][y2][l]);
  if (ans == inf)
    ans = -1;
  printf("%d\\n", ans);
  return 0;
}