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

#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define st first
#define nd second
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = 501;
int n, p[N * N], q[N * N], dp[N * N];
int ans;
int vis[N * N], iter;
int getX(int x) { return x / n; }
int getY(int x) { return x % n; }
int dist(int a, int b) {
  return abs(getX(a) - getX(b)) + abs(getY(a) - getY(b));
}
void bfs(int X) {
  deque<int> Q;
  Q.push_back(X);
  while (Q.size()) {
    int x = Q.front();
    Q.pop_front();
    if (vis[x] == iter)
      continue;
    vis[x] = iter;
    if (getY(x) < n - 1) {
      if (q[x] < iter && dp[x + 1] > dp[x]) {
        dp[x + 1] = dp[x];
        Q.push_front(x + 1);
      } else if (dp[x + 1] > dp[x] + 1) {
        dp[x + 1] = dp[x] + 1;
        Q.push_back(x + 1);
      }
    }
    if (getY(x) > 0) {
      if (q[x] < iter && dp[x - 1] > dp[x]) {
        dp[x - 1] = dp[x];
        Q.push_front(x - 1);
      } else if (dp[x - 1] > dp[x] + 1) {
        dp[x - 1] = dp[x] + 1;
        Q.push_back(x - 1);
      }
    }
    if (getX(x) < n - 1) {
      if (q[x] < iter && dp[x + n] > dp[x]) {
        dp[x + n] = dp[x];
        Q.push_front(x + n);
      } else if (dp[x + n] > dp[x] + 1) {
        dp[x + n] = dp[x] + 1;
        Q.push_back(x + n);
      }
    }
    if (getX(x) > 0) {
      if (q[x] < iter && dp[x - n] > dp[x]) {
        dp[x - n] = dp[x];
        Q.push_front(x - n);
      } else if (dp[x - n] > dp[x] + 1) {
        dp[x - n] = dp[x] + 1;
        Q.push_back(x - n);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    p[i]--;
    q[p[i]] = i;
    dp[p[i]] =
        min({getX(p[i]), n - getX(p[i]) - 1, getY(p[i]), n - getY(p[i]) - 1});
  }
  for (int i = 0; i < n * n; ++i) {
    iter++;
    int cur = p[i];
    ans += dp[cur];
    bfs(cur);
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define st first
#define nd second
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = 501;
int n, p[N * N], q[N * N], dp[N * N];
int ans;
int vis[N * N], iter;
int getX(int x) { return x / n; }
int getY(int x) { return x % n; }
int dist(int a, int b) {
  return abs(getX(a) - getX(b)) + abs(getY(a) - getY(b));
}
void dfs(int x, int r) {
  if (vis[x] == iter)
    return;
  vis[x] = iter;
  if (x != r) {
    if (dp[r] + dist(x, r) - 1 >= dp[x]) {
      return;
    }
    dp[x] = dp[r] + dist(x, r) - 1;
  }
}
void bfs(int X) {
  deque<int> Q;
  Q.push_back(X);
  while (Q.size()) {
    int x = Q.front();
    Q.pop_front();
    if (vis[x] == iter)
      continue;
    vis[x] = iter;
    if (getY(x) < n - 1) {
      if (q[x] < iter && dp[x + 1] >= dp[x]) {
        dp[x + 1] = dp[x];
        Q.push_front(x + 1);
      } else if (dp[x + 1] >= dp[x] + 1) {
        dp[x + 1] = dp[x] + 1;
        Q.push_back(x + 1);
      }
    }
    if (getY(x) > 0) {
      if (q[x] < iter && dp[x - 1] >= dp[x]) {
        dp[x - 1] = dp[x];
        Q.push_front(x - 1);
      } else if (dp[x - 1] >= dp[x] + 1) {
        dp[x - 1] = dp[x] + 1;
        Q.push_back(x - 1);
      }
    }
    if (getX(x) < n - 1) {
      if (q[x] < iter && dp[x + n] >= dp[x]) {
        dp[x + n] = dp[x];
        Q.push_front(x + n);
      } else if (dp[x + n] >= dp[x] + 1) {
        dp[x + n] = dp[x] + 1;
        Q.push_back(x + n);
      }
    }
    if (getX(x) > 0) {
      if (q[x] < iter && dp[x - n] >= dp[x]) {
        dp[x - n] = dp[x];
        Q.push_front(x - n);
      } else if (dp[x - n] >= dp[x] + 1) {
        dp[x - n] = dp[x] + 1;
        Q.push_back(x - n);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    p[i]--;
    q[p[i]] = i;
    dp[p[i]] =
        min({getX(p[i]), n - getX(p[i]) - 1, getY(p[i]), n - getY(p[i]) - 1});
  }
  for (int i = 0; i < n * n; ++i) {
    iter++;
    int cur = p[i];
    ans += dp[cur];
    bfs(cur);
  }
  cout << ans;
}