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

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

typedef long long int ll;
#define endl "\\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILEIO                                                                 \\
  freopen("./input.txt", "r", stdin);                                          \\
  freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \\
       blockTime.second;                                                       \\
       debug("%s : %lld ms\\n ", d,                                             \\
             chrono::duration_cast<chrono::milliseconds>(                      \\
                 chrono::high_resolution_clock::now() - blockTime.first)       \\
                 .count()),                                                    \\
            blockTime.second = false)
const int M = 1e9 + 7;

template <class T> T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) {
  return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2);
}

int n, m, k, x, y, x2, y2, ans = INT_MAX;
vector<vector<char>> a;
vector<vector<float>> cost;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int bfs() {
  queue<pii> q;
  q.push({x, y});
  int r, c;
  cost[x][y] = 0;
  while (!q.empty()) {
    r = q.front().first, c = q.front().second;
    q.pop();
    if (r == x2 && c == y2)
      return cost[r][c];
    forn(dir, 0, 4) {
      forn(i, 1, k + 1) {
        int nr = r + dx[dir] * i;
        int nc = c + dy[dir] * i;
        if (nr < 0 || nc < 0 || nr >= n || nc >= m)
          continue;
        if (a[nr][nc] != '.' || cost[nr][nc] <= cost[r][c])
          break;
        if (cost[nr][nc] > cost[r][c] + 1) {
          cost[nr][nc] = cost[r][c] + 1;
          q.push({nr, nc});
        }
      }
    }
  }
  return -1;
}

int main() {
  FASTIO
#ifdef LOCAL
  FILEIO
#endif
  cin >> n >> m >> k;
  cin >> x >> y >> x2 >> y2;
  --x, --y, --x2, --y2;
  // cout << x << y << x2 << y2 << endl;
  a.resize(n);
  cost.resize(n);
  char foo;
  forn(i, 0, n) {
    forn(j, 0, m) {
      cin >> foo;
      a[i].pb(foo);
      cost[i].pb(INT_MAX);
    }
  }
  cout << bfs();
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
#define endl "\\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILEIO                                                                 \\
  freopen("./input.txt", "r", stdin);                                          \\
  freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \\
       blockTime.second;                                                       \\
       debug("%s : %lld ms\\n ", d,                                             \\
             chrono::duration_cast<chrono::milliseconds>(                      \\
                 chrono::high_resolution_clock::now() - blockTime.first)       \\
                 .count()),                                                    \\
            blockTime.second = false)
const int M = 1e9 + 7;

template <class T> T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) {
  return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2);
}

int n, m, k, x, y, x2, y2, ans = INT_MAX;
vector<vector<char>> a;
vector<vector<float>> cost;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int bfs() {
  queue<pii> q;
  q.push({x, y});
  int r, c;
  cost[x][y] = 0;
  while (!q.empty()) {
    r = q.front().first, c = q.front().second;
    q.pop();
    if (r == x2 && c == y2)
      return cost[r][c];
    forn(dir, 0, 4) {
      forn(i, 1, k + 1) {
        int nr = r + dx[dir] * i;
        int nc = c + dy[dir] * i;
        if (nr < 0 || nc < 0 || nr >= n || nc >= m)
          continue;
        if (a[nr][nc] != '.' || cost[nr][nc] <= cost[r][c])
          break;
        if (cost[nr][nc] > cost[r][c] + 1)
          cost[nr][nc] = cost[r][c] + 1;
        q.push({nr, nc});
      }
    }
  }
  return -1;
}

int main() {
  FASTIO
#ifdef LOCAL
  FILEIO
#endif
  cin >> n >> m >> k;
  cin >> x >> y >> x2 >> y2;
  --x, --y, --x2, --y2;
  // cout << x << y << x2 << y2 << endl;
  a.resize(n);
  cost.resize(n);
  char foo;
  forn(i, 0, n) {
    forn(j, 0, m) {
      cin >> foo;
      a[i].pb(foo);
      cost[i].pb(INT_MAX);
    }
  }
  cout << bfs();
}