Language: C++

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n;i++)
using namespace std;
typedef pair<int,int> pii;
int INF=1e9;
string ans;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool inrange(int y,int x){return (0<=x&&x<=2000&&0<=y&&y<=2000);}
vector<vector<bool>>flag(2001,vector<bool>(2001,true));
int mini;
pii _start,_end;
bool visit=false;
bool dfs(pii now,int d,string s){
    if(d>mini)return false;
    if(d==mini&&now==_end&&visit){
        visit=false;
        ans+=s; return true;
    }
    rep(i,4){
        string temp=s;
        pii next=pii(now.first+dy[i],now.second+dx[i]);
        if(!inrange(next.first, next.second))continue;
        if(dy[i]==1&&dx[i]==0)temp+="U";
        else if(dy[i]==-1&&dx[i]==0)temp+="D";
        else if(!dy[i] && dx[i]==1)temp+="R";
        else temp+="L";
        if(dfs(next,d+1,temp))flag[now.first][now.second]=false;
    }
    return false;
}
void bfs(pii s,pii g){
    queue<pii>que;
    visit=true;
    vector<vector<int>>d(2001,vector<int>(2001,INF));
    que.push(s);
    d[s.first][s.second]=0;
    while(!que.empty()){
        pii now=que.front();
        que.pop();
        if(now==g)break;
        rep(i,4){
            pii next=pii(now.first+dx[i],now.second+dy[i]);
            if(d[next.first][next.second]==INF&&inrange(next.first,next.second)){
                if(!flag[next.first][next.second])continue;
                que.push(next);
                d[next.first][next.second]=d[now.first][now.second]+1;
            }
        }
    }
    mini=d[g.first][g.second];
    _start=s;
    _end=g;
    dfs(_start,0,"");
    flag[s.first][s.second]=true;
    
}
int main(){
    pii s,g;
    cin>>s.second>>s.first>>g.second>>g.first;
    s.first+=1000; s.second+=1000;
    g.first+=1000; g.second+=1000;
    bfs(s,g);
    bfs(g,s);
    bfs(s,g);
    bfs(g,s);
    cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'bool dfs(pii, int, std::string)':
a.cc:16:28: error: reference to 'visit' is ambiguous
   16 |     if(d==mini&&now==_end&&visit){
      |                            ^~~~~
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:13:6: note:                 'bool visit'
   13 | bool visit=false;
      |      ^~~~~
a.cc:17:9: error: reference to 'visit' is ambiguous
   17 |         visit=false;
      |         ^~~~~
/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:13:6: note:                 'bool visit'
   13 | bool visit=false;
      |      ^~~~~
a.cc: In function 'void bfs(pii, pii)':
a.cc:34:5: error: reference to 'visit' is ambiguous
   34 |     visit=true;
      |     ^~~~~
/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:13:6: note:                 'bool visit'
   13 | bool visit=false;
      |      ^~~~~