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

#include <bits/stdc++.h>
#define endl '\\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define zy -2147382637
#define bql 2147483647
#define ll long long
#define ull unsigned long long
#define ld long double
#define il inline
#define sz(x) x.size()
#define maxn 100010
#define rp(i, l, r) for (int i = l; i <= r; i++)
#define rb(i, r, l) for (int i = r; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll lowbit(ll x) { return x & (-x); }
ll prime(ll x) {
  if (x <= 1)
    return false;
  for (int i = 2; i <= int(sqrt(x)); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) {
  ll r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
ll powmod(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
inline int readint() {
  char c = getchar();
  int w = 1;
  int x = 0;
  while (!isdigit(c))
    (c == '-') && (w = -w), c = getchar();
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  x *= w;
  return x;
}
inline ll readll() {
  char c = getchar();
  ll w = 1;
  ll x = 0;
  while (!isdigit(c))
    (c == '-') && (w = -w), c = getchar();
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  x *= w;
  return x;
}
void fl(string name) {
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
}
const int dx[8] = {-1, 1, 0, 0, -1, 1, -1, 1},
          dy[8] = {0, 0, -1, 1, -1, -1, 1, 1};
struct node {
  int x, y;
};
int h, w, k, xx1, yy1, x2, y2;
bool ok(int x, int y) {
  if (x <= h && x >= 1 && y <= w && y >= 1)
    return true;
  else
    return false;
}
queue<node> Q;
int main() {
  cin >> h >> w >> k;
  char g[h + 1][w + 1];
  int vis[h + 1][w + 1], step[h + 1][w + 1];
  memset(vis, 0, sizeof(vis));
  memset(step, 0, sizeof(step));
  cin >> xx1 >> yy1 >> x2 >> y2;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> g[i][j];
    }
  }
  node a, b;
  a.x = xx1, a.y = yy1;
  step[xx1][yy1] = 0;
  vis[xx1][yy1] = 1;
  Q.push(a);
  int ans = bql;
  while (!Q.empty()) {
    a = Q.front();
    Q.pop();
    if (a.x == x2 && a.y == y2) {
      ans = min(ans, step[x2][y2]);
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        b.x = dx[i] * j + a.x;
        b.y = dy[i] * j + a.y;
        if (ok(b.x, b.y) == 0 ||
            (step[b.x][b.y] <= step[a.x][a.y] && step[b.x][b.y]))
          break;
        else {
          if (g[b.x][b.y] == '@')
            break;
          if (g[b.x][b.y] == '.' && !vis[b.x][b.y]) {
            vis[b.x][b.y] = 1;
            step[b.x][b.y] = step[a.x][a.y] + 1;
            Q.push(b);
          }
        }
      }
    }
  }
  if (ans == bql)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
/* stuff to remember
 * int overflow, array bounds
 * special cases (n=1? n=0?)
 * do something instead of nothing and stay organized
 * USE STATIC ARRAYS
 * DEFINING ARRAYS BEFORE main()
 * DO NOT DEFINE ARRAYS IN main()
 * USE INT INSTEAD OF LL,NOTICE THE MLE
 */

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define zy -2147382637
#define bql 2147483647
#define ll long long
#define ull unsigned long long
#define ld long double
#define il inline
#define sz(x) x.size()
#define maxn 100010
#define rp(i, l, r) for (int i = l; i <= r; i++)
#define rb(i, r, l) for (int i = r; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll lowbit(ll x) { return x & (-x); }
ll prime(ll x) {
  if (x <= 1)
    return false;
  for (int i = 2; i <= int(sqrt(x)); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) {
  ll r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
ll powmod(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
inline int readint() {
  char c = getchar();
  int w = 1;
  int x = 0;
  while (!isdigit(c))
    (c == '-') && (w = -w), c = getchar();
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  x *= w;
  return x;
}
inline ll readll() {
  char c = getchar();
  ll w = 1;
  ll x = 0;
  while (!isdigit(c))
    (c == '-') && (w = -w), c = getchar();
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  x *= w;
  return x;
}
void fl(string name) {
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
}
const int dx[8] = {-1, 1, 0, 0, -1, 1, -1, 1},
          dy[8] = {0, 0, -1, 1, -1, -1, 1, 1};
struct node {
  int x, y;
};
int h, w, k, xx1, yy1, x2, y2;
bool ok(int x, int y) {
  if (x <= h && x >= 1 && y <= w && y >= 1)
    return true;
  else
    return false;
}
queue<node> Q;
int main() {
  cin >> h >> w >> k;
  char g[h + 1][w + 1];
  int vis[h + 1][w + 1], step[h + 1][w + 1];
  memset(vis, 0, sizeof(vis));
  memset(step, 0, sizeof(step));
  cin >> xx1 >> yy1 >> x2 >> y2;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> g[i][j];
    }
  }
  node a, b;
  a.x = xx1, a.y = yy1;
  step[xx1][yy1] = 0;
  vis[xx1][yy1] = 1;
  Q.push(a);
  int ans = bql;
  while (!Q.empty()) {
    a = Q.front();
    Q.pop();
    if (a.x == x2 && a.y == y2) {
      ans = min(ans, step[x2][y2]);
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        b.x = dx[i] * j + a.x;
        b.y = dy[i] * j + a.y;
        if (ok(b.x, b.y) ==
            0 /*||(step[b.x][b.y]<=step[a.x][a.y]&&step[b.x][b.y])*/)
          break;
        else {
          if (g[b.x][b.y] == '@')
            break;
          if (g[b.x][b.y] == '.' && !vis[b.x][b.y]) {
            vis[b.x][b.y] = 1;
            step[b.x][b.y] = step[a.x][a.y] + 1;
            Q.push(b);
          }
        }
      }
    }
  }
  if (ans == bql)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
/* stuff to remember
 * int overflow, array bounds
 * special cases (n=1? n=0?)
 * do something instead of nothing and stay organized
 * USE STATIC ARRAYS
 * DEFINING ARRAYS BEFORE main()
 * DO NOT DEFINE ARRAYS IN main()
 * USE INT INSTEAD OF LL,NOTICE THE MLE
 */