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

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

#define name ""
#define ini                                                                    \\
  freopen(name ".inp", "r", stdin);                                            \\
  freopen(name ".out", "w", stdout)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class T> inline void chmin(T &a, const T &val) {
  if (a > val)
    a = val;
}
template <class T> inline void chmax(T &a, const T &val) {
  if (a < val)
    a = val;
}
template <class T> long sqr(T a) { return 1ll * a * a; }
template <class T> void compress(T &a) {
  sort(all(a));
  a.resize(unique(all(a)) - a.begin());
}

const db pi = acos(-1.00);
const db eps = 1e-6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 1e6 + 1;
int n, m, k;
vector<char> c[N];

struct Data {
  pii cur;
  int u, v, dir;
  Data(pii cur, int u, int v, int dir) : cur(cur), u(u), v(v), dir(dir) {}
};

void bfs(int U, int V, int X, int Y) {
  deque<Data> Q;
  vector<vector<int>> dis[4];
  for (int i = 0; i < 4; i++) {
    dis[i].assign(n + 1, vector<int>(m + 1, INF));
  }
  for (int i = 0; i < 4; i++)
    dis[i][U][V] = 0;
  Q.push_back(Data(pii(0, 0), U, V, -1));

  while (Q.size()) {
    Data orz = Q.front();
    Q.pop_front();
    int u = orz.u;
    int v = orz.v;
    int dir = orz.dir;
    pii cur = orz.cur;
    for (int i = 0; i < 4; i++) {
      int x = u + dx[i];
      int y = v + dy[i];
      if (x < 1 || x > n || y < 1 || y > m || c[x][y] == '@')
        continue;
      if (i == dir) {
        if (cur.second == k) {
          if (dis[i][x][y] > cur.first + 1) {
            dis[i][x][y] = cur.first + 1;
            Q.push_back(Data(pii(dis[i][x][y], 1), x, y, i));
          }
        } else {
          if (dis[i][x][y] > cur.first) {
            dis[i][x][y] = cur.first;
            Q.push_front(Data(pii(dis[i][x][y], cur.second + 1), x, y, i));
          }
        }
      } else {
        if (dis[i][x][y] > cur.first + 1) {
          dis[i][x][y] = cur.first + 1;
          Q.push_back(Data(pii(dis[i][x][y], 1), x, y, i));
        }
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 4; i++) {
    chmin(res, dis[i][X][Y]);
  }
  if (res == INF)
    cout << -1;
  else
    cout << res;
}

int main() {
  fastio;
  cin >> n >> m >> k;
  int u, v, x, y;
  cin >> u >> v >> x >> y;
  for (int i = 1; i <= n; i++)
    c[i].resize(m + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  bfs(u, v, x, y);
  return 0;
}


Error: TLE

Fixed submission:

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

#define name ""
#define ini                                                                    \\
  freopen(name ".inp", "r", stdin);                                            \\
  freopen(name ".out", "w", stdout)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class T> inline void chmin(T &a, const T &val) {
  if (a > val)
    a = val;
}
template <class T> inline void chmax(T &a, const T &val) {
  if (a < val)
    a = val;
}
template <class T> long sqr(T a) { return 1ll * a * a; }
template <class T> void compress(T &a) {
  sort(all(a));
  a.resize(unique(all(a)) - a.begin());
}

#define Data pair<pii, pii>
const db pi = acos(-1.00);
const db eps = 1e-6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 1e6 + 1;
int n, m, k;
vector<char> c[N];

void bfs(int U, int V, int X, int Y) {
  priority_queue<Data, vector<Data>, greater<Data>> Pr;
  vector<vector<int>> dis[4];
  for (int i = 0; i < 4; i++) {
    dis[i].assign(n + 1, vector<int>(m + 1, INF));
  }
  for (int i = 0; i < 4; i++) {
    dis[i][U][V] = 0;
    Pr.push(Data(pii(0, i), pii(U, V)));
  }
  while (Pr.size()) {
    int u = Pr.top().second.first;
    int v = Pr.top().second.second;
    int cur = Pr.top().first.first;
    int dir = Pr.top().first.second;
    Pr.pop();
    if (dis[dir][u][v] != cur)
      continue;
    for (int i = 0; i < 4; i++) {
      int x = u;
      int y = v;
      for (int j = 1; j <= k; j++) {
        x += dx[i];
        y += dy[i];
        if (x < 1 || x > n || y < 1 || y > m)
          break;
        if (c[x][y] == '@')
          break;
        if (dis[i][x][y] > cur + 1) {
          dis[i][x][y] = cur + 1;
          Pr.push(Data(pii(cur + 1, i), pii(x, y)));
        }
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 4; i++) {
    chmin(res, dis[i][X][Y]);
  }
  if (res == INF)
    cout << -1;
  else
    cout << res;
}

int main() {
  fastio;
  cin >> n >> m >> k;
  int u, v, x, y;
  cin >> u >> v >> x >> y;
  for (int i = 1; i <= n; i++)
    c[i].resize(m + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  bfs(u, v, x, y);
  return 0;
}