Language: C++

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

#define int ll

int k;

template<typename T>
struct edge{
  int to;
  T cost;
  edge(int to,T cost):to(to),cost(cost){}
};


template<typename T>
vector<int> dijkstra(vector<vector<edge<T>>> &g,int s){//sは始点
  const auto INFTY=numeric_limits<int>::max()/10;//2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<int>dist(g.size(),INFTY);
  using P=pair<int,pair<int,int>>;//firstは最短距離,secondは頂点の番号
  priority_queue<P,vector<P>,greater<P>>pq;
  dist[s]=0;
  rep(i,4)pq.emplace(dist[s],make_pair(s,i));
  while(!pq.empty()){
    auto x=pq.top();pq.pop();
    int v=x.second.first;
		int from=x.second.second;
    if(dist[v]<x.first)continue;//最短距離でなければ飛ばす
    for(auto e:g[v]){
				if(from==e.cost.second){
					if(dist[e.to]>=dist[v]+e.cost.first){
						dist[e.to]=dist[v]+e.cost.first;
						pq.emplace(dist[e.to],make_pair(e.to,e.cost.second));
					}
				}
				else{
					if(dist[e.to]>=(dist[v]+k-1)/k*k+e.cost.first){
						dist[e.to]=(dist[v]+k-1)/k*k+e.cost.first
						pq.emplace(dist[e.to],make_pair(e.to,e.cost.second));
					}
				}
    }
  }
  return dist;
}

using P=pair<int,int>;

void solve()
{
	int h,w;
	cin>>h>>w>>k;
	int x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	x1--;y1--;
	x2--;y2--;
	vector<string>c(h);
	rep(i,h)cin>>c[i];
	vector<vector<edge<P>>>g(h*w);
	const int INFTY=numeric_limits<int>::max()/10;
	char o='@';
	rep(i,h){
		rep(j,w){
			if(c[i][j]=='@')continue;
			if(i+1<h&&c[i+1][j]!=o){
				g[i*w+j].eb((i+1)*w+j,P{1,0});
			}
			if(i-1>=0&&c[i-1][j]!=o){
				g[i*w+j].eb((i-1)*w+j,P{1,1});
			}
			if(j+1<w&&c[i][j+1]!=o){
				g[i*w+j].eb(i*w+(j+1),P{1,2});
			}
			if(j-1>=0&&c[i][j-1]!=o){
				g[i*w+j].eb(i*w+(j-1),P{1,3});
			}
		}
	}
	auto d=dijkstra<P>(g,x1*w+y1);
	// rep(i,h){
	// 	rep(j,w){
	// 		int s=INFTY;
	// 		chmin(s,d[i*w+j]);
	// 		if(s==INFTY){
	// 			cout<<'@';
	// 		}
	// 		else cout<<s;
	// 		cout<<" ";
	// 	}
	// 	cout<<"\n";
	// }
	int ans=INFTY;
	chmin(ans,d[x2*w+y2]);
	if(ans==INFTY)cout<<-1;
	else cout<<((ans+k-1)/k*k)/k;
}

#undef int
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

Compiler output:
a.cc: In function 'std::vector<long long int> dijkstra(std::vector<std::vector<edge<T> > >&, ll)':
a.cc:88:90: error: expected ';' before 'pq'
   88 |                                                 dist[e.to]=(dist[v]+k-1)/k*k+e.cost.first
      |                                                                                          ^
      |                                                                                          ;
   89 |                                                 pq.emplace(dist[e.to],make_pair(e.to,e.cost.second));
      |                                                 ~~