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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int DEBUG = 0;

int n;
vector<vector<int>> d;
vector<vector<bool>> ex;

void bfs(int i, int j) {
  vector<P> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (auto p : dir) {
    int ni = i + p.second, nj = j + p.first;
    if (!(0 <= ni && ni < n && 0 <= nj && nj < n) || d[ni][nj] <= d[i][j])
      continue;

    if (ex[i][j]) {
      if (d[ni][nj] == d[i][j] + 1)
        continue;
      d[ni][nj] = d[i][j] + 1;
    } else
      d[ni][nj] = d[i][j];

    bfs(ni, nj);
  }
}

int main() {
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++)
    cin >> p[i];

  for (int i = 0; i < n; i++) {
    vector<int> di;
    vector<bool> exi;
    for (int j = 0; j < n; j++) {
      int dij = min(min(i, n - i - 1), min(j, n - j - 1));
      di.push_back(dij);
      exi.push_back(true);
    }
    d.push_back(di);
    ex.push_back(exi);
  }

  if (DEBUG >= 1) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (ex[i][j])
          cout << " " << d[i][j] << " ";
        else
          cout << "[" << d[i][j] << "]";
      }
      cout << endl;
    }
    cout << endl;
  }

  int ans = 0;
  for (auto pi : p) {
    pi--;
    int i = pi / n, j = pi % n;
    ex[i][j] = false;
    ans += d[i][j];
    bfs(i, j);

    if (DEBUG) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (ex[i][j])
            cout << " " << d[i][j] << " ";
          else
            cout << "[" << d[i][j] << "]";
        }
        cout << endl;
      }
      cout << endl;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int DEBUG = 0;

int n;
vector<vector<int>> d;
vector<vector<bool>> ex;

void bfs(int i, int j) {
  vector<P> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (auto p : dir) {
    int ni = i + p.second, nj = j + p.first;
    if (!(0 <= ni && ni < n && 0 <= nj && nj < n) || d[ni][nj] <= d[i][j])
      continue;

    if (ex[i][j])
      d[ni][nj] = d[i][j] + 1;
    else
      d[ni][nj] = d[i][j];

    bfs(ni, nj);
  }
}

int main() {
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++)
    cin >> p[i];

  for (int i = 0; i < n; i++) {
    vector<int> di;
    vector<bool> exi;
    for (int j = 0; j < n; j++) {
      int dij = min(min(i, n - i - 1), min(j, n - j - 1));
      di.push_back(dij);
      exi.push_back(true);
    }
    d.push_back(di);
    ex.push_back(exi);
  }

  if (DEBUG >= 1) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (ex[i][j])
          cout << " " << d[i][j] << " ";
        else
          cout << "[" << d[i][j] << "]";
      }
      cout << endl;
    }
    cout << endl;
  }

  int ans = 0;
  for (auto pi : p) {
    pi--;
    int i = pi / n, j = pi % n;
    ex[i][j] = false;
    ans += d[i][j];
    bfs(i, j);

    if (DEBUG) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (ex[i][j])
            cout << " " << d[i][j] << " ";
          else
            cout << "[" << d[i][j] << "]";
        }
        cout << endl;
      }
      cout << endl;
    }
  }

  cout << ans << endl;

  return 0;
}