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

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

const int INF = 1e9;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> c(H + 2, vector<int>(W + 2));
  vector<vector<int>> d(H + 2, vector<int>(W + 2, INF));

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= W; j++) {
      c[i][j] = (s[j - 1] == '.' ? 1 : 0);
    }
  }

  priority_queue<pair<int, pair<pair<int, int>, int>>,
                 vector<pair<int, pair<pair<int, int>, int>>>,
                 greater<pair<int, pair<pair<int, int>, int>>>>
      que;
  que.push(make_pair(0, make_pair(make_pair(x1, y1), -1)));
  while (!que.empty()) {
    int dist = que.top().first;
    int x = que.top().second.first.first, y = que.top().second.first.second;
    int dir = que.top().second.second;
    que.pop();
    if (x == x2 && y == y2) {
      cout << dist << endl;
      return 0;
    }
    if (dist > d[x][y])
      continue;
    for (int i = 0; i < 4; i++) {
      if (i % 2 == dir % 2)
        continue;
      int ddx = 0, ddy = 0;
      while (true) {
        ddx += dx[i], ddy += dy[i];
        int tmp = (abs(ddx + ddy) + K - 1) / K;
        if (c[x + ddx][y + ddy]) {
          if (d[x + ddx][y + ddy] > dist + tmp) {
            d[x + ddx][y + ddy] = dist + tmp;
            que.push(make_pair(dist + tmp,
                               make_pair(make_pair(x + ddx, y + ddy), i)));
          }
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

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

const int INF = 1e9;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> c(H + 2, vector<int>(W + 2));
  vector<vector<int>> d(H + 2, vector<int>(W + 2, INF));

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= W; j++) {
      c[i][j] = (s[j - 1] == '.' ? 1 : 0);
    }
  }

  priority_queue<pair<int, pair<pair<int, int>, int>>,
                 vector<pair<int, pair<pair<int, int>, int>>>,
                 greater<pair<int, pair<pair<int, int>, int>>>>
      que;
  que.push(make_pair(0, make_pair(make_pair(x1, y1), -1)));
  while (!que.empty()) {
    int dist = que.top().first;
    int x = que.top().second.first.first, y = que.top().second.first.second;
    int dir = que.top().second.second;
    que.pop();
    if (x == x2 && y == y2) {
      cout << dist << endl;
      return 0;
    }
    if (dist > d[x][y])
      continue;
    for (int i = 0; i < 4; i++) {
      if (i == dir)
        continue;
      int ddx = 0, ddy = 0;
      while (true) {
        ddx += dx[i], ddy += dy[i];
        int tmp = (abs(ddx + ddy) + K - 1) / K;
        if (c[x + ddx][y + ddy]) {
          if (d[x + ddx][y + ddy] > dist + tmp) {
            d[x + ddx][y + ddy] = dist + tmp;
            que.push(make_pair(dist + tmp,
                               make_pair(make_pair(x + ddx, y + ddy), i)));
          }
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}