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

#include <algorithm>
#include <assert.h>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long int ll;
using ull = unsigned long long;

int d[1000100];
char fi[1000100];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x, y, xx, yy;
  cin >> x >> y >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> fi[i * w + j];
      d[i * w + j] = 1e9;
    }
  }
  d[x * w + y] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (q.size()) {
    auto p = q.front();
    q.pop();
    int sx = p.first, sy = p.second;
    for (int i = sx + 1; i < h && i <= sx + k; i++) {
      if (fi[i * w + sy] != '.')
        break;
      if (d[i * w + sy] <= d[sx * w + sy])
        break;
      if (d[i * w + sy] > d[sx * w + sy] + 1) {
        d[i * w + sy] = d[sx * w + sy] + 1;
        q.push(make_pair(i, sy));
      }
    }
    for (int i = sx - 1; i >= 0 && i >= sx - k; i--) {
      if (fi[i * w + sy] != '.')
        break;
      if (d[i * w + sy] <= d[sx * w + sy])
        break;
      if (d[i * w + sy] > d[sx * w + sy] + 1) {
        d[i * w + sy] = d[sx * w + sy] + 1;
        q.push(make_pair(i, sy));
      }
    }
    for (int i = sy + 1; i < w && i <= sy + k; i++) {
      if (fi[sx * w + i] != '.')
        break;
      if (d[sx * w + sy] >= d[sx * w + i])
        break;
      if (d[sx * w + sy] + 1 < d[sx * w + i]) {
        d[sx * w + i] = d[sx * w + sy] + 1;
        q.push(make_pair(sx, i));
      }
    }
    for (int i = sy - 1; i >= 0 && i >= sy - k; i--) {
      if (fi[sx * w + i] != '.')
        break;
      if (d[sx * w + sy] >= d[sx * w + i])
        break;
      if (d[sx * w + sy] + 1 < d[sx * w + i]) {
        d[sx * w + i] = d[sx * w + sy] + 1;
        q.push(make_pair(sx, i));
      }
    }
  }
  if (d[xx * w + yy] == 1e9) {
    printf("-1\\n");
  } else {
    printf("%d\\n", d[xx * w + yy]);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long int ll;

ll dp[1000100];

char fi[1000100];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x, y, xx, yy;
  cin >> x >> y >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> fi[i * w + j];
      dp[i * w + j] = 1e9;
    }
  }
  dp[x * w + y] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (q.size()) {
    auto p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < h && 0 <= ny && ny < w &&
          dp[nx * w + ny] >= dp[p.first * w + p.second] + 1 &&
          fi[nx * w + ny] != '@') {
        int mx = 1;
        for (int j = 2; j <= k; j++) {
          nx += dx[i], ny += dy[i];
          if (0 <= nx && nx < h && 0 <= ny && ny < w &&
              dp[nx * w + ny] >= dp[p.first * w + p.second] + 1 &&
              fi[nx * w + ny] != '@') {
            mx++;
          } else {
            nx -= dx[i], ny -= dy[i];
            break;
          }
        }
        /*if(p.first==2&&p.second==1){
                cout << i << " " << mx << endl;
        }*/
        for (int j = mx; j >= 1; j--) {
          if (dp[nx * w + ny] > dp[p.first * w + p.second] + 1)
            dp[nx * w + ny] = dp[p.first * w + p.second] + 1;
          q.push(make_pair(nx, ny));
          nx -= dx[i], ny -= dy[i];
        }
      } else
        continue;
    }
  }
  /*for(int i=0;i<h;i++){
          for(int j=0;j<w;j++){
                  cout << dp[i*w+j] << " ";
          }
          cout << endl;
  }*/
  if (dp[xx * w + yy] == 1e9) {
    printf("-1\\n");
  } else {
    printf("%lld\\n", dp[xx * w + yy]);
  }
}