Language: C++

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();


struct Node{
    int curr_x;
    int curr_y;
    int dir_headed;
    float dist;

    Node(int x,int y,int dir,float dis){
        curr_x=x,curr_y=y,dir_headed=dir,dist=dis;
    }
};

struct cmp{
    bool operator()(const Node &a,const Node &b){
        if(a.dist!=b.dist)
            return a.dist<b.dist;
        else if(a.curr_x!=b.curr_x) 
            return a.curr_x<b.curr_x; 
        else if(a.curr_y!=b.curr_y) 
            return a.curr_y<b.curr_y;
        else 
            return a.dir_headed<b.dir_headed; 
    }
};

int32_t main(){
    
    int n,m,k;cin>>n>>m>>k;
    vector<string> grid(n);
    int start_x,start_y,end_x,end_y;cin>>start_x>>start_y>>end_x>>end_y;
    start_x--,start_y--,end_x--,end_y--;

    for(auto &i : grid) cin>>i;


   float distance[n][m][4];
   for(int i=0;i<n;i++) for(int j=0;j<m;j++) for(int k=0;k<4;k++) distance[i][j][k]=1e9;

   set<Node,cmp> s; s.insert(Node(start_x,start_y,0,0.0));distance[start_x][start_y][0]=0;

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


   while(!s.empty()){
       if(distance[s.begin()->curr_x][s.begin()->curr_y][s.begin()->dir_headed]<s.begin()->dist){s.erase(s.begin());continue;}
       int curr_x=s.begin()->curr_x;
       int curr_y=s.begin()->curr_y;
       int dir_headed=s.begin()->dir_headed;
       float dis=s.begin()->dist;
       s.erase(s.begin());

       for(int i=0;i<4;i++){
            int next_x=curr_x+dx[i],next_y=curr_y+dy[i];
            float dist;
            if(i==dir_headed)dist=dis+1.0/k;
            else {distance[curr_x][curr_y][i]=min(distance[curr_x][curr_y][i],ceil(dis));dist=ceil(dis)+1.0/k;}
            if(next_x>=0&&next_x<n&&next_y>=0&&next_y<m&&grid[next_x][next_y]!='@'&&distance[next_x][next_y][i]>dist){
                s.insert(Node(next_x,next_y,i,dist));
                distance[next_x][next_y][dir_headed]=dist;

            }
        }
   }

    int ans=1e9;
    for(int i=0;i<4;i++) ans=min(ans,(int)ceil(distance[end_x][end_y][i]));


   if(ans>=1e9) cout<<-1<<"\n";
   else{
       cout<<ans<<"\n";
   }


    return 0;
}

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 = cmp; _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 = cmp; _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 = cmp; _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 = cmp; _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>, cmp, 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:44:29:   required from here
   44 |    set<Node,cmp> s; s.insert(Node(start_x,start_y,0,0.0));distance[start_x][start_y][0]=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 cmp&, const Node&, const Node&>' evaluates to false