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

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

#define ll long long
#define d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

int h, w, k;
int sx, sy, ex, ey;

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

int main() {
  fast();
  cin >> h >> w >> k;
  char a[h][w];

  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  rep(i, 0, h) { rep(j, 0, w) cin >> a[i][j]; }

  int dis[h][w];

  rep(i, 0, h) {
    rep(j, 0, w) { dis[i][j] = inf; }
  }

  dis[sx][sy] = 0;

  bool visited[h][w];
  clr(visited);

  queue<pair<int, int>> q;

  q.push({sx, sy});

  while (!q.empty()) {
    int x = q.front().F;
    int y = q.front().S;

    if (x == ex && y == ey) {
      cout << dis[ex][ey];
      return 0;
    }

    q.pop();
    if (visited[x][y])
      continue;

    visited[x][y] = true;
    rep(i, 0, 4) {
      if (i == 0) {
        rep(j, 0, k) {
          if (x + dx[i] < 0 || y + dy[i] + j < 0 || x + dx[i] >= h ||
              y + dy[i] + j >= w || a[x + dx[i]][y + dy[i] + j] == '@')
            break;

          if (dis[x][y] + 1 > dis[x + dx[i]][y + dy[i] + j])
            break;
          if (dis[x][y] + 1 < dis[x + dx[i]][y + dy[i] + j]) {
            dis[x + dx[i]][y + dy[i] + j] = dis[x][y] + 1;
            q.push({x + dx[i], y + dy[i] + j});
          }
        }
      }
      if (i == 1) {
        rep(j, 0, k) {
          if (x + dx[i] + j < 0 || y + dy[i] < 0 || x + dx[i] + j >= h ||
              y + dy[i] >= w || a[x + dx[i] + j][y + dy[i]] == '@')
            break;
          if (dis[x][y] + 1 > dis[x + dx[i] + j][y + dy[i]])
            break;
          if (dis[x][y] + 1 < dis[x + dx[i] + j][y + dy[i]]) {
            dis[x + dx[i] + j][y + dy[i]] = dis[x][y] + 1;
            q.push({x + dx[i] + j, y + dy[i]});
          }
        }
      }
      if (i == 2) {
        rep(j, 0, k) {
          if (x + dx[i] < 0 || y + dy[i] - j < 0 || x + dx[i] >= h ||
              y + dy[i] - j >= w || a[x + dx[i]][y + dy[i] - j] == '@')
            break;

          if (dis[x][y] + 1 > dis[x + dx[i]][y + dy[i] - j])
            break;
          if (dis[x][y] + 1 < dis[x + dx[i]][y + dy[i] - j]) {
            dis[x + dx[i]][y + dy[i] - j] = dis[x][y] + 1;
            q.push({x + dx[i], y + dy[i] - j});
          }
        }
      }
      if (i == 3) {
        rep(j, 0, k) {
          if (x + dx[i] - j < 0 || y + dy[i] < 0 || x + dx[i] - j >= h ||
              y + dy[i] >= w || a[x + dx[i] - j][y + dy[i]] == '@')
            break;
          if (dis[x][y] + 1 > dis[x + dx[i] - j][y + dy[i]])
            break;
          if (dis[x][y] + 1 < dis[x + dx[i] - j][y + dy[i]]) {
            dis[x + dx[i] - j][y + dy[i]] = dis[x][y] + 1;
            q.push({x + dx[i] - j, y + dy[i]});
          }
        }
      }
    }
  }

  cout << "-1";

  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

int h, w, k;
int sx, sy, ex, ey;

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

int main() {
  cin >> h >> w >> k;
  char a[h][w];

  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  rep(i, 0, h) { rep(j, 0, w) cin >> a[i][j]; }

  int dis[h][w];

  rep(i, 0, h) {
    rep(j, 0, w) { dis[i][j] = inf; }
  }

  dis[sx][sy] = 0;

  queue<pair<int, int>> q;

  q.push({sx, sy});

  while (!q.empty()) {
    int x = q.front().F;
    int y = q.front().S;

    if (x == ex && y == ey) {
      cout << dis[ex][ey];
      return 0;
    }

    q.pop();

    rep(i, 0, 4) {
      if (i == 0) {
        rep(j, 0, k) {
          if (x + dx[i] < 0 || y + dy[i] + j < 0 || x + dx[i] >= h ||
              y + dy[i] + j >= w || a[x + dx[i]][y + dy[i] + j] == '@')
            break;

          if (dis[x][y] + 1 < dis[x + dx[i]][y + dy[i] + j]) {
            dis[x + dx[i]][y + dy[i] + j] = dis[x][y] + 1;
            q.push({x + dx[i], y + dy[i] + j});
          }
        }
      }
      if (i == 1) {
        rep(j, 0, k) {
          if (x + dx[i] + j < 0 || y + dy[i] < 0 || x + dx[i] + j >= h ||
              y + dy[i] >= w || a[x + dx[i] + j][y + dy[i]] == '@')
            break;

          if (dis[x][y] + 1 < dis[x + dx[i] + j][y + dy[i]]) {
            dis[x + dx[i] + j][y + dy[i]] = dis[x][y] + 1;
            q.push({x + dx[i] + j, y + dy[i]});
          }
        }
      }
      if (i == 2) {
        rep(j, 0, k) {
          if (x + dx[i] < 0 || y + dy[i] - j < 0 || x + dx[i] >= h ||
              y + dy[i] - j >= w || a[x + dx[i]][y + dy[i] - j] == '@')
            break;

          if (dis[x][y] + 1 < dis[x + dx[i]][y + dy[i] - j]) {
            dis[x + dx[i]][y + dy[i] - j] = dis[x][y] + 1;
            q.push({x + dx[i], y + dy[i] - j});
          }
        }
      }
      if (i == 3) {
        rep(j, 0, k) {
          if (x + dx[i] - j < 0 || y + dy[i] < 0 || x + dx[i] - j >= h ||
              y + dy[i] >= w || a[x + dx[i] - j][y + dy[i]] == '@')
            break;

          if (dis[x][y] + 1 < dis[x + dx[i] - j][y + dy[i]]) {
            dis[x + dx[i] - j][y + dy[i]] = dis[x][y] + 1;
            q.push({x + dx[i] - j, y + dy[i]});
          }
        }
      }
    }
  }

  cout << "-1";

  return 0;
}