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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
long double eps = 1e-9;
long double pi = acos(-1);

ll mod = 1e9 + 7;
template <typename T> void add(T &a, T b) { a = (a + b) % mod; }
template <typename T> void sub(T &a, T b) { a = (a + mod - b) % mod; }

class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> parent;

  UnionFind(int N) { parent = vector<int>(N, -1); }

  int root(int A) {
    if (parent[A] < 0)
      return A;
    return parent[A] = root(parent[A]);
  }

  int size(int A) { return -parent[root(A)]; }

  bool unite(int A, int B) {
    A = root(A), B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    parent[A] += parent[B];
    parent[B] = A;
    return true;
  }

  bool same(int A, int B) { return root(A) == root(B); }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int h, w, a;
  cin >> h >> w >> a;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  s[sx][sy] = 'S';
  s[gx][gy] = 'G';
  UnionFind uni(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '@')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x >= 0 && y >= 0 && x < h && y < w) {
          if (s[x][y] == '@')
            continue;
          uni.unite(i * w + j, x * w + y);
        }
      }
    }
  }
  if (uni.same(sx * w + sy, gx * w + gy) == false) {
    cout << -1 << endl;
    return 0;
  }
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  queue<array<int, 5>> q;
  q.push({0, sx, sy, -1, 0});
  while (q.size()) {
    array<int, 5> p = q.front();
    q.pop();
    int x = p[1], y = p[2], dist = p[0];
    int r = p[3], ok = (p[4] == a);
    if (d[x][y] < dist)
      continue;
    for (int i = 0; i < 4; i++) {
      if (i + r == 3)
        continue;
      if (i == r && !ok)
        continue;
      for (int k = 1; k <= a; k++) {
        int nx = x + k * dx[i];
        int ny = y + k * dy[i];
        if (nx >= h || ny >= w)
          break;
        if (nx >= 0 && ny >= 0) {
          if (s[nx][ny] == '@')
            break;
          if (d[nx][ny] <= dist + 1)
            continue;
          chmin(d[nx][ny], dist + 1);
          if (s[nx][ny] == 'G') {
            cout << dist + 1 << endl;
            return 0;
          }
          q.push({dist + 1, nx, ny, i, k});
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
long double eps = 1e-9;
long double pi = acos(-1);

ll mod = 1e9 + 7;
template <typename T> void add(T &a, T b) { a = (a + b) % mod; }
template <typename T> void sub(T &a, T b) { a = (a + mod - b) % mod; }

class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> parent;

  UnionFind(int N) { parent = vector<int>(N, -1); }

  int root(int A) {
    if (parent[A] < 0)
      return A;
    return parent[A] = root(parent[A]);
  }

  int size(int A) { return -parent[root(A)]; }

  bool unite(int A, int B) {
    A = root(A), B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    parent[A] += parent[B];
    parent[B] = A;
    return true;
  }

  bool same(int A, int B) { return root(A) == root(B); }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int h, w, a;
  cin >> h >> w >> a;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  s[sx][sy] = 'S';
  s[gx][gy] = 'G';
  UnionFind uni(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '@')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x >= 0 && y >= 0 && x < h && y < w) {
          if (s[x][y] == '@')
            continue;
          uni.unite(i * w + j, x * w + y);
        }
      }
    }
  }
  if (uni.same(sx * w + sy, gx * w + gy) == false) {
    cout << -1 << endl;
    return 0;
  }
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  queue<array<int, 5>> q;
  q.push({0, sx, sy, -1, 0});
  while (q.size()) {
    array<int, 5> p = q.front();
    q.pop();
    int x = p[1], y = p[2], dist = p[0];
    int r = p[3], ok = (p[4] == a);
    if (d[x][y] < dist)
      continue;
    for (int i = 0; i < 4; i++) {
      if (i + r == 3)
        continue;
      if (i == r && !ok)
        continue;
      for (int k = 1; k <= a; k++) {
        int nx = x + k * dx[i];
        int ny = y + k * dy[i];
        if (nx >= h || ny >= w)
          break;
        if (nx >= 0 && ny >= 0) {
          if (s[nx][ny] == '@')
            break;
          if (d[nx][ny] <= dist)
            continue;
          chmin(d[nx][ny], dist + 1);
          if (s[nx][ny] == 'G') {
            cout << dist + 1 << endl;
            return 0;
          }
          q.push({dist + 1, nx, ny, i, k});
        }
      }
    }
  }
}