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

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

const int inf = 1e9;

#define x1 qoiweur
#define y1 waklsdjf
#define x2 asldkfjlsajdfl
#define y2 qqoiuiui

int n, m, k;
int x1, y1, x2, y2;

vector<vector<int>> g;
vector<vector<int>> dist;

struct dat {
  pair<int, int> cost;
  int x, y;
  int dir;
  dat(pair<int, int> a, int b, int c, int d) {
    cost = a;
    x = b;
    y = c;
    dir = d;
  }
  bool operator<(const dat &o) const {
    return make_pair(cost, make_pair(x, make_pair(y, dir))) <
           make_pair(o.cost, make_pair(o.x, make_pair(o.y, o.dir)));
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  g.resize(n, vector<int>(m, 0));
  dist.resize(n, vector<int>(m, inf));

  vector<vector<vector<int>>> done(n,
                                   vector<vector<int>>(m, vector<int>(4, 0)));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '@') {
        g[i][j] = 1;
      }
    }
  }

  dist[x1][y1] = 0;
  set<dat> s;
  s.insert({{0, 0}, x1, y1, 0});
  s.insert({{0, 0}, x1, y1, 1});
  s.insert({{0, 0}, x1, y1, 2});
  s.insert({{0, 0}, x1, y1, 3});

  while (!s.empty()) {
    auto cur = *s.begin();
    s.erase(s.begin());
    if (g[cur.x][cur.y] == 1) {
      continue;
    }
    if (done[cur.x][cur.y][cur.dir])
      continue;
    done[cur.x][cur.y][cur.dir] = 1;
    dist[cur.x][cur.y] = cur.cost.first + (cur.cost.second > 00);

    for (int dd = 0; dd < 4; dd++) {
      int x = cur.x;
      int y = cur.y;
      pair<int, int> cost = cur.cost;
      if (dd == cur.dir) {
        if (dd == 0) {
          x++;
        } else if (dd == 1) {
          x--;
        } else if (dd == 2) {
          y++;
        } else if (dd == 3) {
          y--;
        }
        if (x < 0 || y < 0 || x >= n || y >= m)
          continue;
        cost.second++;
        if (cost.second == k) {
          cost.first++;
          cost.second = 0;
        }
        s.insert({cost, x, y, dd});
      } else {
        if (cost.second > 0) {
          cost.first++;
        }
        cost.second = 0;
        s.insert({cost, x, y, dd});
      }
    }
  }

  // for (int i=0; i<n; i++) {
  // 	for (int j=0; j<m; j++) {
  // 	    cout<<dist[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }

  auto res = dist[x2][y2];
  if (res == inf)
    out(-1);
  out(res);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

const int inf = 1e9;

#define x1 qoiweur
#define y1 waklsdjf
#define x2 asldkfjlsajdfl
#define y2 qqoiuiui

int n, m, k;
int x1, y1, x2, y2;

vector<vector<int>> g;
vector<vector<int>> dist;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  g.resize(n, vector<int>(m, 0));
  dist.resize(n, vector<int>(m, inf));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '@') {
        g[i][j] = 1;
      }
    }
  }

  dist[x1][y1] = 0;
  set<pair<int, pair<int, int>>> s;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      s.insert({dist[i][j], {i, j}});
    }
  }

  auto maybe_add = [&](int cost, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m)
      return;
    if (dist[x][y] <= cost)
      return;

    s.erase({dist[x][y], {x, y}});
    dist[x][y] = cost;
    s.insert({dist[x][y], {x, y}});
  };

  auto add_nei = [&](int i, int j) {
    for (int x = 1; x <= k; x++) {
      if (j + x >= m)
        break;
      if (g[i][j + x])
        break;
      maybe_add(1 + dist[i][j], i, j + x);
    }

    for (int x = 1; x <= k; x++) {
      if (j - x < 0)
        break;
      if (g[i][j - x])
        break;
      maybe_add(1 + dist[i][j], i, j - x);
    }

    for (int x = 1; x <= k; x++) {
      if (i + x >= n)
        break;
      if (g[i + x][j])
        break;
      maybe_add(1 + dist[i][j], i + x, j);
    }

    for (int x = 1; x <= k; x++) {
      if (i - x < 0)
        break;
      if (g[i - x][j])
        break;
      maybe_add(1 + dist[i][j], i - x, j);
    }
  };

  while (!s.empty()) {
    auto cur = *s.begin();
    s.erase(s.begin());
    int i = cur.second.first;
    int j = cur.second.second;
    int wei = cur.first;
    assert(wei == dist[i][j]);

    add_nei(i, j);
  }

  if (dist[x2][y2] == inf)
    out(-1);

  out(dist[x2][y2]);

  return 0;
}