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

#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ll long long
#define PR pair<int, int>

const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const long long INF = 5e18;
const double PI = 2 * acos(0.0);
// template<typename T> using ordered_set = tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

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

int n, m, k, sx, sy, fx, fy;
vector<vector<int>> vis, dis;
string s[N];

bool valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }

void bfs() {
  queue<PR> q;
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (!q.empty()) {
    PR u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = (dx[i] * j) + u.first;
        int y = (dy[i] * j) + u.second;
        if (!valid(x, y)) {
          continue;
        }
        if (s[x][y] == '@') {
          break;
        }
        if (vis[x][y] == 1 && dis[u.first][u.second] + 1 > dis[x][y]) {
          break;
        }
        if (vis[x][y] == 1) {
          continue;
        }
        q.push({x, y});
        vis[x][y] = 1;
        dis[x][y] = dis[u.first][u.second] + 1;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> k >> sx >> sy >> fx >> fy;
  sx--, sy--, fx--, fy--;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    vector<int> tmp(m, 0);
    vis.push_back(tmp);
    dis.push_back(tmp);
  }
  bfs();
  cout << (vis[fx][fy] == 1 ? dis[fx][fy] : -1) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ll long long
#define PR pair<int, int>

const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const long long INF = 5e18;
const double PI = 2 * acos(0.0);
// template<typename T> using ordered_set = tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

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

struct Data {
  int x, y, dir, cnt;

  Data() { dir = cnt = x = y = 0; }

  Data(int _x, int _y, int d, int c) {
    dir = d;
    cnt = c;
    x = _x;
    y = _y;
  }
};

int h, w, k, sx, sy, fx, fy;
map<PR, int> vis, cost;
string s[N];

bool valid(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }

int bfs() {
  queue<Data> q;
  vis[{sx, sy}] = 1;
  for (int i = 0; i < 4; i++) {
    int x = dx[i] + sx;
    int y = dy[i] + sy;
    if (valid(x, y) && s[x][y] != '@') {
      q.push(Data(x, y, i, 1));
      vis[{x, y}] = cost[{x, y}] = 1;
    }
  }
  int ans = 2e9;
  while (!q.empty()) {
    Data u = q.front();
    q.pop();
    // Compress
    int tmp = cost[{u.x, u.y}] - u.cnt;
    int hop = u.cnt / k;
    if (u.cnt % k) {
      hop++;
    }
    // Destination
    if (u.x == fx && u.y == fy) {
      ans = min(ans, tmp + hop);
      continue;
    }
    // Same Direction
    int x = dx[u.dir] + u.x;
    int y = dy[u.dir] + u.y;
    if (valid(x, y) && s[x][y] != '@') {
      if (vis[{x, y}] == 0) {
        vis[{x, y}] = 1;
        cost[{x, y}] = cost[{u.x, u.y}] + 1;
        q.push(Data(x, y, u.dir, u.cnt + 1));
      } else if (tmp + hop < cost[{x, y}]) {
        cost[{x, y}] = tmp + u.cnt + 1;
        q.push(Data(x, y, u.dir, u.cnt + 1));
      }
    }
    cost[{u.x, u.y}] = tmp + hop;
    for (int i = 0; i < 4; i++) {
      if (i == u.dir) {
        continue;
      }
      x = dx[i] + u.x;
      y = dy[i] + u.y;
      if (valid(x, y) && s[x][y] != '@') {
        if (vis[{x, y}] == 0) {
          cost[{x, y}] = tmp + hop + 1;
          vis[{x, y}] = 1;
          q.push(Data(x, y, i, 1));
        } else if (tmp + hop + 1 < cost[{x, y}]) {
          cost[{x, y}] = tmp + hop + 1;
          q.push(Data(x, y, i, 1));
        }
      }
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k >> sx >> sy >> fx >> fy;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  sx--, sy--, fx--, fy--;
  int ans = bfs();
  if (ans == 2e9) {
    ans = -1;
  }
  cout << ans << '\\n';
  return 0;
}