Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int n;
int arr[2505];
ll dp[505][505];
vector<vector<int> > visit(505,vector<int>(505,1));
ll cal1(ll x,ll y){
	//cout<<"ok"<<x<<" "<<y<<endl;
    if(x<=0 || x>n ||y<=0|| y>n) return 0;
    if(dp[x][y]!=-1) return dp[x][y];
    ll ans=n*n;
    ans=min(ans,visit[x][y]+cal1(x+1,y));
    ans=min(ans,visit[x][y]+cal1(x,y+1));
    //cout<<x<<" "<<y<<" "<<ans<<endl;
    return dp[x][y]=ans;
}
ll cal2(ll x,ll y){
	//cout<<"ok"<<x<<" "<<y<<endl;
    if(x<=0 || x>n ||y<=0|| y>n) return 0;
    if(dp[x][y]!=-1) return dp[x][y];
    ll ans=n*n;
    ans=min(ans,visit[x][y]+cal2(x+1,y));
    ans=min(ans,visit[x][y]+cal2(x,y-1));
    //cout<<x<<" "<<y<<" "<<ans<<endl;
    return dp[x][y]=ans;
}
ll cal3(ll x,ll y){
	//cout<<"ok"<<x<<" "<<y<<endl;
    if(x<=0 || x>n ||y<=0|| y>n) return 0;
    if(dp[x][y]!=-1) return dp[x][y];
    ll ans=n*n;
    ans=min(ans,visit[x][y]+cal3(x-1,y));
    ans=min(ans,visit[x][y]+cal3(x,y+1));
    //cout<<x<<" "<<y<<" "<<ans<<endl;
    return dp[x][y]=ans;
}
ll cal4(ll x,ll y){
	//cout<<"ok"<<x<<" "<<y<<endl;
    if(x<=0 || x>n ||y<=0|| y>n) return 0;
    if(dp[x][y]!=-1) return dp[x][y];
    ll ans=n*n;
    ans=min(ans,visit[x][y]+cal4(x-1,y));
    ans=min(ans,visit[x][y]+cal4(x,y-1));
    //cout<<x<<" "<<y<<" "<<ans<<endl;
    return dp[x][y]=ans;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin>>n;
    visit=vector<vector<int> >(n+1,vector<int>(n+1,1));
    for(int i=0; i<n*n; i++) cin>>arr[i];
    ll ans=0;
    for(int i=0; i<n*n; i++){
    	ll x=arr[i]/n;
    	ll y=((arr[i]%n)==0?n:arr[i]%n);
    	if(arr[i]%n) x++;
        visit[x][(arr[i]%n)==0?n:arr[i]%n]=0;
        //cout<<x<<" "<<y<<endl;
        //for(int j=1; j<=n; j++) {for(int k=1;k<=n; k++)cout<<visit[j][k]<<" ";cout<<endl;}        
        memset(dp,-1,sizeof dp);
        ll tans1=cal1(x,y);
        memset(dp,-1,sizeof dp);
        ll tans2=cal2(x,y);
        memset(dp,-1,sizeof dp);
        ll tans3=cal3(x,y);
        memset(dp,-1,sizeof dp);
        ll tans4=cal4(x,y);
        ans+=min(tans1,min(tans2,min(tans3,tans4)));
    }
    cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'll cal1(ll, ll)':
a.cc:14:17: error: reference to 'visit' is ambiguous
   14 |     ans=min(ans,visit[x][y]+cal1(x+1,y));
      |                 ^~~~~
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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc:15:17: error: reference to 'visit' is ambiguous
   15 |     ans=min(ans,visit[x][y]+cal1(x,y+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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc: In function 'll cal2(ll, ll)':
a.cc:24:17: error: reference to 'visit' is ambiguous
   24 |     ans=min(ans,visit[x][y]+cal2(x+1,y));
      |                 ^~~~~
/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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc:25:17: error: reference to 'visit' is ambiguous
   25 |     ans=min(ans,visit[x][y]+cal2(x,y-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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc: In function 'll cal3(ll, ll)':
a.cc:34:17: error: reference to 'visit' is ambiguous
   34 |     ans=min(ans,visit[x][y]+cal3(x-1,y));
      |                 ^~~~~
/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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc:35:17: error: reference to 'visit' is ambiguous
   35 |     ans=min(ans,visit[x][y]+cal3(x,y+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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc: In function 'll cal4(ll, ll)':
a.cc:44:17: error: reference to 'visit' is ambiguous
   44 |     ans=min(ans,visit[x][y]+cal4(x-1,y));
      |                 ^~~~~
/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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc:45:17: error: reference to 'visit' is ambiguous
   45 |     ans=min(ans,visit[x][y]+cal4(x,y-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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc: In function 'int main()':
a.cc:52:5: error: reference to 'visit' is ambiguous
   52 |     visit=vector<vector<int> >(n+1,vector<int>(n+1,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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~
a.cc:59:9: error: reference to 'visit' is ambiguous
   59 |         visit[x][(arr[i]%n)==0?n:arr[i]%n]=0;
      |         ^~~~~
/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:8:22: note:                 'std::vector<std::vector<int> > visit'
    8 | vector<vector<int> > visit(505,vector<int>(505,1));
      |                      ^~~~~