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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  char c[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));
  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int y = current_pos.first;
    int x = current_pos.second;
    que.pop();
    for (int i = 1; i <= k; i++) {
      int next_x = x + i;
      if (next_x < 0 or next_x >= w or c[y][next_x] == '@')
        break;
      if (dist[y][next_x] != -1 and dist[y][next_x] <= dist[y][x])
        break;
      if (dist[y][next_x] > dist[y][x] + 1 or dist[y][next_x] == -1) {
        que.push(make_pair(y, next_x));
        dist[y][next_x] = dist[y][x] + 1;
      }
    }
    for (int i = 1; i <= k; i++) {
      int next_x = x - i;
      if (next_x < 0 or next_x >= w or c[y][next_x] == '@')
        break;
      if (dist[y][next_x] != -1 and dist[y][next_x] <= dist[y][x])
        break;
      if (dist[y][next_x] > dist[y][x] + 1 or dist[y][next_x] == -1) {
        que.push(make_pair(y, next_x));
        dist[y][next_x] = dist[y][x] + 1;
      }
    }
    for (int i = 1; i <= k; i++) {
      int next_y = y + i;
      if (next_y < 0 or next_y >= h or c[next_y][x] == '@')
        break;
      if (dist[next_y][x] != -1 and dist[next_y][x] <= dist[y][x])
        break;
      if (dist[next_y][x] > dist[y][x] + 1 or dist[next_y][x] == -1) {
        que.push(make_pair(next_y, x));
        dist[next_y][x] = dist[y][x] + 1;
      }
    }
    for (int i = 1; i <= k; i++) {
      int next_y = y - i;
      if (next_y < 0 or next_y >= h or c[next_y][x] == '@')
        break;
      if (dist[next_y][x] != -1 and dist[next_y][x] <= dist[y][x])
        break;
      if (dist[next_y][x] > dist[y][x] + 1 or dist[next_y][x] == -1) {
        que.push(make_pair(next_y, x));
        dist[next_y][x] = dist[y][x] + 1;
      }
    }
  }
  if (dist[x2][y2] != -1)
    cout << dist[x2][y2] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  char c[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));
  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int y = current_pos.first;
    int x = current_pos.second;
    que.pop();
    for (int i = 1;; i++) {
      int next_x = x + i;
      if (next_x < 0 or next_x >= w or c[y][next_x] == '@')
        break;
      int cnt = (i + k - 1) / k;
      if (dist[y][next_x] > dist[y][x] + cnt or dist[y][next_x] == -1) {
        que.push(make_pair(y, next_x));
        dist[y][next_x] = dist[y][x] + cnt;
      }
    }
    for (int i = 1;; i++) {
      int next_x = x - i;
      if (next_x < 0 or next_x >= w or c[y][next_x] == '@')
        break;
      int cnt = (i + k - 1) / k;
      if (dist[y][next_x] > dist[y][x] + cnt or dist[y][next_x] == -1) {
        que.push(make_pair(y, next_x));
        dist[y][next_x] = dist[y][x] + cnt;
      }
    }
    for (int i = 1;; i++) {
      int next_y = y + i;
      if (next_y < 0 or next_y >= h or c[next_y][x] == '@')
        break;
      int cnt = (i + k - 1) / k;
      if (dist[next_y][x] > dist[y][x] + 1 or dist[next_y][x] == -1) {
        que.push(make_pair(next_y, x));
        dist[next_y][x] = dist[y][x] + cnt;
      }
    }
    for (int i = 1;; i++) {
      int next_y = y - i;
      if (next_y < 0 or next_y >= h or c[next_y][x] == '@')
        break;
      int cnt = (i + k - 1) / k;
      if (dist[next_y][x] > dist[y][x] + 1 or dist[next_y][x] == -1) {
        que.push(make_pair(next_y, x));
        dist[next_y][x] = dist[y][x] + cnt;
      }
    }
  }
  if (dist[x2][y2] != -1)
    cout << dist[x2][y2] << endl;
  else
    cout << -1 << endl;
  return 0;
}