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;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
#define debug(x) cout<<#x<<' '<<x<<endl
#define read(x)  freopen("input.txt","r",stdin);
#define iter(x)  x.begin(),x.end()
#define ll long long
#define ull unsigned ll
#define endl '\n'
#define inf INT_MAX
#define mod 1000000007
set<pair<int,int> >visit;
vector<vector<pair<int,int> > >parent;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
pair<int,int>start,finish;
int h,w,k;
vector<vector<char> >grid;
bool valid(pair<int,int>p)
{
    return p.first<h and p.first>-1 and p.second<w and p.second>-1;
}
void bfs()
{
    visit.insert(start);
    queue<pair<int,int> >q;
    q.push(start);
    while(!q.empty())
    {
        pair<int,int> p=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            pair<int,int>j={p.first+dx[i],p.second+dy[i]};
            if(valid(j) and visit.count(j)==0 and grid[j.first][j.second]=='.')
            {
                parent[j.first][j.second]=p;
                visit.insert(j);
                q.push(j);
            }
        }
    }
    int ans=0;

    if(visit.count(finish))
    {
        int i=0,j=0;
        pair<int,int >p=parent[finish.first][finish.second];
       while(p!=start)
       {
           if(p.first==finish.first)
           {
               if(j!=0)
               ans+=(j-1)/k+1;
               j=0;
               i++;
           }
           else
           {
               if(i!=0)
              ans+=(i-1)/k+1;
               i=0;
               j++;
           }

           finish=p;
           p=parent[finish.first][finish.second];

       }
       if(p.first==finish.first)
           {
               i++;
               ans+=(i-1)/k+1;

           }
           else
           {
               j++;
              ans+=(j-1)/k+1;
            
           }
        cout<<ans<<endl;

    }
    else
        cout<<-1<<endl;
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>h>>w>>k;
    parent.assign(h,vector<pair<int,int> >(w,{-1,-1}));
    grid.assign(h,vector<char >(w));
    cin>>start.first>>start.second>>finish.first>>finish.second;
     start.first--,start.second--,finish.first--,finish.second--;
    for(auto &c:grid)
        for(auto &x:c)
            cin>>x;

    bfs();


}


Compiler output:
a.cc: In function 'void bfs()':
a.cc:29:5: error: reference to 'visit' is ambiguous
   29 |     visit.insert(start);
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:20: note:                 'std::set<std::pair<int, int> > visit'
   16 | set<pair<int,int> >visit;
      |                    ^~~~~
a.cc:39:29: error: reference to 'visit' is ambiguous
   39 |             if(valid(j) and visit.count(j)==0 and grid[j.first][j.second]=='.')
      |                             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:20: note:                 'std::set<std::pair<int, int> > visit'
   16 | set<pair<int,int> >visit;
      |                    ^~~~~
a.cc:42:17: error: reference to 'visit' is ambiguous
   42 |                 visit.insert(j);
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:20: note:                 'std::set<std::pair<int, int> > visit'
   16 | set<pair<int,int> >visit;
      |                    ^~~~~
a.cc:49:8: error: reference to 'visit' is ambiguous
   49 |     if(visit.count(finish))
      |        ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:20: note:                 'std::set<std::pair<int, int> > visit'
   16 | set<pair<int,int> >visit;
      |                    ^~~~~
a.cc: At global scope:
a.cc:92:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   92 | main()
      | ^~~~