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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll h, w, k, sx, sy, ex, ey;
  cin >> h >> w >> k >> sx >> sy >> ex >> ey;

  g = vector<string>(h);
  used = vector<vector<bool>>(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
    cin >> g[i];
  }
  sx--;
  sy--;
  ex--;
  ey--;
  P s = P(sx, sy);
  vector<vector<ll>> dist(h, vector<ll>(w, mod));
  dist[sx][sy] = 0;
  queue<P> q;
  q.push(s);

  while (q.size() > 0) {
    P cur = q.front();
    q.pop();
    if (used[cur.first][cur.second]) {
      continue;
    }
    used[cur.first][cur.second] = 1;
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        ll nx = cur.first + dx[i] * j;
        ll ny = cur.second + dy[i] * j;
        if (nx >= 0 && ny >= 0 && nx < h && ny < w) {
          if (used[nx][ny]) {
            break;
          }
          if (g[nx][ny] == '@') {
            break;
          }
          if (dist[nx][ny] <= dist[cur.first][cur.second]) {
            break;
          }
          dist[nx][ny] = dist[cur.first][cur.second] + 1;
          q.push(P(nx, ny));
        }
      }
    }
    if (dist[ex][ey] != mod) {
      cout << dist[ex][ey] << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll h, w, k, sx, sy, ex, ey;
  cin >> h >> w >> k >> sx >> sy >> ex >> ey;

  g = vector<string>(h);
  used = vector<vector<bool>>(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
    cin >> g[i];
  }
  sx--;
  sy--;
  ex--;
  ey--;
  P s = P(sx, sy);
  vector<vector<ll>> dist(h, vector<ll>(w, mod));
  dist[sx][sy] = 0;
  queue<P> q;
  q.push(s);

  while (q.size() > 0) {
    P cur = q.front();
    q.pop();
    if (used[cur.first][cur.second]) {
      continue;
    }
    used[cur.first][cur.second] = 1;
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        ll nx = cur.first + dx[i] * j;
        ll ny = cur.second + dy[i] * j;
        if (nx >= 0 && ny >= 0 && nx < h && ny < w && !used[nx][ny]) {
          if (g[nx][ny] == '@') {
            break;
          }
          if (dist[nx][ny] <= dist[cur.first][cur.second]) {
            break;
          }
          dist[nx][ny] = dist[cur.first][cur.second] + 1;
          q.push(P(nx, ny));
        }
      }
    }
    if (dist[ex][ey] != mod) {
      cout << dist[ex][ey] << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}