Language: C++

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 250010;
const int MAXM = 10010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m, k;
int ans, tot, cnt;

int a[MAXM][MAXM];
int vis[MAXM][MAXM];

string s;

inline int read() {
	int s = 0, f = 1;
	char ch = getchar();
	while ('0' > ch || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
	while ('0' <= ch && ch <= '9') {s = (s << 3) + (s << 1) + ch - 48; ch = getchar();}
	return s * f;
}

int d[] = {-1, 1, n, -n};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct data {
	int t, x, y;
//	bool operator > (const data x, const data y) {
//		return x.t < y.t;
//	}
	bool operator < (const data &c) const {
        return t > c.t;
    }
};

void Bfs(int s) {
	priority_queue<data> q;
	q.push(data{0, (s - 1) / n + 1, (s - 1) % n + 1});
//	qx, qy;
//	queue<int> q1;
//	qx.push((s - 1) / n + 1);
//	qy.push((s - 1) % n + 1);
//	printf("%d %d\n", qx.front(), qy.front());
//	q1.push(0);
	memset(vis, 0, sizeof(vis));
//	vis[z] = 0;
	int res = INF;
	while (q.size()) {
//		int x = qx.front(), y = qy.front(), t = q1.front();
		int x = q.top().x;
		int y = q.top().y;
		int t = q.top().t;
		q.pop();
//		qx.pop(); qy.pop();
//		q1.pop();
		vis[x][y] = 1;
//		if (x % n == 1 || x <= n || x > n * n - n || x % n == 0)
		if (x == 1 || x == n || y == 1 || y == n) {
			res = min(res, t);
//			continue;
//			printf("finish: %d\n", t);
			break;
		}
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (1 <= xx && xx <= n && 1 <= yy && yy <= n && !vis[xx][yy]) {
				q.push((data){t + a[xx][yy], xx, yy});
//				qx.push(xx);
//				qy.push(yy);
//				q1.push(t + a[xx][yy]);
			}
//			if (x / n * n < x + d[i] && x + d[i] <= x / n * n + n && !vis[x + d[i]]) {
//				q.push(x + d[i]);
//				q1.push(t + e[x][i]);
//			}
		}
//		for (int i = 2; i < 4; i++) {
//			if (0 < x + d[i] && x + d[i] <= n * n && !vis[x + d[i]]) {
//				q.push(x + d[i]);
//				q1.push(t + e[x][i]);
//			}
//		}
	}
	ans += res;
	return;
}

int p[MAXN];

int main(){
	int T;
	scanf("%d", &n);
	for (int i = 1; i <= n * n; i++) {
		scanf("%d", &p[i]);
	}
//	for (int i = 1; i <= n * n; i++) {
//		int c = i;
//		e[c][1] = e[c][2] = e[c][3] = e[c][0] = 1;
//	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			a[i][j] = 1;
		}
	}
		
	for (int i = 1; i <= n * n; i++) {
		Bfs(p[i]);
		a[(p[i] - 1) / n + 1][(p[i] - 1) % n + 1] = 0;
//		int c = n * (p[i] - 1) + p[i];
////		e[c][1] = e[c][2] = e[c][3] = e[c][4] = 0;
//		if (c > n) e[c - n][2] = 0;
//		if (c < n * n - n + 1) e[c + n][3] = 0;
//		e[c - 1][0] = e[c + 1][1] = 0;
//		print();
	}
	printf("%d\n", ans);
	return 0;
}



Compiler output:
a.cc: In function 'void Bfs(int)':
a.cc:44:28: error: template argument 1 is invalid
   44 |         priority_queue<data> q;
      |                            ^
a.cc:44:28: error: template argument 2 is invalid
a.cc:44:28: error: template argument 3 is invalid
a.cc:45:11: error: request for member 'push' in 'q', which is of non-class type 'int'
   45 |         q.push(data{0, (s - 1) / n + 1, (s - 1) % n + 1});
      |           ^~~~
a.cc:45:16: error: reference to 'data' is ambiguous
   45 |         q.push(data{0, (s - 1) / n + 1, (s - 1) % n + 1});
      |                ^~~~
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:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data {
      |        ^~~~
a.cc:55:18: error: request for member 'size' in 'q', which is of non-class type 'int'
   55 |         while (q.size()) {
      |                  ^~~~
a.cc:57:27: error: request for member 'top' in 'q', which is of non-class type 'int'
   57 |                 int x = q.top().x;
      |                           ^~~
a.cc:58:27: error: request for member 'top' in 'q', which is of non-class type 'int'
   58 |                 int y = q.top().y;
      |                           ^~~
a.cc:59:27: error: request for member 'top' in 'q', which is of non-class type 'int'
   59 |                 int t = q.top().t;
      |                           ^~~
a.cc:60:19: error: request for member 'pop' in 'q', which is of non-class type 'int'
   60 |                 q.pop();
      |                   ^~~
a.cc:75:35: error: request for member 'push' in 'q', which is of non-class type 'int'
   75 |                                 q.push((data){t + a[xx][yy], xx, yy});
      |                                   ^~~~
a.cc:75:41: error: reference to 'data' is ambiguous
   75 |                                 q.push((data){t + a[xx][yy], xx, yy});
      |                                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data {
      |        ^~~~