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

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

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (int i = s; i <= int(e); ++i)
#define DEC(i, s, e) for (int i = s; i >= int(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
typedef pair<pi, int> pii;

int H, W, K, x3, x2, y3, y2;

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

inline int readInt() {
  int x = 0;
  char ch = getchar_unlocked();
  while (ch < '0' || ch > '9')
    ch = getchar_unlocked();
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar_unlocked();
  }
  return x;
}

inline char readChar() {
  int x = 0;
  char ch = getchar_unlocked();
  while (ch != '.' && ch != '@')
    ch = getchar_unlocked();
  return ch;
}
int main() {
  fast;

  H = readInt();
  W = readInt();
  K = readInt();
  x3 = readInt();
  y3 = readInt();
  x2 = readInt();
  y2 = readInt();

  char grid[H + 1][W + 1];
  int dist[H + 1][W + 1];
  mem(dist, -1);
  FOR(i, 1, H) FOR(j, 1, W) grid[i][j] = readChar();

  queue<pi> q;

  q.push(pi(x3, y3));
  dist[x3][y3] = 0;
  while (!q.empty()) {
    pi cur = q.front();
    q.pop();

    // if (dist[cur.f][cur.s] != -1) continue;

    FOR(i, 0, 3) {
      int nx = cur.f, ny = cur.s;
      FOR(j, 1, K) {
        nx += dx[i];
        ny += dy[i];

        if (nx < 1 || ny < 1 || nx > H || ny > W)
          break;
        if ((dist[nx][ny] != -1 && dist[nx][ny] < dist[cur.f][cur.s] + 1) ||
            grid[nx][ny] == '@')
          break;

        if (dist[nx][ny] != dist[cur.f][cur.s] + 1)
          q.push(pi(nx, ny));
        dist[nx][ny] = dist[cur.f][cur.s] + 1;
      }
    }
  }
  cout << dist[x2][y2];
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (int i = s; i <= int(e); ++i)
#define DEC(i, s, e) for (int i = s; i >= int(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
typedef pair<pi, int> pii;

int H, W, K, x3, x2, y3, y2;

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

inline int readInt() {
  int x = 0;
  char ch = getchar_unlocked();
  while (ch < '0' || ch > '9')
    ch = getchar_unlocked();
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar_unlocked();
  }
  return x;
}

inline char readChar() {
  int x = 0;
  char ch = getchar_unlocked();
  while (ch != '.' && ch != '@')
    ch = getchar_unlocked();
  return ch;
}
int main() {
  fast;

  H = readInt();
  W = readInt();
  K = readInt();
  x3 = readInt();
  y3 = readInt();
  x2 = readInt();
  y2 = readInt();

  char grid[H + 1][W + 1];
  int dist[H + 1][W + 1];
  mem(dist, -1);
  FOR(i, 1, H) FOR(j, 1, W) grid[i][j] = readChar();

  queue<pi> q;

  q.push(pi(x3, y3));
  dist[x3][y3] = 0;
  while (!q.empty()) {
    pi cur = q.front();
    q.pop();

    // if (dist[cur.f][cur.s] != -1) continue;

    FOR(i, 0, 3) {
      int nx = cur.f, ny = cur.s;
      FOR(j, 1, K) {
        nx += dx[i];
        ny += dy[i];

        if (nx < 1 || ny < 1 || nx > H || ny > W)
          break;
        if ((dist[nx][ny] != -1 && dist[nx][ny] < dist[cur.f][cur.s] + 1) ||
            grid[nx][ny] == '@')
          break;
        dist[nx][ny] = dist[cur.f][cur.s] + 1;
        q.push(pi(nx, ny));
      }
    }
  }
  cout << dist[x2][y2];
}