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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

typedef pair<ll, ll> p;
typedef pair<ll, p> pt;

ll INF = 1000000000000000000;
int main(int argc, char const *argv[]) {
  ll h, w, k;
  std::cin >> h >> w >> k;
  p s(0, 0), e(0, 0);
  scanf("%lld %lld", &s.first, &s.second);
  scanf("%lld %lld", &e.first, &e.second);
  s.first--, s.second--;
  e.first--, e.second--;

  vvl cost = VV(h, w, INF, ll);
  vector<string> v(h);
  re(i, h) std::cin >> v[i];

  priority_queue<pt, vector<pt>, greater<pt>> q;
  q.push(pt(0, s));
  cost[s.first][s.second] = 0;

  while (!q.empty()) {
    pt tmp = q.top();
    q.pop();
    ll c = tmp.first;
    ll x = tmp.second.first;
    ll y = tmp.second.second;

    if (cost[x][y] < c)
      continue;
    // @ かすでに訪れたマスがあるまで
    // ->
    for (int i = 1; i <= k; i++) {
      if (y + i >= w)
        break;
      if (cost[x][y + i] <= c || v[x][y + i] == '@')
        break;
      if (cost[x][y + i] != c + 1)
        q.push(pt(c + 1, p(x, y + i)));
      cost[x][y + i] = c + 1;
    }

    for (int i = -1; i >= -k; i--) {
      if (y + i < 0)
        break;
      if (cost[x][y + i] <= c || v[x][y + i] == '@')
        break;
      if (cost[x][y + i] != c + 1)
        q.push(pt(c + 1, p(x, y + i)));
      cost[x][y + i] = c + 1;
    }

    for (int i = 1; i <= k; i++) {
      if (x + i >= h)
        break;
      if (cost[x + i][y] <= c || v[x + i][y] == '@')
        break;
      if (cost[x + i][y] != c + 1)
        q.push(pt(c + 1, p(x + i, y)));
      cost[x + i][y] = c + 1;
    }

    for (int i = -1; i >= -k; i--) {
      if (x + i < 0)
        break;
      if (cost[x + i][y] <= c || v[x + i][y] == '@')
        break;
      if (cost[x + i][y] != c + 1)
        q.push(pt(c + 1, p(x + i, y)));
      cost[x + i][y] = c + 1;
    }
  }

  if (cost[e.first][e.second] == INF) {
    std::cout << -1 << '\\n';
    return 0;
  }
  std::cout << cost[e.first][e.second] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

typedef pair<ll, ll> p;
typedef pair<ll, p> pt;

ll INF = 1000000000000000000;
int main(int argc, char const *argv[]) {
  ll h, w, k;
  std::cin >> h >> w >> k;
  p s(0, 0), e(0, 0);
  scanf("%lld %lld", &s.first, &s.second);
  scanf("%lld %lld", &e.first, &e.second);
  s.first--, s.second--;
  e.first--, e.second--;

  vvl cost = VV(h, w, INF, ll);
  vector<string> v(h);
  re(i, h) std::cin >> v[i];

  priority_queue<pt, vector<pt>, greater<pt>> q;
  q.push(pt(0, s));
  while (!q.empty()) {
    pt tmp = q.top();
    q.pop();
    ll c = tmp.first, x = tmp.second.first, y = tmp.second.second;
    if (c >= cost[x][y])
      continue;
    cost[x][y] = c;

    // @ かすでに訪れたマスがあるまで
    // ->
    for (int i = 1; i <= k; i++) {
      if (y + i >= w)
        break;
      if (cost[x][y + i] != INF || v[x][y + i] == '@')
        break;
      q.push(pt(c + 1, p(x, y + i)));
    }

    for (int i = -1; i >= -k; i--) {
      if (y + i < 0)
        break;
      if (cost[x][y + i] != INF || v[x][y + i] == '@')
        break;
      q.push(pt(c + 1, p(x, y + i)));
    }

    for (int i = 1; i <= k; i++) {
      if (x + i >= h)
        break;
      if (cost[x + i][y] != INF || v[x + i][y] == '@')
        break;
      q.push(pt(c + 1, p(x + i, y)));
    }

    for (int i = -1; i >= -k; i--) {
      if (x + i < 0)
        break;
      if (cost[x + i][y] != INF || v[x + i][y] == '@')
        break;
      q.push(pt(c + 1, p(x + i, y)));
    }
  }
  if (cost[e.first][e.second] == INF) {
    std::cout << -1 << '\\n';
    return 0;
  }
  std::cout << cost[e.first][e.second] << '\\n';
  return 0;
}