Problem p02644 (C++) — buggy submission, verdict: Runtime Error

/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(17);
  return 0;
}();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename... T> string format(const string &fmt, T &&...args) {
  size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
  unique_ptr<char[]> buf(new char[sz]);
  snprintf(buf.get(), sz, fmt.c_str(), args...);
  return string(buf.get(), buf.get() + sz - 1);
}

template <class T> struct rge {
  T b, e;
  auto begin() const { return b; }
  auto end() const { return e; }
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
#ifdef LOCAL
  debug(int line) {
    if (line)
      cerr << "LINE(" << line << ") -> ";
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator<<(T i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator<<(T i) {
    return *this << range(begin(i), end(i));
  }
  template <class T, class U> debug &operator<<(pair<T, U> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  debug &operator<<(tuple<> &) { return *this << "()"; };
  template <class... T> debug &operator<<(tuple<T...> d) {
    *this << "(";
    debug_tuple<sizeof...(T), 0>(d);
    return *this << ")";
  }
  template <size_t L, size_t I, class T> void debug_tuple(const T &t) {
    *this << (I == 0 ? "" : ", ") << get<I>(t);
    if (I + 1 < L)
      debug_tuple<L, (I + 1) % L>(t);
  }
  template <class T> debug &operator<<(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << (it != d.b ? ", " : "") << *it;
    return *this << "]";
  }
  debug &operator<<(ostream &(*pf)(std::ostream &)) {
    cerr << pf;
    return *this;
  }
#else
  debug(int) {}
  template <class T> debug &operator<<(T &&) { return *this; }
  debug &operator<<(ostream &(*)(std::ostream &)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0() debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int INF = 0x3f3f3f3f;

void solve(int) {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;

  vector<string> grid(H);
  for (int i = 0; i < H; ++i) {
    cin >> grid[i];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;
  queue<pair<int, int>> que;
  que.emplace(x1, y1);

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  while (que.size()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();

    if (MT(x, y) == MT(x2, y2)) {
      cout << dist[x][y] << endl;
      return;
    }

    for (int i = 0; i < 4; ++i) {
      int xx = x, yy = y;
      for (int j = 1; j <= K; ++j) {
        xx += dx[i];
        yy += dy[i];

        if (xx >= 0 and xx < H and yy >= 0 and yy < W and
            grid[xx][yy] == '.' and dist[xx][yy] >= dist[x][y] + 1) {

          if (dist[xx][yy] > dist[x][y] + 1) {
            dist[xx][yy] = dist[x][y] + 1;
            que.emplace(xx, yy);
          }
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  int T = 1;
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve(i);
  }
  return 0;
}


Error: 0

Fixed submission:

/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(17);
  return 0;
}();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename... T> string format(const string &fmt, T &&...args) {
  size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
  unique_ptr<char[]> buf(new char[sz]);
  snprintf(buf.get(), sz, fmt.c_str(), args...);
  return string(buf.get(), buf.get() + sz - 1);
}

template <class T> struct rge {
  T b, e;
  auto begin() const { return b; }
  auto end() const { return e; }
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
#ifdef LOCAL
  debug(int line) {
    if (line)
      cerr << "LINE(" << line << ") -> ";
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator<<(T i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator<<(T i) {
    return *this << range(begin(i), end(i));
  }
  template <class T, class U> debug &operator<<(pair<T, U> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  debug &operator<<(tuple<> &) { return *this << "()"; };
  template <class... T> debug &operator<<(tuple<T...> d) {
    *this << "(";
    debug_tuple<sizeof...(T), 0>(d);
    return *this << ")";
  }
  template <size_t L, size_t I, class T> void debug_tuple(const T &t) {
    *this << (I == 0 ? "" : ", ") << get<I>(t);
    if (I + 1 < L)
      debug_tuple<L, (I + 1) % L>(t);
  }
  template <class T> debug &operator<<(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << (it != d.b ? ", " : "") << *it;
    return *this << "]";
  }
  debug &operator<<(ostream &(*pf)(std::ostream &)) {
    cerr << pf;
    return *this;
  }
#else
  debug(int) {}
  template <class T> debug &operator<<(T &&) { return *this; }
  debug &operator<<(ostream &(*)(std::ostream &)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0() debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int INF = 0x3f3f3f3f;

pair<int, int> que[1000005];

void solve(int) {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;

  vector<string> grid(H);
  for (int i = 0; i < H; ++i) {
    cin >> grid[i];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;

  int l = 0, r = 0;
  que[r++] = {x1, y1};

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  while (l < r) {
    int x, y;
    tie(x, y) = que[l++];

    if (MT(x, y) == MT(x2, y2)) {
      cout << dist[x][y] << endl;
      return;
    }

    for (int i = 0; i < 4; ++i) {
      int xx = x, yy = y;
      for (int j = 1; j <= K; ++j) {
        xx += dx[i];
        yy += dy[i];

        if (xx >= 0 and xx < H and yy >= 0 and yy < W and
            grid[xx][yy] == '.' and dist[xx][yy] >= dist[x][y] + 1) {
          dist[xx][yy] = dist[x][y] + 1;
          que[r++] = {xx, yy};
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  int T = 1;
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve(i);
  }
  return 0;
}