Language: C++

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define li          long
#define ll          long long
#define ull         unsigned long long
#define ff          first
#define ss          second
#define pii         pair<int,int>
#define pli         pair<li,li>
#define pll         pair<ll,ll>
#define pb          push_back
#define fo(i,k,n)   for(int i = k;i<n; i++)
#define ro(i,k,n)   for(int i = k; i>=n; i--)
#define tc          int t;cin>>t;while(t--)
#define all(v)      v.begin(),v.end()
#define inf         INT_MAX
#define ninf        INT_MIN
#define W(x)        cout<<x<<endl
#define WW(x,y)     cout<<x<<' '<<y<<endl;
#define in(x)       cin>>x
#define vi          vector<int>
#define vll         vector<long long>
#define vpi         vector<pair<int>>
#define vpll        vector<pair<long long>>
#define vvi         vector<vector<int>>
#define vvll        vector<vector<long long>>
#define vvpi        vector<vector<pair<int,int>>>
#define vvpll       vector<vector<pair<long long>>>
const ll mod1 = 99844353;
const ll mod2 = 1e9+7;
/* template <class T>
using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; */

void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
} 

template<class T>
T power(T a,T b){
    T res = (T)1;
    while(b>0){
        if(b&1)res = res*a;
        b>>=1;
        a*=a;
    }
    return res;
}

const int mxn = 1e6;
vector<vector<char>> g;

struct node{
    int x,y,w;
    node(int x,int y,int z){
        this->x = x,this->y = y,this->w = z;
    }
};
struct comp{
    bool operator()(const node& n1,const node& n2){
        return n1.w<=n2.w;
    }
};
void solve(){
    int h,w,k,x1,y1,x2,y2;
    cin>>h>>w>>k>>x1>>y1>>x2>>y2;
    x1-=1,y1-=1,x2-=1,y2-=1;
    g = vector<vector<char>>(h,vector<char>(w));
    for(int i = 0; i<h; i++){
        for(int j = 0; j<w; j++)cin>>g[i][j];
    }
    vector<vector<int>> dp(h,vector<int>(w,INT_MAX));
    set<node,comp> st;
    st.insert(node(x1,y1,0));
    dp[x1][y1] = 0;
    while(!st.empty()){
        node start = *st.begin();
        st.erase(st.begin());
        if(start.x == x2&&start.y == y2){
            cout<<start.w<<"\n";
            return;
        }
        for(int x = start.x+1; x<=start.x+k&&x<h; x++){
            if(g[x][start.y] == '@')break;
            if(dp[x][start.y]>start.w+1){
                if(st.find(node(x,start.y,dp[x][start.y]))!=st.end())st.erase(node(x,start.y,dp[x][start.y]));
                dp[x][start.y] = start.w+1;
                st.insert(node(x,start.y,dp[x][start.y]));
            }
        }
        for(int x = start.x-1; x>=start.x-k&&x>=0; x--){
            if(g[x][start.y] == '@')break;
            if(dp[x][start.y]>start.w+1){
                if(st.find(node(x,start.y,dp[x][start.y]))!=st.end())st.erase(node(x,start.y,dp[x][start.y]));
                dp[x][start.y] = start.w+1;
                st.insert(node(x,start.y,dp[x][start.y]));
            }
        }
        for(int y = start.y+1; y<=start.y+k&&y<w; y++){
            if(g[start.x][y] == '@')break;
            if(dp[start.x][y]>start.w+1){
                if(st.find(node(start.x,y,dp[start.x][y]))!=st.end())st.erase(node(start.x,y,dp[start.x][y]));
                dp[start.x][y] = start.w+1;
                st.insert(node(start.x,y,dp[start.x][y]));
            }
        }
        for(int y = start.y-1; y>=start.y-k&&y>=0; y--){
            if(g[start.x][y] == '@')break;
            if(dp[start.x][y]>start.w+1){
                if(st.find(node(start.x,y,dp[start.x][y]))!=st.end())st.erase(node(start.x,y,dp[start.x][y]));
                dp[start.x][y] = start.w+1;
                st.insert(node(start.x,y,dp[start.x][y]));
            }
        }
    }
    if(dp[x2][y2] == INT_MAX)cout<<-1<<"\n";
    else cout<<dp[x2][y2]<<"\n";
}

signed main(){
    fastio();
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = node; _Val = node; _KeyOfValue = std::_Identity<node>; _Compare = comp; _Alloc = std::allocator<node>; _Const_Link_type = const std::_Rb_tree_node<node>*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = node; _Val = node; _KeyOfValue = std::_Identity<node>; _Compare = comp; _Alloc = std::allocator<node>; key_type = node]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = node; _Key = node; _Val = node; _KeyOfValue = std::_Identity<node>; _Compare = comp; _Alloc = std::allocator<node>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:523:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = node; _Compare = comp; _Alloc = std::allocator<node>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<node, node, std::_Identity<node>, comp, std::allocator<node> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<node>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<node>, node>::rebind<node>; typename _Alloc::value_type = node; value_type = node]'
  523 |           _M_t._M_insert_unique(std::move(__x));
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:79:14:   required from here
   79 |     st.insert(node(x1,y1,0));
      |     ~~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const comp&, const node&, const node&>' evaluates to false