Language: C++

//B 
#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b) 
using namespace std;
typedef long long ll;
typedef pair<int,int> Node; 
const int maxn = 505;
const int INF = 1e9;
bool G[maxn][maxn],vis[maxn][maxn];
int dis[maxn][maxn];
int p[maxn*maxn],n;
void spfa(int sx,int sy){
//	G[sx][sy] = 1;
	queue<Node>q;
	q.push(mp(sx,sy));
	dis[sx][sy] = 0;
	vis[sx][sy] = 1;
	while(!q.empty()){
		Node u = q.front();
		q.pop();
		int ux = u.first,uy = u.second;
		vis[ux][uy] = 0;
//		cout << ux << " " << uy << endl;
		for(int i = -1;i <= 1;i++)
			for(int j = -1;j <= 1;j++)if(abs(i+j) == 1){
				int vx = ux+i,vy = uy+j;
				if(vx < 0 or vx > n+1 or vy < 0 or vy > n+1)continue;
				if(dis[vx][vy] > dis[ux][uy] + (!G[vx][vy])){
					dis[vx][vy] = dis[ux][uy] + (!G[vx][vy]);
					if(!vis[vx][vy]){
						vis[vx][vy] = 1;
						q.push(mp(vx,vy));
					}
				}
			}
	}
	G[sx][sy] = 1;
}
int main(){
	cin >> n;
	ll ans = 0;
	for(int i = 0;i <= n+1;i++)G[0][i] = G[n+1][i] = G[i][0] = G[i][n+1] = 1;
	for(int t = 1;t <= n*n;t++){
		int x;
		cin >> x;
		int sx = (x-1)/n+1,sy = x%n;
		if(!sy)sy = n;
		for(int i = 0;i <= n+1;i++)
			for(int j = 0;j <= n+1;j++){
				dis[i][j] = INF;
				vis[i][j] = 0;
			}
		spfa(sx,sy);
		for(int i = 0;i <= n+1;i++)tmp = min({tmp,dis[0][i],dis[n+1][i],dis[i][0],dis[i][n+1]});
		ans += dis[0][0];
//		for(int i = 0;i <= n+1;i++){
//			for(int j = 0;j <= n+1;j++)cout << G[i][j] << " ";
//			cout << endl;
//		}
//		cout << t << " " << tmp << " " << dis[0][0] << endl;
//		cout << endl;
	}
	cout << ans;
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:54:44: error: 'tmp' was not declared in this scope; did you mean 'tm'?
   54 |                 for(int i = 0;i <= n+1;i++)tmp = min({tmp,dis[0][i],dis[n+1][i],dis[i][0],dis[i][n+1]});
      |                                            ^~~
      |                                            tm
a.cc:54:53: error: no matching function for call to 'min(<brace-enclosed initializer list>)'
   54 |                 for(int i = 0;i <= n+1;i++)tmp = min({tmp,dis[0][i],dis[n+1][i],dis[i][0],dis[i][n+1]});
      |                                                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 1 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   candidate expects 2 arguments, 1 provided