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

#include <bits/stdc++.h>
using namespace std;

long long H, W, K;
long long xstart, ystart, xend, yend;
string ban[1000005];

map<pair<long long, long long>, long long> ans;

bool valid(long long x, long long y) {
  if (x < 0 || x >= H || y < 0 || y >= W) {
    return false;
  }

  return ban[x][y] == '.';
}

int main() {
  cin >> H >> W >> K;
  cin >> xstart >> ystart >> xend >> yend;
  xstart--;
  ystart--;
  xend--;
  yend--;

  for (long long i = 0; i < H; i++) {
    cin >> ban[i];
  }

  // cost, (x, y)
  // priority_queue< pair<long long, pair<long long, long long> >,
  //	vector<pair<long long, pair<long long, long long> > >,
  //	greater<pair<long long, pair<long long, long long> > > > Q;
  queue<pair<long long, pair<long long, long long>>> Q;

  Q.push(make_pair(0, make_pair(xstart, ystart)));

  while (!Q.empty()) {
    long long cost = Q.front().first;
    long long x = Q.front().second.first;
    long long y = Q.front().second.second;

    if (x == xend && y == yend) {
      cout << cost << endl;
      return 0;
    }

    Q.pop();

    // 下
    for (long long i = 1; i <= K; i++) {
      if (!valid(x + i, y)) {
        break;
      }
      if (ans.count(make_pair(x + i, y)) == 0) {
        ans[make_pair(x + i, y)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x + i, y)));
      } else if (ans[make_pair(x + i, y)] <= cost) {
        break;
      }
    }
    // 上
    for (long long i = 1; i <= K; i++) {
      if (!valid(x - i, y)) {
        break;
      }
      if (ans.count(make_pair(x - i, y)) == 0) {
        ans[make_pair(x - i, y)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x - i, y)));
      } else if (ans[make_pair(x - i, y)] <= cost) {
        break;
      }
    }

    // 右
    for (long long i = 1; i <= K; i++) {
      if (!valid(x, y + i)) {
        break;
      }
      if (ans.count(make_pair(x, y + i)) == 0) {
        ans[make_pair(x, y + i)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x, y + i)));
      } else if (ans[make_pair(x, y + i)] <= cost) {
        break;
      }
    }

    // 右
    for (long long i = 1; i <= K; i++) {
      if (!valid(x, y - i)) {
        break;
      }
      if (ans.count(make_pair(x, y - i)) == 0) {
        ans[make_pair(x, y - i)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x, y - i)));
      } else if (ans[make_pair(x, y - i)] <= cost) {
        break;
      }
    }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long H, W, K;
long long xstart, ystart, xend, yend;
string ban[1000005];

map<pair<long long, long long>, long long> ans;

bool valid(long long x, long long y) {
  if (x < 0 || x >= H || y < 0 || y >= W) {
    return false;
  }

  return ban[x][y] == '.';
}

int main() {
  cin >> H >> W >> K;
  cin >> xstart >> ystart >> xend >> yend;
  xstart--;
  ystart--;
  xend--;
  yend--;

  for (long long i = 0; i < H; i++) {
    cin >> ban[i];
  }

  // cost, (x, y)
  // priority_queue< pair<long long, pair<long long, long long> >,
  //	vector<pair<long long, pair<long long, long long> > >,
  //	greater<pair<long long, pair<long long, long long> > > > Q;
  queue<pair<long long, pair<long long, long long>>> Q;

  Q.push(make_pair(0, make_pair(xstart, ystart)));

  while (!Q.empty()) {
    long long cost = Q.front().first;
    long long x = Q.front().second.first;
    long long y = Q.front().second.second;

    if (x == xend && y == yend) {
      cout << cost << endl;
      return 0;
    }

    Q.pop();

    // 下
    for (long long i = 1; i <= K; i++) {
      if (!valid(x + i, y)) {
        break;
      }
      if (ans.count(make_pair(x + i, y)) == 0) {
        ans[make_pair(x + i, y)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x + i, y)));
      }
    }
    // 上
    for (long long i = 1; i <= K; i++) {
      if (!valid(x - i, y)) {
        break;
      }
      if (ans.count(make_pair(x - i, y)) == 0) {
        ans[make_pair(x - i, y)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x - i, y)));
      }
    }

    // 右
    for (long long i = 1; i <= K; i++) {
      if (!valid(x, y + i)) {
        break;
      }
      if (ans.count(make_pair(x, y + i)) == 0) {
        ans[make_pair(x, y + i)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x, y + i)));
      }
    }

    // 右
    for (long long i = 1; i <= K; i++) {
      if (!valid(x, y - i)) {
        break;
      }
      if (ans.count(make_pair(x, y - i)) == 0) {
        ans[make_pair(x, y - i)] = cost + 1;
        Q.push(make_pair(cost + 1, make_pair(x, y - i)));
      }
    }
  }
  cout << -1 << endl;
}