Language: C++

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define fprintf if(false) fprintf
#endif
#define debug_int(x) fprintf(stderr, "%s = %d\n", #x, x)
#define debug_double(x) fprintf(stderr, "%s = %llf\n", #x, x)
#define debug_array(v,n) fprintf(stderr, "%s = [ ", #v); for(int i=0; i<n; i++) fprintf(stderr, "%d ", v[i]); fprintf(stderr, "]\n")
#define forr(i,start,end) for(int i=start; i<end; i++)

using lli = long long int;
using ii = pair<int, int>;

const int MAXN = 501;
const ii dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int dp[MAXN][MAXN];
bool empty[MAXN][MAXN];

int main() {
    int n;
    scanf("%d", &n);

    vector<int> p(n*n);
    for(int i=0; i<n*n; i++) {
        scanf("%d", &p[i]);
    }
    
    for(int row=1; row<=n; row++) {
        for(int col=1; col<=n; col++) {
            dp[row][col] = min({row-1, n-row, col-1, n-col});
        }
    }

    lli ans = 0;
    for(int i=0; i<n*n; i++) {
        int row = (p[i]-1)/n+1;
        int col = ((p[i]-1)%n)+1;

        ans += lli(dp[row][col]);
        empty[row][col] = true;
        
        queue<ii> q;
        q.push({row, col});
        while(!q.empty()) {
            ii u = q.front(); q.pop();

            for(int j=0; j<4; j++) {
                int nrow = u.first + dir[j].first;
                int ncol = u.second + dir[j].second;

                if(nrow < 1 or nrow > n or ncol < 1 or ncol > n) {
                    continue;
                }

                if(dp[nrow][ncol] > dp[u.first][u.second]) {
                    dp[nrow][ncol] = dp[u.first][u.second];

                    if(empty[nrow][ncol]) {
                        q.push({nrow, ncol});
                    }
                }
            }
        }
    }
    printf("%I64d\n", ans);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:42:9: error: reference to 'empty' is ambiguous
   42 |         empty[row][col] = true;
      |         ^~~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:302:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(initializer_list<_Tp>)'
  302 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:292:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  292 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:282:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  282 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
a.cc:19:6: note:                 'bool empty [501][501]'
   19 | bool empty[MAXN][MAXN];
      |      ^~~~~
a.cc:60:24: error: reference to 'empty' is ambiguous
   60 |                     if(empty[nrow][ncol]) {
      |                        ^~~~~
/usr/include/c++/14/bits/range_access.h:302:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(initializer_list<_Tp>)'
  302 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:292:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  292 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:282:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  282 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
a.cc:19:6: note:                 'bool empty [501][501]'
   19 | bool empty[MAXN][MAXN];
      |      ^~~~~