#include #include #include #include #include #include using namespace std; using P = pair; using TP = tuple; const vector dx = {+0,+1,+1,+0,+1}; const vector dy = {+1,+1,+0,-1,-1}; queue q; void bfs(int s, const int& n, const vector>& a, vector>& dist) { q.emplace(s,0,0); vector> visited(n, vector (n,false)); while(!q.empty()) { auto [y,x,c] = q.front(); q.pop(); if (visited[y][x]) continue; visited[y][x] = true; dist[s][a[y][x]-1] += c; for (int i=0; i<5; i++) { int xi = x + dx[i]; int yi = y + dy[i]; if (xi<0 || n<=xi) continue; if (yi<0 || n<=yi) continue; if (visited[yi][xi]) continue; q.emplace(yi, xi, c+1); } } } int main() { int n; cin >> n; vector> a(n,vector(n,0)); for (int i=0; i> dist(n, vector(n, 0)); for (int i=0; i ans(n, INT_MAX); for (int i=0; i