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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#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 M = 1e9 + 7;
const ll INF = 1e9 + 7;

ll h, w, k, sx, sy, gx, gy, s, g, ans;
ll a[1100000];
bool c[1100000];
bool done[1100000];
ll dst[1100000];
priority_queue<P, vector<P>, greater<P>> q;

ll bfs() {
  q.push(P(0, s));
  done[s] = true;
  dst[s] = 0;

  while (q.size()) {
    P p = q.top();
    q.pop();
    ll dist = p.first + 1;
    ll pos = p.second;
    ll px = pos / w;
    ll py = pos % w;

    for (ll i = px - 1; i >= max(0LL, px - k); i--) {
      ll ps = i * w + py;
      if (!c[ps])
        break;
      if (done[ps]) {
        if (dst[ps] == dist)
          continue;
        else
          break;
      }
      done[ps] = true;
      if (i == gx and py == gy) {
        cout << dist << endl;
        exit(0);
      }
      dst[ps] = dist;
      q.push(P(dist, ps));
    }
    for (ll i = px + 1; i < min(h, px + k + 1); i++) {
      ll ps = i * w + py;
      if (!c[ps])
        break;
      if (done[ps]) {
        if (dst[ps] == dist)
          continue;
        else
          break;
      }
      done[ps] = true;
      if (i == gx and py == gy) {
        cout << dist << endl;
        exit(0);
      }
      dst[ps] = dist;
      q.push(P(dist, ps));
    }
    for (ll i = py - 1; i >= max(0LL, py - k); i--) {
      ll ps = px * w + i;
      if (!c[ps])
        break;
      if (done[ps]) {
        if (dst[ps] == dist)
          continue;
        else
          break;
      }
      done[ps] = true;
      if (px == gx and i == gy) {
        cout << dist << endl;
        exit(0);
      }
      dst[ps] = dist;
      q.push(P(dist, ps));
    }
    for (ll i = py + 1; i < min(w, py + k + 1); i++) {
      ll ps = px * w + i;
      if (!c[ps])
        break;
      if (done[ps]) {
        if (dst[ps] == dist)
          continue;
        else
          break;
      }
      done[ps] = true;
      if (px == gx and i == gy) {
        cout << dist << endl;
        exit(0);
      }
      dst[ps] = dist;
      q.push(P(dist, ps));
    }
  }
  return -1;
}

int main() {
  scanf("%lld%lld%lld", &h, &w, &k);
  scanf("%lld%lld%lld%lld", &sx, &sy, &gx, &gy);
  sx--;
  sy--;
  gx--;
  gy--;
  s = sx * w + sy;
  g = gx * w + gy;

  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      char ch;
      scanf(" %c", &ch);
      if (ch == '.') {
        c[i * w + j] = true;
      }
    }
  }

  cout << bfs() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#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 M = 1e9 + 7;
const ll INF = 1e9 + 7;

ll h, w, k, sx, sy, gx, gy, s, g, ans;
ll a[1100000];
bool c[1100000];
bool done[1100000];
priority_queue<P, vector<P>, greater<P>> q;

ll bfs() {
  q.push(P(0, s));
  done[s] = true;

  while (q.size()) {
    P p = q.top();
    q.pop();
    ll dist = p.first + 1;
    ll pos = p.second;
    ll px = pos / w;
    ll py = pos % w;

    for (ll i = px - 1; i >= max(0LL, px - k); i--) {
      ll ps = i * w + py;
      if (!c[ps])
        break;
      if (done[ps])
        continue;
      done[ps] = true;
      if (i == gx and py == gy) {
        cout << dist << endl;
        exit(0);
      }
      q.push(P(dist, ps));
    }
    for (ll i = px + 1; i < min(h, px + k + 1); i++) {
      ll ps = i * w + py;
      if (!c[ps])
        break;
      if (done[ps])
        continue;
      done[ps] = true;
      if (i == gx and py == gy) {
        cout << dist << endl;
        exit(0);
      }
      q.push(P(dist, ps));
    }
    for (ll i = py - 1; i >= max(0LL, py - k); i--) {
      ll ps = px * w + i;
      if (!c[ps])
        break;
      if (done[ps])
        continue;
      done[ps] = true;
      if (px == gx and i == gy) {
        cout << dist << endl;
        exit(0);
      }
      q.push(P(dist, ps));
    }
    for (ll i = py + 1; i < min(w, py + k + 1); i++) {
      ll ps = px * w + i;
      if (!c[ps])
        break;
      if (done[ps])
        continue;
      done[ps] = true;
      if (px == gx and i == gy) {
        cout << dist << endl;
        exit(0);
      }
      q.push(P(dist, ps));
    }
  }
  return -1;
}

int main() {
  scanf("%lld%lld%lld", &h, &w, &k);
  scanf("%lld%lld%lld%lld", &sx, &sy, &gx, &gy);
  sx--;
  sy--;
  gx--;
  gy--;
  s = sx * w + sy;
  g = gx * w + gy;

  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      char ch;
      scanf(" %c", &ch);
      if (ch == '.') {
        c[i * w + j] = true;
      }
    }
  }

  cout << bfs() << endl;
  return 0;
}