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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 505

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<int, int> par;
typedef pair<int, par> par1;

int N, A[maxn][maxn], nc, nf, cost, C[maxn][maxn], sol, a;
par B[maxn * maxn];
deque<par> Q;
int movf[] = {0, 0, 1, -1};
int movc[] = {1, -1, 0, 0};

int bfs(int f, int c) {
  Q.push_front(par(f, c));
  A[f][c] = 0;
  int ret = C[f][c];
  while (!Q.empty()) {
    f = Q.front().fi;
    c = Q.front().se;
    Q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nf = f + movf[i], nc = c + movc[i];
      if (nf < 1 || nf > N || nc < 1 || nc > N) {
        continue;
      }

      if (C[nf][nc] > C[f][c] + (A[f][c] != 0)) {
        C[nf][nc] = C[f][c] + (A[f][c] != 0);
        if (A[f][c])
          Q.push_back(par(nf, nc));
        else
          Q.push_front(par(nf, nc));
      }
    }
  }
  return ret;
}
int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;

  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      A[i][j] = (i - 1) * N + j;
      B[(i - 1) * N + j] = par(i, j);
      C[i][j] = min(min(i, j) - 1, min(N - i, N - j));
    }

  for (int i = 1; i <= N * N; i++) {
    cin >> a;
    sol += bfs(B[a].fi, B[a].se);
  }

  cout << sol;

  /*
  4
  6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8


  */
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 505

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<int, int> par;
typedef pair<int, par> par1;

int N, A[maxn][maxn], nc, nf, cost, C[maxn][maxn], sol, a;
par B[maxn * maxn];
deque<par> Q;
int movf[] = {0, 0, 1, -1};
int movc[] = {1, -1, 0, 0};

int bfs(int f, int c) {
  int ret = 1 << 30;
  Q.push_front(par(f, c));
  for (int i = 1; i <= N; i++)
    fill(C[i] + 1, C[i] + 1 + N, 1 << 30);
  C[f][c] = 0;
  A[f][c] = 0;
  while (!Q.empty()) {
    f = Q.front().fi;
    c = Q.front().se;
    Q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nf = f + movf[i], nc = c + movc[i];
      if (nf < 1 || nf > N || nc < 1 || nc > N) {
        ret = min(ret, C[f][c]);
        continue;
      }

      if (C[nf][nc] > C[f][c] + (A[nf][nc] != 0)) {
        C[nf][nc] = C[f][c] + (A[nf][nc] != 0);
        if (A[nf][nc])
          Q.push_back(par(nf, nc));
        else
          Q.push_front(par(nf, nc));
      }
    }
  }
  return ret;
}
int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;

  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      A[i][j] = (i - 1) * N + j;
      B[(i - 1) * N + j] = par(i, j);
    }

  for (int i = 1; i <= N * N; i++) {
    cin >> a;
    sol += bfs(B[a].fi, B[a].se);
  }

  cout << sol;
}