Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define all(s) s.begin(), s.end()
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second
#define bit(x, y) ((x >> y) & 1)

const int N = 505;

int n, dp[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
ii ord[N * N];
int a[N][N];
bool emp[N][N];

bool inG(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int id = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      emp[i][j] = 1;
      id++;
      int foo;
      cin >> foo;
      ord[id] = {(foo - 1) / n + 1, (foo - 1) % n + 1};
      a[ord[id].x][ord[id].y] = id;
      dp[i][j] = min({i, j, n + 1 - i, n + 1 - j});
    }
  }
  // for (int i = 1; i <= n; i++) {
  //     for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
  //     cout << endl;
  // }
  vector<vector<bool>> check(n + 1, vector<bool>(n + 1, 0));
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    queue<ii> q;
    q.push(ord[i]);
    // cout << ord[i].x <<  ' ' << ord[i].y << endl;
    emp[ord[i].x][ord[i].y] = 0;
    stack<ii> st;
    st.push(ord[i]);
    check[ord[i].x][ord[i].y] = 1;
    while (!q.empty()) {
      ii u = q.front();
      q.pop();
      dp[u.x][u.y]--;
      // cout << '(' << u.x << ' ' << u.y << ')' << ' ';
      for (int j = 0; j < 4; j++) {
        int x = u.x + dx[j];
        int y = u.y + dy[j];
        if (!inG(x, y))
          continue;
        if (check[x][y])
          continue;
        if (dp[x][y] == dp[u.x][u.y] + 1 + emp[x][y]) {
          check[x][y] = 1;
          q.push({x, y});
          st.push({x, y});
        }
      }
    }
    while (!st.empty()) {
      auto u = st.top();
      st.pop();
      check[u.x][u.y] = 0;
    }
    // cout << endl;
    // if (dp[ord[i].x][ord[i].y]) cout << ord[i].x <<' ' << ord[i].y << ' ' <<
    // dp[ord[i].x][ord[i].y] << endl;
    ans += dp[ord[i].x][ord[i].y];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

#define all(s) s.begin(), s.end()
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second
#define bit(x, y) ((x >> y) & 1)

const int N = 505;

int n, dp[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
ii ord[N * N];
int a[N][N];
bool emp[N][N];

bool inG(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int id = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      emp[i][j] = 1;
      id++;
      int foo;
      cin >> foo;
      ord[id] = {(foo - 1) / n + 1, (foo - 1) % n + 1};
      a[ord[id].x][ord[id].y] = id;
      dp[i][j] = min({i, j, n + 1 - i, n + 1 - j});
    }
  }
  // for (int i = 1; i <= n; i++) {
  //     for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
  //     cout << endl;
  // }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    queue<ii> q;
    q.push(ord[i]);
    // cout << ord[i].x <<  ' ' << ord[i].y << endl;
    emp[ord[i].x][ord[i].y] = 0;
    vector<vector<bool>> check(n + 1, vector<bool>(n + 1, 0));
    check[ord[i].x][ord[i].y] = 1;
    while (!q.empty()) {
      ii u = q.front();
      q.pop();
      dp[u.x][u.y]--;
      // cout << '(' << u.x << ' ' << u.y << ')' << ' ';
      for (int j = 0; j < 4; j++) {
        int x = u.x + dx[j];
        int y = u.y + dy[j];
        if (!inG(x, y))
          continue;
        if (check[x][y])
          continue;
        if (dp[x][y] == dp[u.x][u.y] + 1 + emp[x][y]) {
          check[x][y] = 1;
          q.push({x, y});
        }
      }
    }
    // cout << endl;
    // if (dp[ord[i].x][ord[i].y]) cout << ord[i].x <<' ' << ord[i].y << ' ' <<
    // dp[ord[i].x][ord[i].y] << endl;
    ans += dp[ord[i].x][ord[i].y];
  }
  cout << ans;
  return 0;
}