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

#include <bits/stdc++.h>
using namespace std;

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvs vector<vs>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define F first
#define S second
#define mp make_pair

#define dist(a, b, p, q) sqrt((p - a) * (p - a) + (q - b) * (q - b))

#define pp(n) printf("%.10Lf", n);
#define line cout << "\\n";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

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

const ld pie = 3.1415926535897932384626;
const ll mod = 1e9 + 7;

/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor
/// ( (a-1)/b ) .

vvc v;

int n, m, max_dis;
int sx, sy, ex, ey;

void read() {
  cin >> n >> m >> max_dis;
  cin >> sx >> sy >> ex >> ey;

  v = vvc(n + 2, vc(m + 2, '@'));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> v[i][j];
  }
}

vvi dis;
const int inf = 1e6 + 10;

void bfs() {
  queue<pii> q;

  dis = vvi(n + 2, vi(m + 2, inf));

  dis[sx][sy] = 0;
  q.push({sx, sy});

  while (q.empty() == false) {
    auto node = q.front();
    q.pop();

    int x = node.F, y = node.S;

    for (int k = 0; k < 4; k++) {
      for (int times = 1; times <= max_dis; times++) {
        int xx = x + dx[k] * times;
        int yy = y + dy[k] * times;

        //                cout << "xx : " << xx << " and yy : " << yy ; line ;

        if (v[xx][yy] != '.')
          break;
        if (dis[xx][yy] <= dis[x][y])
          break;

        //                cout << "hello" ; line ;

        if (dis[x][y] + 1 < dis[xx][yy]) {
          dis[xx][yy] = 1 + dis[x][y];
          q.push({xx, yy});
        }
      }
    }
  }
}

void solve(int test_case) {
  read();
  bfs();

  int ans = dis[ex][ey];

  if (ans >= inf)
    ans = -1;

  cout << ans;
  line;
}

int main() {
  fast int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvs vector<vs>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define F first
#define S second
#define mp make_pair

#define dist(a, b, p, q) sqrt((p - a) * (p - a) + (q - b) * (q - b))

#define pp(n) printf("%.10Lf", n);
#define line cout << "\\n";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

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

const ld pie = 3.1415926535897932384626;
const ll mod = 1e9 + 7;

/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor
/// ( (a-1)/b ) .

vvc v;
int n, m, max_dis;

int sx, sy, ex, ey;

void read() {
  cin >> n >> m >> max_dis;
  cin >> sx >> sy >> ex >> ey;

  v = vvc(n + 2, vc(m + 2, '@'));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> v[i][j];
  }
}

vector<vector<array<int, 4>>> dis;
const int inf = 1e6 + 10;

void bfs(int x, int y) {
  queue<array<int, 3>> q;

  for (int k = 0; k < 4; k++) {
    dis[x][y][k] = 0;
    q.push({x, y, k});
  }

  while (q.empty() == false) {
    auto node = q.front();
    q.pop();

    int x = node[0], y = node[1], parent_k = node[2];

    for (int k = 0; k < 4; k++) {
      for (int times = 1; times <= max_dis; times++) {
        int xx = x + dx[k] * times;
        int yy = y + dy[k] * times;

        if (v[xx][yy] == '@')
          break;
        if (dis[xx][yy][k] <= dis[x][y][parent_k])
          break;

        dis[xx][yy][k] = 1 + dis[x][y][parent_k];
        q.push({xx, yy, k});
      }
    }
  }
}

void solve(int test_case) {
  read();

  dis = vector<vector<array<int, 4>>>(n + 2, vector<array<int, 4>>(m + 2));

  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++)
      for (int k = 0; k < 4; k++)
        dis[i][j][k] = inf;
  }

  bfs(sx, sy);

  int x = ex, y = ey;
  int ans = inf;

  for (int k = 0; k < 4; k++) {
    ans = min(ans, dis[x][y][k]);
  }

  if (ans >= inf)
    ans = -1;

  cout << ans;
  line;
}

int main() {
  fast int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }

  return 0;
}