Language: C++

// (c) 2020, redotter
// The grass was greener...

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#pragma comment(linker, "/stack:200000000")
#endif

#define pb push_back
#define the(a) (a).begin(), (a).end()
#define ft first
#define sd second
#define endl "\n"
#define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define y1 y11

using i64 = int;
using i32 = int;
using f64 = double;

template <class T> using vec = vector<T>;
using str = string; using p64 = pair<i64, i64>;
using v64 = vec<i64>; using vv64 = vec<v64>;
using s64 = set<i64>; using m64 = map<i64, i64>;
template <size_t Size1> using a64 = array<i64, Size1>;
template <size_t Size1, size_t Size2> using aa64 = array<a64<Size2>, Size1>;
template <class T> using hash_set = unordered_set<T>;
template <class T, class U> using hash_map = unordered_map<T, U>;

mt19937 gen(998244353);
const i64 inf = numeric_limits<i64>::max() / 2;

template <class T> inline i64 len(const T& a) { return (i64)(a.size()); }
inline void fast() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }
template <class T, class S>
inline bool check_min(T& a, S b) { if (a > b) return a = b, 1; return 0; }
template <class T, class S>
inline bool check_max(T& a, S b) { if (a < b) return a = b, 1; return 0; }

void read() {}
template<class T, class ...U>
void read(T& a, U& ...b) { cin >> a; read(b...); }
void print() {}
template<class T, class ...U>
void print(const T& a, const U& ...b) { cout << a << " "; print(b...); }
void println() { cout << "\n"; }
template<class T, class ...U>
void println(const T& a, const U& ...b) { cout << a << " "; println(b...); }

struct Problem {
	void files(str in, str out) {
#ifdef ONLINE_JUDGE
		freopen(in.c_str(), "r", stdin), freopen(out.c_str(), "w", stdout);
#endif
	}
	void solve(), input();
	void many() { i64 t; cin >> t; while (t--) input(), solve(); }
	void make(bool mt = 0) { if (mt) many(); else input(), solve(); }
} problem;

signed main() {
	fast();
	problem.make();
#ifdef _DEBUG
	println(); println("[finished]"); system("pause > nul");
#endif
	return 0;
}

i64 n, m, k;
i64 x1, y1, x2, y2;
vv64 a;
vec<p64> dirs = { {-1, 0}, {+1, 0}, {0, -1}, {0, +1} };

i64 ok(i64 i, i64 j) {
	if (!(0 <= i && i < n && 0 <= j && j < m)) return 0;
	return a[i][j];
}

struct Object {
	p64 p;
	i64 dir;
	bool operator < (Object a) const {
		pair<p64, i64> p1 = { p, dir };
		pair<p64, i64> p2 = { a.p, a.dir };
		return p1 < p2;
	}
};

i64 dijkstra(p64 s, p64 t) 
	map<Object, i64> d;
	set<pair<i64, Object>> q;
	forn(z, 4) {
		d[{s, z}] = 0;
		q.insert({ d[{s, z}], {s, z} });
	}
	while (!q.empty()) {
		Object v = q.begin()->sd;
		q.erase(q.begin());
		forn(j, 4) {
			Object u;
			u.p = { v.p.ft + dirs[j].ft, v.p.sd + dirs[j].sd };
			u.dir = j;
			if (!ok(u.p.ft, u.p.sd)) continue;
			if (!d.count(u)) d[u] = inf;
			i64 cur = inf;
			if (u.dir == v.dir) {
				cur = d[v] + 1;
			} else {
				cur = d[v] / k * k + k;
			}
			if (d[u] > cur) {
				q.erase({ d[u], u });
				d[u] = cur;
				q.insert({ d[u], u });
			}
		}
	}
	i64 ans = inf;
	forn(z, 4) {
		if (d.count({ t, z })) {
			check_min(ans, d[{t, z}]);
		}
	}
	return ans;
}

void Problem::solve() {
	i64 res = dijkstra({ x1, y1 }, { x2, y2 });
	i64 ans = (res + k - 1) / k;
	if (res == inf) ans = -1;
	println(ans);
}

void Problem::input() {
	read(n, m, k);
	read(x1, y1, x2, y2);
	--x1, --y1, --x2, --y2;
	a.resize(n, v64(m, 0));
	forn(i, n) {
		forn(j, m) {
			char c;
			read(c);
			a[i][j] = c != '@';
		}
	}
}


Compiler output:
a.cc:93:9: error: expected initializer before 'map'
   93 |         map<Object, i64> d;
      |         ^~~
a.cc:16:20: error: expected unqualified-id before 'for'
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                    ^~~
a.cc:95:9: note: in expansion of macro 'forn'
   95 |         forn(z, 4) {
      |         ^~~~
a.cc:16:42: error: expected constructor, destructor, or type conversion before '<' token
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                                          ^
a.cc:95:9: note: in expansion of macro 'forn'
   95 |         forn(z, 4) {
      |         ^~~~
a.cc:16:52: error: expected constructor, destructor, or type conversion before '++' token
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                                                    ^~
a.cc:95:9: note: in expansion of macro 'forn'
   95 |         forn(z, 4) {
      |         ^~~~
a.cc:99:9: error: expected unqualified-id before 'while'
   99 |         while (!q.empty()) {
      |         ^~~~~
a.cc:16:20: error: expected unqualified-id before 'for'
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                    ^~~
a.cc:122:9: note: in expansion of macro 'forn'
  122 |         forn(z, 4) {
      |         ^~~~
a.cc:16:42: error: expected constructor, destructor, or type conversion before '<' token
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                                          ^
a.cc:122:9: note: in expansion of macro 'forn'
  122 |         forn(z, 4) {
      |         ^~~~
a.cc:16:52: error: expected constructor, destructor, or type conversion before '++' token
   16 | #define forn(i, n) for (int (i) = 0; (i) < (n); (i)++)
      |                                                    ^~
a.cc:122:9: note: in expansion of macro 'forn'
  122 |         forn(z, 4) {
      |         ^~~~
a.cc:127:9: error: expected unqualified-id before 'return'
  127 |         return ans;
      |         ^~~~~~
a.cc:128:1: error: expected declaration before '}' token
  128 | }
      | ^
a.cc: In member function 'void Problem::solve()':
a.cc:131:19: error: 'dijkstra' was not declared in this scope
  131 |         i64 res = dijkstra({ x1, y1 }, { x2, y2 });
      |                   ^~~~~~~~