Language: C++

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

int main() {
source:
    ll n;
    cin >> n;
    ll p[n * n];
    rep(i, n * n) {
        cin >> p[i];
        p[i]--;
    }

    ll distance[n][n] = {};
    rep(i, n) {
        rep(j, n) {
            ll d = min(min(i, n - i - 1), min(j, n - j - 1));
            distance[i][j] = d + 1;
        }
    }

    ll x_ar[] = {-1, 0, 0, 1}, y_ar[] = {0, -1, 1, 0};
    bool remain[n][n];
    memset(-1, remain, sizeof(remain));

    ll ans = 0;
    rep(i, n * n) {
        ll num = p[i];
        ll x = num / n, y = num % n;
        ans += distance[x][y] - 1;
        remain[x][y] = false;
        queue<pair<ll, ll>> q;
        q.push(make_pair(x, y));
        bool seen[n][n] = {};
        while (!q.empty()) {
            auto p = q.front();
            q.pop();
            ll x_now = p.first, y_now = p.second;
            if (seen[x_now][y_now]) {
                continue;
            }
            seen[x_now][y_now] = true;
            ll value = distance[x_now][y_now];
            rep(j, 4) {
                ll x_next = x_now + x_ar[j], y_next = y_now + y_ar[j];
                if (x_next < 0 || n <= x_next) {
                    continue;
                }
                if (y_next < 0 || n <= y_next) {
                    continue;
                }
                if (remain[x_next][y_next] &&
                    distance[x_next][y_next] == value + 1) {
                    if (!seen[x_next][y_next]) {
                        q.push(make_pair(x_next, y_next));
                    }
                } else if (!remain[x_next][y_next] &&
                           distance[x_next][y_next] == value) {
                    if (!seen[x_next][y_next]) {
                        q.push(make_pair(x_next, y_next));
                    }
                }
            }
            distance[x_now][y_now]--;
        }
    }

    cout << ans << endl;

    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:33:12: error: invalid conversion from 'int' to 'void*' [-fpermissive]
   33 |     memset(-1, remain, sizeof(remain));
      |            ^~
      |            |
      |            int
In file included from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/string.h:61:28: note:   initializing argument 1 of 'void* memset(void*, int, size_t)'
   61 | extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
      |                      ~~~~~~^~~
a.cc:33:16: error: invalid conversion from 'bool (*)[n]' to 'int' [-fpermissive]
   33 |     memset(-1, remain, sizeof(remain));
      |                ^~~~~~
      |                |
      |                bool (*)[n]
/usr/include/string.h:61:37: note:   initializing argument 2 of 'void* memset(void*, int, size_t)'
   61 | extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
      |                                 ~~~~^~~