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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char mp[h + 2][w + 2];
  ll done[h + 2][w + 2];
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      if (i == 0 || j == 0 || i == h + 1 || j == w + 1) {
        mp[i][j] = '@';
        done[i][j] = 1;
      } else {
        cin >> mp[i][j];
        done[i][j] = INF;
      }
    }
  }

  priority_queue<pair<ll, tuple<int, int>>, vector<pair<ll, tuple<int, int>>>,
                 greater<pair<ll, tuple<int, int>>>>
      q;
  q.push(make_pair(0, make_tuple(x1, y1)));
  done[x1][y1] = 0;

  int dir_x[] = {1, 0, -1, 0};
  int dir_y[] = {0, 1, 0, -1};
  ll ans = -1;
  while (q.size() > 0) {
    auto tmp = q.top();
    q.pop();
    auto tpl = tmp.second;
    ll w = tmp.first;
    ll tx = get<0>(tpl);
    ll ty = get<1>(tpl);

    if (tx == x2 && ty == y2) {
      ans = w;
      break;
    }

    for (int i = 0; i < 4; i++) {
      ll nx = tx, ny = ty;
      ll cnt = 1;
      while (cnt <= k) {
        ny += dir_y[i];
        nx += dir_x[i];
        if (mp[nx][ny] == '@')
          break;
        if (done[nx][ny] <= w)
          break;
        if (done[nx][ny] > w + 1) {
          q.push(make_pair(w + 1, make_tuple(nx, ny)));
          done[nx][ny] = w + 1;
        }
        cnt++;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char mp[h + 2][w + 2];
  int done[h + 2][w + 2];
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      if (i == 0 || j == 0 || i == h + 1 || j == w + 1) {
        mp[i][j] = '@';
        done[i][j] = 1;
      } else {
        cin >> mp[i][j];
        done[i][j] = 0;
      }
    }
  }

  priority_queue<pair<ll, tuple<int, int, int>>,
                 vector<pair<ll, tuple<int, int, int>>>,
                 greater<pair<ll, tuple<int, int, int>>>>
      q;
  q.push(make_pair(0, make_tuple(x1, y1, -1)));
  done[x1][y1] = 1;

  int dir_x[] = {1, 0, -1, 0};
  int dir_y[] = {0, 1, 0, -1};
  ll ans = -1;
  while (q.size() > 0) {
    auto tmp = q.top();
    q.pop();
    auto tpl = tmp.second;
    ll w = tmp.first;
    ll tx = get<0>(tpl);
    ll ty = get<1>(tpl);
    ll cm = get<2>(tpl);

    if (tx == x2 && ty == y2) {
      ans = w;
      break;
    }

    for (int i = 0; i < 4; i++) {
      ll nx = tx, ny = ty;
      ll cnt = 1;
      if (i == cm)
        continue;
      int cm2 = -1;
      if (i <= 1)
        cm2 = i + 2;
      else
        cm2 = i - 2;
      while (cnt <= k && mp[nx + dir_x[i]][ny + dir_y[i]] == '.') {
        ny += dir_y[i];
        nx += dir_x[i];
        if (done[nx][ny] == 0) {
          q.push(make_pair(w + 1, make_tuple(nx, ny, cm2)));
          done[nx][ny] = 1;
        }
        cnt++;
      }
    }
  }

  cout << ans << endl;
  return 0;
}