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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

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

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> grid(H);
  int dist[H][W];
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
    for (int j = 0; j < W; j++)
      dist[i][j] = (grid[i][j] == '@' ? -1 : INT_MAX);
  }
  ii init = {x1, y1}, nd = {x2, y2};
  dist[init.first][init.second] = 0;
  grid[init.first][init.second] = '@';
  queue<ii> q;
  q.push(init);
  while (!q.empty()) {
    ii cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        ii nxt = cur;
        nxt.first += dx[i] * j;
        nxt.second += dy[i] * j;
        if (nxt.first < 0 || nxt.first >= H || nxt.second < 0 ||
            nxt.second >= W ||
            dist[nxt.first][nxt.second] <= dist[cur.first][cur.second])
          break;
        if (dist[nxt.first][nxt.second] == INT_MAX)
          q.push(nxt);
        dist[nxt.first][nxt.second] = dist[cur.first][cur.second] + 1;
      }
    }
  }
  cout << (dist[nd.first][nd.second] == INT_MAX ? -1
                                                : dist[nd.first][nd.second])
       << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

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

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> grid(H);
  int dist[H][W];
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
    for (int j = 0; j < W; j++)
      dist[i][j] = (grid[i][j] == '@' ? -1 : INT_MAX);
  }
  ii init = {x1, y1}, nd = {x2, y2};
  dist[init.first][init.second] = 0;
  grid[init.first][init.second] = '@';
  queue<ii> q;
  q.push(init);
  while (!q.empty()) {
    ii cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        ii nxt = cur;
        nxt.first += dx[i] * j;
        nxt.second += dy[i] * j;
        if (nxt.first < 0 || nxt.first >= H || nxt.second < 0 ||
            nxt.second >= W)
          continue;
        if (grid[nxt.first][nxt.second] != '.')
          break;
        if (dist[nxt.first][nxt.second] < dist[cur.first][cur.second] + 1)
          break;
        dist[nxt.first][nxt.second] = dist[cur.first][cur.second] + 1;
        q.push(nxt);
      }
    }
  }
  cout << (dist[nd.first][nd.second] == INT_MAX ? -1
                                                : dist[nd.first][nd.second])
       << "\\n";
  return 0;
}