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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
vector<bool> gra[maxn];
vector<int> eg;
vector<vector<int>> dis;
queue<pair<int, int>> que;
int n, m, k, x1, y1, x2, y2;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  eg.resize(m, INF);
  dis.resize(n, eg);
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    for (int j = 0; j < m; j++)
      gra[i].emplace_back(ss[j] == '@');
  }
  dis[x2][y2] = 0;
  que.emplace(x2, y2);
  while (que.size()) {
    pair<int, int> cur = que.front();
    que.pop();
    int x = cur.first, y = cur.second, now = dis[x][y];
    if (now > dis[x1][y1])
      continue;
    for (int i = 0; i < 4; i++)
      for (int d = 1; d <= k; d++) {
        int p = x + d * dx[i], q = y + d * dy[i];
        if (p < 0 || p >= n || q < 0 || q >= m || gra[p][q])
          break;
        if (now + 1 < dis[p][q]) {
          dis[p][q] = now + 1;
          que.emplace(p, q);
        } else if (now + 1 > dis[p][q])
          break;
      }
  }
  cout << (dis[x1][y1] == INF ? -1 : dis[x1][y1]) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
vector<bool> gra[maxn];
vector<int> eg;
vector<vector<int>> dis;
queue<pair<int, int>> que;
int n, m, k, x1, y1, x2, y2;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  eg.resize(m, INF);
  dis.resize(n, eg);
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    for (int j = 0; j < m; j++)
      gra[i].emplace_back(ss[j] == '@');
  }
  dis[x2][y2] = 0;
  que.emplace(x2, y2);
  while (que.size()) {
    pair<int, int> cur = que.front();
    que.pop();
    int x = cur.first, y = cur.second, now = dis[x][y];
    if (now > dis[x1][y1])
      continue;
    for (int d = 1; d <= k; d++) {
      int p = x, q = y + d;
      if (q >= m || gra[p][q])
        break;
      if (now + 1 < dis[p][q]) {
        dis[p][q] = now + 1;
        que.emplace(p, q);
      }
    }
    for (int d = 1; d <= k; d++) {
      int p = x, q = y - d;
      if (q < 0 || gra[p][q])
        break;
      if (now + 1 < dis[p][q]) {
        dis[p][q] = now + 1;
        que.emplace(p, q);
      }
    }
    for (int d = 1; d <= k; d++) {
      int p = x + d, q = y;
      if (p >= n || gra[p][q])
        break;
      if (now + 1 < dis[p][q]) {
        dis[p][q] = now + 1;
        que.emplace(p, q);
      }
    }
    for (int d = 1; d <= k; d++) {
      int p = x - d, q = y;
      if (p < 0 || gra[p][q])
        break;
      if (now + 1 < dis[p][q]) {
        dis[p][q] = now + 1;
        que.emplace(p, q);
      }
    }
  }
  cout << (dis[x1][y1] == INF ? -1 : dis[x1][y1]) << '\\n';
  return 0;
}