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

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  pair<int, int> from, to;
  cin >> from.first >> from.second >> to.first >> to.second;
  from.first--;
  from.second--;
  to.first--;
  to.second--;
  vector<string> ike(h);
  REP(i, h)
  cin >> ike[i];
  vector<vector<int>> visited(h, vector<int>(w, 1000001));
  visited[from.first][from.second] = 0;
  queue<pair<pair<int, int>, int>> Q;
  Q.push({from, 0});
  int ans = -1;
  while (!Q.empty()) {
    pair<pair<int, int>, int> q = Q.front();
    Q.pop();
    if (q.first == to) {
      ans = q.second;
      break;
    }
    REP(i, 4) {
      FOR(j, 1, k + 1) {
        if (WRAP(q.first.first + dy[i] * j, q.first.second + dx[i] * j, h, w)) {
          if (ike[q.first.first + dy[i] * j][q.first.second + dx[i] * j] !=
              '@') {
            if (visited[q.first.first + dy[i] * j]
                       [q.first.second + dx[i] * j] == 1000001) {
              visited[q.first.first + dy[i] * j][q.first.second + dx[i] * j] =
                  q.second + 1;
              Q.push({{q.first.first + dy[i] * j, q.first.second + dx[i] * j},
                      q.second + 1});
            } else {
              if (visited[q.first.first + dy[i] * j]
                         [q.first.second + dx[i] * j] < q.second + 1)
                break;
            }
          } else {
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  pair<int, int> from, to;
  cin >> from.first >> from.second >> to.first >> to.second;
  from.first--;
  from.second--;
  to.first--;
  to.second--;
  vector<string> ike(h);
  REP(i, h)
  cin >> ike[i];
  vector<vector<bool>> visited(h, vector<bool>(w, false));
  visited[from.first][from.second] = true;
  queue<pair<pair<int, int>, int>> Q;
  Q.push({from, 0});
  int ans = -1;
  while (!Q.empty()) {
    pair<pair<int, int>, int> q = Q.front();
    Q.pop();
    if (q.first == to) {
      ans = q.second;
      break;
    }
    REP(i, 4) {
      FOR(j, 1, k + 1) {
        if (WRAP(q.first.first + dy[i] * j, q.first.second + dx[i] * j, h, w)) {
          if (ike[q.first.first + dy[i] * j][q.first.second + dx[i] * j] !=
              '@') {
            if (!visited[q.first.first + dy[i] * j]
                        [q.first.second + dx[i] * j]) {
              visited[q.first.first + dy[i] * j][q.first.second + dx[i] * j] =
                  true;
              Q.push({{q.first.first + dy[i] * j, q.first.second + dx[i] * j},
                      q.second + 1});
            }
          } else {
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}