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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f;
int tc;
int h, w, k;
string grid[M];
int dir[] = {-1, 1, 0, 0};
int dic[] = {0, 0, -1, 1};
int dis[M], directions[M];

bool valid(int r, int c) {
  return r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '@';
}

int main() {
  int x1, x2, y1, y2;
  scanf("%d %d %d ", &h, &w, &k);
  scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);

  for (int i = 0; i < h; i++) {
    string s;
    getline(cin, s);
    grid[i] = s;
  }
  x1--;
  y1--;
  x2--;
  y2--;
  memset(dis, -1, sizeof dis);
  dis[x1 * w + y1] = 0;
  directions[x1 * w + y1] = 15;
  queue<pair<int, int>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    int ur = q.front().first, uc = q.front().second;
    // cout<<ur<<" "<<uc<<endl;
    q.pop();
    if (ur == x2 && uc == y2)
      break;
    for (int i = 0; i < 4; i++) {
      int steps = 1, vr, vc;
      while (steps <= k) {
        vr = ur + (dir[i] * steps);
        vc = uc + (dic[i] * steps);
        if (valid(vr, vc)) {
          if (!((directions[vr * w + vc] >> i) & 1)) {
            if (!directions[vr * w + vc]) {
              dis[vr * w + vc] = dis[ur * w + uc] + 1;
              q.push({vr, vc});
            }
            directions[vr * w + vc] |= (1 << i);
          } else if (dis[vr * w + vc] == dis[ur * w + uc] + 1) {
            steps++;
            continue;
          } else
            break;

        } else
          break;
        steps++;
      }
    }
  }
  printf("%d", dis[x2 * w + y2]);
  return 0;
}

// Another Submission


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
typedef long long ll;

#define row second.first
#define col second.second
#define cost first

int const N = 2e5 + 5, M = 1e3 + 5, MOD = 1e9 + 7, OO = 0x3f3f3f3f;

int n, m, k, X1, Y1, X2, Y2;
vector<vector<int>> dis;
string grid[N];

int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};

bool valid(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m && grid[r][c] == '.';
}

void dijkstra(int sr, int sc) {
  int ur, uc, vr, vc, c;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  dis[sr][sc] = 0;
  q.push({0, {sr, sc}});
  while (!q.empty()) {
    ur = q.top().row;
    uc = q.top().col;
    c = q.top().cost;
    q.pop();
    if (ur == X2 - 1 && uc == Y2 - 1)
      return;
    if (dis[ur][uc] != c)
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int cnt = 0;
        vr = ur + (j * dr[i]);
        vc = uc + (j * dc[i]);
        if (valid(vr, vc)) {
          if (dis[vr][vc] == OO) {
            dis[vr][vc] = dis[ur][uc] + 1;
            q.push({dis[vr][vc], {vr, vc}});
          } else
            cnt++;
          if (cnt == 2)
            break;
        } else
          break;
      }
    }
  }
}

int main() {
  FIO
          //    freopen("input.txt","rt",stdin);
          //    freopen("output.txt","wt",stdout);
          cin >>
      n >> m >> k >> X1 >> Y1 >> X2 >> Y2;
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  dis = vector<vector<int>>(n + 1, vector<int>(m + 1, OO));
  dijkstra(X1 - 1, Y1 - 1);
  if (dis[X2 - 1][Y2 - 1] == OO)
    cout << "-1\\n";
  else
    cout << dis[X2 - 1][Y2 - 1] << "\\n";
}