WA26分第三个样例过不了(郊游)

rt

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


struct p
{
	int x, y;
};
const int N = 510;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, a[N][N];
bool exist[N][N];

void bfs(p st)
{
	queue<p> q;
	q.push(st);
	while(!q.empty())
	{
		p cur = q.front();
		q.pop();
		for(int i = 0; i < 4; i ++)
		{
			p nxt;
			nxt.x = cur.x + dir[i][0];
			nxt.y = cur.y + dir[i][1];
			if(nxt.x < 0 || nxt.x >= n || nxt.y < 0 || nxt.y >= n)
				continue;
			if(a[nxt.x][nxt.y] > a[cur.x][cur.x] + exist[cur.x][cur.y]) 
			{
				a[nxt.x][nxt.y] = a[cur.x][cur.x] + exist[cur.x][cur.y];
				q.push(nxt);
			}
		}
	}
}

void print()
{
	for(int i = 0; i < n; i ++)
	{
		for(int j = 0; j < n; j ++)
		{
			cout << a[i][j] << ' ';
		}
		cout << endl;
	}
}

int main()
{
	cin >> n;
	for(int i = 0; i < n; i ++)
	{
		for(int j = 0; j < n; j ++)
		{
			a[i][j] = min(min(i, j), min(n-i-1, n-j-1));
			//cout << a[i][j] << ' ';
			exist[i][j] = 1;
		}
		//cout << endl;
	}
	int q, res = 0;
	for(int i = 0; i < n * n; i ++)
	{
		scanf("%d", &q);
		q -= 1;
		p st;
		st.x = q/n;
		st.y = q%n;
		res += a[st.x][st.y];
		exist[st.x][st.y] = 0;
		//cout << st.x << ' ' << st.y << endl;
		//cout << endl;
		bfs(st);
		//print();
	}
	cout << res;
	return 0;
}

解决了